UNPKG

@lcap/builder

Version:
170 lines (169 loc) 7.46 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.buildTheme = exports.viteBuildTheme = void 0; const vite_1 = require("vite"); const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const gen_theme_config_1 = __importDefault(require("./gens/gen-theme-config")); const input_paths_1 = require("../constants/input-paths"); const lcap_1 = require("../utils/lcap"); const logger_1 = __importDefault(require("../utils/logger")); const LIVE_RELOAD = { html: `<script type="text/javascript"> if ('WebSocket' in window) { (function() { var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; var address = protocol + window.location.host + window.location.pathname + '/ws'; var socket = new WebSocket(address); socket.onmessage = function(msg) { if (msg.data === 'nasl.theme') window.location.reload(); }; console.log('Live reload enabled.'); })(); } </script>`, tag: '<!-- Code injected by live-server -->', }; function viteBuildTheme(themeConfig, options, watch) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const loadResult = yield (0, vite_1.loadConfigFromFile)({ command: 'build', mode: 'production' }); if (!loadResult || !loadResult.config) { logger_1.default.error('未找到 vite 配置文件'); return; } const { config } = loadResult; let entry; if (config.build && config.build.lib) { entry = config.build.lib.entry; delete config.build.lib; } config.plugins = (_a = config.plugins) === null || _a === void 0 ? void 0 : _a.flat(); const index = (_b = config.plugins) === null || _b === void 0 ? void 0 : _b.findIndex((p) => p && p.name === 'vite:lcap-build'); if (index && index !== -1) { (_c = config.plugins) === null || _c === void 0 ? void 0 : _c.splice(index, 1); } (_d = config.plugins) === null || _d === void 0 ? void 0 : _d.push({ name: 'vite:lcap-theme-html', transformIndexHtml(html, ctx) { return __awaiter(this, void 0, void 0, function* () { html = html.replace('\'[THEME INFO HERE]\'', JSON.stringify({ previewPages: themeConfig.previewPages, components: themeConfig.components.filter((c) => !c.hidden).map((c) => ({ name: c.name, title: c.title, group: c.group })), })).replace(LIVE_RELOAD.tag, watch ? LIVE_RELOAD.html : ''); return html; }); }, }); if (!config.build) { config.build = {}; } if (!config.build.outDir) { config.build.outDir = 'dist-theme'; } config.build.outDir = `${config.build.outDir}/theme`; config.build.rollupOptions = Object.assign(Object.assign({}, (config.build.rollupOptions || {})), { input: input_paths_1.themePath, external: [] }); if (!config.resolve) { config.resolve = {}; } if (options.framework && options.framework.startsWith('vue')) { const vueAlias = options.framework === 'vue3' ? 'vue/dist/vue.esm-bundler.js' : 'vue/dist/vue.esm.js'; if (!Array.isArray(config.resolve.alias)) { config.resolve.alias = Object.assign(Object.assign({}, config.resolve.alias), { vue: vueAlias }); } else { config.resolve.alias.push({ find: 'vue', replacement: vueAlias, }); } } config.build.sourcemap = false; config.build.minify = 'esbuild'; config.build.emptyOutDir = false; config.publicDir = ''; config.build.assetsDir = ''; config.base = './'; yield (0, vite_1.build)(Object.assign(Object.assign({ configFile: false, envFile: false }, config), { mode: watch ? 'staging' : 'production' })); }); } exports.viteBuildTheme = viteBuildTheme; const Groups = [ 'Container', 'Layout', 'Navigation', 'Display', 'Table', 'Form', 'Selector', 'chart', 'Chart', 'Basic', 'Advanced', 'Feedback', 'Effects', 'Process', ]; const getGroupIndex = (n) => { const i = Groups.indexOf(n); return i === -1 ? 20 : i; }; function getComponentList(options) { const components = (0, lcap_1.getComponentMetaInfos)(options.rootPath, true); const list = components.map(({ name, kebabName, group, title = '', children = [], show, }) => { return { name: options.framework.startsWith('vue') ? kebabName : name, group, title, children: children.map((child) => (options.framework.startsWith('vue') ? child.kebabName : child.name)), show, }; }); if (options.type === 'extension') { return list; } return list.sort((a, b) => (getGroupIndex(a.group) - getGroupIndex(b.group))); } function buildTheme(options, mode = 'production') { return __awaiter(this, void 0, void 0, function* () { const components = yield getComponentList(options); // 如果组件列表为空,则不构建 theme if (components.length === 0) { return; } const themeConfig = (0, gen_theme_config_1.default)({ components: components, themeVarCssPath: options.theme.themeVarCssPath || '', themeComponentFolder: options.theme.themeComponentFolder || '', previewPages: options.theme.previewPages || [], oldCssVarPath: options.theme.oldCssVarPath, findThemeType: options.theme.findThemeType, type: options.type, dependencies: options.dependencies, }, options.framework); if (!themeConfig || !themeConfig.components || themeConfig.components.length === 0) { return; } logger_1.default.start('开始构建 theme'); yield fs_extra_1.default.writeJSON(path_1.default.join(options.destDir, 'theme.config.json'), themeConfig, { spaces: 2 }); logger_1.default.success('生成 theme.config.json 成功!'); // 非 staging 模式下,才进行 vite theme 构建 if (mode !== 'staging') { yield viteBuildTheme(themeConfig, options, mode === 'watch'); } logger_1.default.success('构建theme 成功'); }); } exports.buildTheme = buildTheme;