@modern-js/builder
Version:
Builder of modern.js.
90 lines (89 loc) • 3.97 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var svg_exports = {};
__export(svg_exports, {
builderPluginSvg: () => builderPluginSvg
});
module.exports = __toCommonJS(svg_exports);
var import_path = __toESM(require("path"));
var import_builder_shared = require("@modern-js/builder-shared");
const builderPluginSvg = () => {
return {
name: "builder-plugin-svg",
setup(api) {
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
const config = api.getNormalizedConfig();
const defaultExport = config.output.svgDefaultExport;
const assetType = "svg";
const distDir = (0, import_builder_shared.getDistPath)(config.output, "svg");
const filename = (0, import_builder_shared.getFilename)(config.output, "svg", isProd);
const outputName = import_path.default.posix.join(distDir, filename);
const maxSize = config.output.dataUriLimit[assetType];
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(import_builder_shared.SVG_REGEX);
if (config.output.disableSvgr) {
(0, import_builder_shared.chainStaticAssetRule)({
rule,
maxSize,
filename: import_path.default.posix.join(distDir, filename),
assetType
});
return;
}
(0, import_builder_shared.chainStaticAssetRule)({
rule,
maxSize,
filename: import_path.default.posix.join(distDir, filename),
assetType,
issuer: {
// The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
not: [
import_builder_shared.JS_REGEX,
import_builder_shared.TS_REGEX
]
}
});
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type("asset/inline").resourceQuery(/inline/);
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/url/).set("generator", {
filename: outputName
});
rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").use(CHAIN_ID.USE.SVGR).loader(require.resolve("@svgr/webpack")).options({
svgo: true,
svgoConfig: (0, import_builder_shared.getSvgoDefaultConfig)()
}).end().when(defaultExport === "url", (c) => c.use(CHAIN_ID.USE.URL).loader((0, import_builder_shared.getSharedPkgCompiledPath)("url-loader")).options({
limit: config.output.dataUriLimit.svg,
name: outputName
}));
});
}
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
builderPluginSvg
});