UNPKG

ng-alain-plugin-theme

Version:
144 lines (143 loc) 6.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildThemeCSS = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const less_1 = __importDefault(require("less")); const LessPluginCleanCSS = require('less-plugin-clean-css'); const utils_1 = require("./utils"); const root = process.cwd(); let node_modulesPath = ''; function fixConfig(config) { var _a; let styleSourceRoot = 'src'; if (config.name) { const angularJsonPath = (0, path_1.join)(root, 'angular.json'); const sourceRoot = (_a = (0, utils_1.getJSON)(angularJsonPath)) === null || _a === void 0 ? void 0 : _a.projects[config.name].sourceRoot; if (sourceRoot != null) { styleSourceRoot = sourceRoot; } } config = (0, utils_1.deepMergeKey)({ additionalLibraries: [], additionalThemeVars: [], list: [], min: true, projectStylePath: (0, utils_1.mergePath)(styleSourceRoot, 'styles.less'), }, true, config); const list = []; config.list.forEach(item => { if (!item.theme && !item.modifyVars) { return; } if (!item.key) { item.key = item.theme || 'invalid-key'; } if (!item.filePath) { item.filePath = (0, utils_1.mergePath)(styleSourceRoot, `assets/style.${item.key || 'invalid-name'}.css`); } list.push(Object.assign({ projectThemeVar: [] }, item)); }); if (list.length === 0) { throw new Error(`Not found valid theme configuration`); } config.list = list; return config; } function genStripVar(item) { const modifyVars = item.modifyVars || {}; const stripPrefixOfModifyVars = {}; Object.keys(modifyVars).forEach(key => { const newKey = key.startsWith('@') ? key.substring(1) : key; stripPrefixOfModifyVars[newKey] = modifyVars[key]; }); return stripPrefixOfModifyVars; } function buildCss(options, config) { return __awaiter(this, void 0, void 0, function* () { const plugins = []; if (options.min === true) { plugins.push(new LessPluginCleanCSS({ advanced: true })); } return less_1.default .render(options.content, Object.assign(Object.assign({ plugins, paths: [ (0, path_1.join)(root, 'node_modules/ng-zorro-antd/style/color'), (0, path_1.join)(root, 'node_modules/@delon/theme/system/mixins'), (0, path_1.join)(root, 'node_modules'), ] }, config.buildLessOptions), { modifyVars: Object.assign({}, options.modifyVars) })) .then(res => res.css); }); } function genThemeLess(type, extraThemeVars) { const list = []; if ((0, fs_1.existsSync)((0, path_1.join)(root, node_modulesPath, 'ng-zorro-antd/style'))) { list.push(`@import 'ng-zorro-antd/style/themes/${type}.less';`); } const delonPath = (0, path_1.join)(root, node_modulesPath, '@delon'); if ((0, fs_1.existsSync)((0, path_1.join)(delonPath, 'theme'))) { list.push(`@import '@delon/theme/system/theme-${type}.less';`); list.push(`@import '@delon/theme/layout-default/style/theme-${type}.less';`); list.push(`@import '@delon/theme/layout-blank/style/theme-${type}.less';`); } ['abc', 'chart'].forEach(libName => { const libThemePath = (0, path_1.join)(delonPath, libName, `theme-${type}.less`); if ((0, fs_1.existsSync)(libThemePath)) { list.push(`@import '@delon/${libName}/theme-${type}.less';`); } }); if (Array.isArray(extraThemeVars) && extraThemeVars.length > 0) { list.push(...extraThemeVars.map(path => { const lessFilePath = (0, path_1.join)(root, path.replace(`#NAME#`, type)); if (!(0, fs_1.existsSync)(lessFilePath)) { return ''; } return `@import '${lessFilePath}';`; })); } return list; } function buildThemeCSS(config) { return __awaiter(this, void 0, void 0, function* () { var _a; node_modulesPath = config.nodeModulesPath || 'node_modules'; config = fixConfig(config); const promises = (_a = config.list) === null || _a === void 0 ? void 0 : _a.map(item => { var _a; const content = [ `@import '${config.projectStylePath}';`, ...config.additionalLibraries.map(v => `@import '${v}';`), ...genThemeLess(item.theme || 'default', (_a = config.additionalThemeVars) !== null && _a !== void 0 ? _a : []), ].join(''); const options = { min: config.min, content, modifyVars: genStripVar(item), }; if ((0, fs_1.existsSync)(item.filePath)) { (0, fs_1.unlinkSync)(item.filePath); } return buildCss(options, config) .then(css => { (0, fs_1.writeFileSync)(item.filePath, css); console.log(`✅ Style '${item.key}' generated successfully. Output: ${item.filePath}`); }) .catch(ex => { console.error(`❌ Style '${item.key}' generation failed. ${ex.message}`); }); }); yield Promise.all(promises); }); } exports.buildThemeCSS = buildThemeCSS;