nx/packages/vite/plugins/rollup-replace-files.plugin.ts
nodegin b3ff11f5d9
fix(vite): Support fileReplacements for devServer (#13761)
Co-authored-by: ⁢ <john@doe.gov>
2022-12-13 12:11:29 +02:00

52 lines
1.4 KiB
TypeScript

// source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js
import * as fs from 'fs';
import { resolve } from 'path';
/**
* @function replaceFiles
* @param {FileReplacement[]} replacements
* @return {({name: "rollup-plugin-replace-files", enforce: "pre", Promise<resolveId>})}
*/
export default function replaceFiles(replacements: FileReplacement[]) {
if (!replacements?.length) {
return null;
}
return {
name: 'rollup-plugin-replace-files',
enforce: 'pre',
async transform(code, id) {
/**
* 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) =>
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();
} catch (err) {
console.error(err);
return code;
}
}
return code;
},
};
}
export interface FileReplacement {
replace: string;
with: string;
}