antd4-theme-vars
Version:
Generate different theme files for antd@4.x
150 lines (148 loc) • 5.98 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);
// src/generate.ts
var generate_exports = {};
__export(generate_exports, {
default: () => generate,
filterAntdGlobalStylePlugin: () => filterAntdGlobalStylePlugin,
outputThemeFile: () => outputThemeFile,
uniqueThemeConfigs: () => uniqueThemeConfigs
});
module.exports = __toCommonJS(generate_exports);
var import_constants = require("./constants");
var import_path = require("path");
var import_clean_css = __toESM(require("clean-css"));
var import_logger = __toESM(require("./logger"));
var import_chalk = __toESM(require("chalk"));
var import_less = __toESM(require("less"));
var import_postcss = __toESM(require("postcss"));
var import_fs_extra = require("fs-extra");
function uniqueThemeConfigs(themes) {
const themesMap = /* @__PURE__ */ new Map();
themes.forEach((config) => {
const { prefixCls } = config;
if (!prefixCls) {
import_logger.default.warn(`The prefixCls \`${prefixCls}\` is invalid. Skipped.`);
return;
}
if (prefixCls === import_constants.DEFAULT_ANT_PREFIX) {
import_logger.default.warn(
`Default \`${import_constants.DEFAULT_ANT_PREFIX}\` prefixCls is not allowed. Skipped.`
);
return;
}
themesMap.set(prefixCls, config);
});
return Array.from(themesMap.values());
}
function filterAntdGlobalStylePlugin(options) {
const { prefixCls } = options;
return {
postcssPlugin: "filter-antd-global-style",
// 插件名称
Root(root) {
root.walkRules((rule) => {
if (!(rule.selector.startsWith(`.${prefixCls}-`) || rule.selector.startsWith(`[class^=${prefixCls}-]`) || rule.selector.startsWith(`[class*=${prefixCls}-]`))) {
rule.remove();
}
});
}
};
}
async function outputThemeFile(themeConfig, globalConfig) {
const { prefixCls, fileName, variables } = themeConfig;
const {
outputDir: _outputDir,
minifyCSS = true,
antdLessPath: _antdLessPath,
antdLessLookingPaths: _antdLessLookingPaths
} = globalConfig;
try {
const _replacedVars = Object.entries(variables ?? {}).reduce((prev, [_varName, varValue]) => {
const varName = _varName.startsWith("@") ? _varName : `@${_varName}`;
const current = { ...prev };
current[varName] = varValue;
return current;
}, {});
const replacedVars = {
"@theme": fileName,
..._replacedVars,
"@ant-prefix": prefixCls
};
const outputDir = _outputDir || (0, import_path.join)(process.cwd(), import_constants.DEFAULT_OUTPUT_DIR);
const antdLessPath = _antdLessPath || (0, import_path.join)(process.cwd(), import_constants.DEFAULT_ANTD_LESS_PATH);
const antdDirPaths = _antdLessLookingPaths || [
(0, import_path.join)(process.cwd(), import_constants.DEFAULT_ANTD_LESS_LOOKING_PATH)
];
const targetCSSPath = (0, import_path.join)(outputDir, `${fileName}.css`);
const antdLessContent = (0, import_fs_extra.readFileSync)(antdLessPath, "utf-8");
const allCssRes = await import_less.default.render(antdLessContent, {
modifyVars: replacedVars,
paths: antdDirPaths,
javascriptEnabled: true
});
const result = await (0, import_postcss.default)([
filterAntdGlobalStylePlugin({ prefixCls })
]).process(allCssRes.css, {
from: void 0
});
const cssContent = result.css;
(0, import_fs_extra.outputFileSync)(targetCSSPath, cssContent);
import_logger.default.success(
`${import_chalk.default.blueBright.underline(targetCSSPath)} was generated!`
);
if (minifyCSS) {
const targetMinCSSPath = (0, import_path.join)(outputDir, `${fileName}.min.css`);
const minifiedCssContent = new import_clean_css.default().minify(cssContent).styles;
(0, import_fs_extra.outputFileSync)(targetMinCSSPath, minifiedCssContent);
import_logger.default.info(
`${import_chalk.default.blueBright.underline(targetMinCSSPath)} was generated.`
);
}
} catch (error) {
import_logger.default.error(error);
process.exit(1);
}
}
async function generate(config) {
const { outputDir: _outputDir, themes: _themes } = config;
if (!Array.isArray(_themes))
return;
const outputDir = _outputDir || (0, import_path.join)(process.cwd(), import_constants.DEFAULT_OUTPUT_DIR);
const themes = uniqueThemeConfigs(_themes);
(0, import_fs_extra.emptyDirSync)(outputDir);
for (const themeConfig of themes) {
await outputThemeFile(themeConfig, config);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
filterAntdGlobalStylePlugin,
outputThemeFile,
uniqueThemeConfigs
});