@nx/vite
Version:
51 lines (50 loc) • 1.98 kB
JavaScript
// source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js
/**
* @function replaceFiles
* @param {FileReplacement[]} replacements
* @return {({name: "rollup-plugin-replace-files", enforce: "pre" | "post" | undefined, Promise<resolveId>})}
*/ "use strict";
Object.defineProperty(exports, "replaceFiles", {
enumerable: true,
get: function() {
return replaceFiles;
}
});
const _extends = require("@swc/helpers/_/_extends");
function replaceFiles(replacements) {
if (!(replacements == null ? void 0 : replacements.length)) {
return null;
}
return {
name: 'rollup-plugin-replace-files',
enforce: 'pre',
async resolveId (source, importer, options) {
const resolved = await this.resolve(source, importer, _extends._({}, 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)=>{
var _resolved_id;
return resolved == null ? void 0 : (_resolved_id = resolved.id) == null ? void 0 : _resolved_id.endsWith(replacement.replace);
});
if (foundReplace) {
console.info(`replace "${foundReplace.replace}" with "${foundReplace.with}"`);
try {
// return new file content
return {
id: foundReplace.with
};
} catch (err) {
console.error(err);
return null;
}
}
return null;
}
};
}
//# sourceMappingURL=rollup-replace-files.plugin.js.map