From b1c05fee4868b6455cbdf757e5a60b4513a2972b Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 18 Nov 2022 17:24:33 +0200 Subject: [PATCH] fix(bundling): file replacement for vite (#13255) (cherry picked from commit e4092aa47f31a3cff3c5a8fb9ffbf45377786976) --- .../plugins/rollup-replace-files.plugin.ts | 21 +++++++++++++++---- packages/vite/src/utils/helper-functions.ts | 4 +--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/vite/plugins/rollup-replace-files.plugin.ts b/packages/vite/plugins/rollup-replace-files.plugin.ts index 209f514674586..525c213ec857e 100644 --- a/packages/vite/plugins/rollup-replace-files.plugin.ts +++ b/packages/vite/plugins/rollup-replace-files.plugin.ts @@ -1,5 +1,7 @@ // source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js +import { resolve } from 'path'; + /** * @function replaceFiles * @param {FileReplacement[]} replacements @@ -12,10 +14,21 @@ export default function replaceFiles(replacements: FileReplacement[]) { return { name: 'rollup-plugin-replace-files', enforce: 'pre', - async resolveId(source, importer) { - const resolved = await this.resolve(source, importer, { skipSelf: true }); - const foundReplace = replacements.find( - (replacement) => replacement.replace === resolved?.id + async resolveId(source, importer, options) { + const resolved = await this.resolve(source, importer, { + ...options, + skipSelf: true, + }); + + /** + * The reason we're using endsWith here is because the resolved id + * will be the absolute path to the file. We want to check if the + * file ends with the file we're trying to replace, which will be essentially + * the path from the root of our workspace. + */ + + const foundReplace = replacements.find((replacement) => + resolved?.id?.endsWith(replacement.replace) ); if (foundReplace) { console.info( diff --git a/packages/vite/src/utils/helper-functions.ts b/packages/vite/src/utils/helper-functions.ts index 945f3bf8c318e..9e2219094cdf5 100644 --- a/packages/vite/src/utils/helper-functions.ts +++ b/packages/vite/src/utils/helper-functions.ts @@ -41,6 +41,7 @@ export async function getBuildConfig( context.root, projectRoot ), + plugins: [replaceFiles(options.fileReplacements)], build: getViteBuildOptions( options as ViteDevServerExecutorOptions & ViteBuildExecutorOptions, projectRoot @@ -144,9 +145,6 @@ export function getViteBuildOptions( commonjsOptions: { transformMixedEsModules: true, }, - rollupOptions: { - plugins: [replaceFiles(options.fileReplacements)], - }, }; buildOptions = {