Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundling): file replacement for vite #13255

Merged
merged 1 commit into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions 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
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions packages/vite/src/utils/helper-functions.ts
Expand Up @@ -41,6 +41,7 @@ export async function getBuildConfig(
context.root,
projectRoot
),
plugins: [replaceFiles(options.fileReplacements)],
build: getViteBuildOptions(
options as ViteDevServerExecutorOptions & ViteBuildExecutorOptions,
projectRoot
Expand Down Expand Up @@ -144,9 +145,6 @@ export function getViteBuildOptions(
commonjsOptions: {
transformMixedEsModules: true,
},
rollupOptions: {
plugins: [replaceFiles(options.fileReplacements)],
},
};

buildOptions = {
Expand Down