beesbuild
Version:
构建工具链
52 lines (51 loc) • 1.61 kB
JavaScript
import rollupAlias from "@rollup/plugin-alias";
import { flattenDeep } from "lodash-unified";
import { resolveAliases } from "../../utils.mjs";
function themeChalkAlias(themeChalk, ctx) {
if ((themeChalk != null ? themeChalk : false) === false) return [];
if (typeof themeChalk === "boolean") {
themeChalk = {};
}
return {
name: "theme-chalk-alias-plugin",
resolveId(id) {
for (const [sourceThemeChalk, bundleThemeChalk] of Object.entries(themeChalk)) {
if (id.startsWith(sourceThemeChalk) && bundleThemeChalk) {
return {
id: id.replaceAll(sourceThemeChalk, bundleThemeChalk),
external: "absolute"
};
}
}
}
};
}
function baseAliasPlugin(alias, ctx) {
const entries = resolveAliases(ctx, false);
if (entries.length === 0) return [];
return rollupAlias({
entries,
async customResolver(id, importer, options) {
const resolved = await this.resolve(id, importer, options);
return resolved || { id, meta: { "vite:alias": { noResolved: true } } };
}
});
}
function aliasPlugin(alias, ctx) {
var _a;
const plugins = [];
const rollup = ctx == null ? void 0 : ctx.options.rollup;
const replaceAlias = (_a = ctx == null ? void 0 : ctx.options.replace) == null ? void 0 : _a.alias;
if (alias || replaceAlias) {
plugins.push(baseAliasPlugin(alias, ctx));
}
if (rollup == null ? void 0 : rollup.themeChalkAlias) {
plugins.push(themeChalkAlias(rollup.themeChalkAlias, ctx));
}
return flattenDeep(plugins);
}
export {
aliasPlugin,
baseAliasPlugin,
themeChalkAlias
};