Skip to content

Commit

Permalink
Revert "fix(vite): Support fileReplacements for devServer" (#14104)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jan 3, 2023
1 parent c829756 commit 53cffac
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/vite/plugins/rollup-replace-files.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js

import * as fs from 'fs';
import { resolve } from 'path';

/**
Expand All @@ -15,7 +14,11 @@ export default function replaceFiles(replacements: FileReplacement[]) {
return {
name: 'rollup-plugin-replace-files',
enforce: 'pre',
async transform(code, 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
Expand All @@ -24,23 +27,23 @@ export default function replaceFiles(replacements: FileReplacement[]) {
*/

const foundReplace = replacements.find((replacement) =>
id.endsWith(replacement.replace)
resolved?.id?.endsWith(replacement.replace)
);
if (foundReplace) {
console.info(
`replace "${foundReplace.replace}" with "${foundReplace.with}"`
);
try {
// return new file content
return fs
.readFileSync(id.replace(foundReplace.replace, foundReplace.with))
.toString();
return {
id: foundReplace.with,
};
} catch (err) {
console.error(err);
return code;
return null;
}
}
return code;
return null;
},
};
}
Expand Down

0 comments on commit 53cffac

Please sign in to comment.