UNPKG

@modern-js/builder

Version:
107 lines (106 loc) 4.04 kB
"use strict"; 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 rem_exports = {}; __export(rem_exports, { builderPluginRem: () => builderPluginRem }); module.exports = __toCommonJS(rem_exports); var import_lodash = __toESM(require("@modern-js/utils/lodash")); var import_builder_shared = require("@modern-js/builder-shared"); const defaultOptions = { enableRuntime: true, rootFontSize: 50 }; const builderPluginRem = () => ({ name: "builder-plugin-rem", pre: [ "builder-plugin-css", "builder-plugin-less", "builder-plugin-sass", "builder-plugin-stylus" ], setup(api) { api.modifyBundlerChain(async (chain, { CHAIN_ID, isServer, isWebWorker, HtmlPlugin }) => { const config = api.getNormalizedConfig(); const { output: { convertToRem } } = config; if (!convertToRem || isServer || isWebWorker) { return; } const userOptions = { ...defaultOptions, ...typeof convertToRem === "boolean" ? {} : convertToRem }; const { default: PxToRemPlugin } = await Promise.resolve().then(() => __toESM(require((0, import_builder_shared.getSharedPkgCompiledPath)("postcss-pxtorem")))); const applyRules = [ CHAIN_ID.RULE.CSS, CHAIN_ID.RULE.LESS, CHAIN_ID.RULE.SASS, CHAIN_ID.RULE.STYLUS ]; const getPxToRemPlugin = () => PxToRemPlugin({ rootValue: userOptions.rootFontSize, unitPrecision: 5, propList: [ "*" ], ...import_lodash.default.cloneDeep(userOptions.pxtorem || {}) }); applyRules.forEach((name) => { chain.module.rules.has(name) && chain.module.rule(name).use(CHAIN_ID.USE.POSTCSS).tap((options = {}) => { var _options_postcssOptions; return { ...options, postcssOptions: { ...options.postcssOptions || {}, plugins: [ ...((_options_postcssOptions = options.postcssOptions) === null || _options_postcssOptions === void 0 ? void 0 : _options_postcssOptions.plugins) || [], getPxToRemPlugin() ] } }; }); }); if (!userOptions.enableRuntime) { return; } const entries = Object.keys(chain.entryPoints.entries() || {}); const distDir = (0, import_builder_shared.getDistPath)(config.output, "js"); chain.plugin(CHAIN_ID.PLUGIN.AUTO_SET_ROOT_SIZE).use(import_builder_shared.AutoSetRootFontSizePlugin, [ userOptions, entries, HtmlPlugin, distDir ]); }); } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { builderPluginRem });