@lcap/builder
Version:
lcap builder utils
175 lines (174 loc) • 7.4 kB
JavaScript
;
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.buildIDE = exports.viteBuildIde = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const lodash_1 = require("lodash");
const vite_1 = require("vite");
const child_process_1 = require("child_process");
const logger_1 = __importDefault(require("../utils/logger"));
function viteBuildIde(options, rootPath, watch, send) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const pkg = yield fs_extra_1.default.readJSON(path_1.default.join(rootPath, 'package.json'));
let buildConfig = {
define: {
'process.env': {
NODE_ENV: 'production',
},
},
build: {
target: ['es2020', 'edge88', 'firefox78', 'chrome56', 'safari14'],
lib: {
entry: options.entry || '',
formats: ['umd'],
name: `$ideMaterial${(0, lodash_1.upperFirst)((0, lodash_1.camelCase)(pkg.name))}`,
fileName: (format, entryName) => {
switch (format) {
case 'es':
return `${entryName}.mjs`;
case 'cjs':
return `${entryName}.cjs`;
default:
return `${entryName}.js`;
}
},
},
rollupOptions: {
output: {
assetFileNames: (assetInfo) => {
if (assetInfo.name === 'style.css') {
return 'index.css';
}
return '[name][extname]';
},
interop: 'compat',
},
},
outDir: options.outDir,
},
};
if (options.configFile && !options.configFile.startsWith('vite.config')) {
const loadResult = yield (0, vite_1.loadConfigFromFile)({ command: 'build', mode: 'production' }, options.configFile, rootPath);
if (loadResult && loadResult.config) {
buildConfig = (0, vite_1.mergeConfig)(loadResult.config, buildConfig);
}
}
if ((_b = (_a = buildConfig.build) === null || _a === void 0 ? void 0 : _a.rollupOptions) === null || _b === void 0 ? void 0 : _b.external) {
delete buildConfig.build.rollupOptions.external;
}
if (watch) {
if (!buildConfig.build) {
buildConfig.build = {};
}
buildConfig.build.watch = {};
}
if (watch && (0, lodash_1.isFunction)(send)) {
if (!buildConfig.plugins) {
buildConfig.plugins = [];
}
buildConfig.plugins.push({
name: 'vite:lcap:socket',
closeBundle() {
return __awaiter(this, void 0, void 0, function* () {
send('update.ide');
});
},
});
}
yield (0, vite_1.build)(Object.assign(Object.assign({ configFile: false, envFile: false }, buildConfig), { mode: watch ? 'staging' : 'production' }));
});
}
exports.viteBuildIde = viteBuildIde;
const isExistEntry = (entry, rootPath) => {
if (!entry) {
return false;
}
if (entry.indexOf('.') !== -1) {
return fs_extra_1.default.existsSync(path_1.default.resolve(rootPath, entry));
}
return [
'.js',
'.jsx',
'.tsx',
'.ts',
].findIndex((ext) => {
return fs_extra_1.default.existsSync(path_1.default.resolve(rootPath, `${entry}${ext}`));
}) !== -1;
};
const settersFolder = 'setters';
function buildSetters(options) {
var _a;
if (!((_a = options.ide) === null || _a === void 0 ? void 0 : _a.setters)) {
return;
}
const { rootPath, entries } = options.ide.setters;
if (!fs_extra_1.default.existsSync(path_1.default.resolve(rootPath))) {
logger_1.default.warn(`[builder] setters root path ${rootPath} not exists`);
return;
}
const setters = {};
Object.entries(entries).forEach(([key, entry]) => {
if (fs_extra_1.default.existsSync(path_1.default.resolve(rootPath, entry))) {
setters[key] = entry;
}
else {
logger_1.default.warn(`[builder] setters entry ${entry} not exists`);
}
});
if (Object.keys(setters).length === 0) {
logger_1.default.warn('[builder] setters entries is empty');
return;
}
fs_extra_1.default.writeFileSync(path_1.default.resolve(rootPath, 'setters.json'), JSON.stringify(setters, null, 2));
(0, child_process_1.execSync)('npm run build', {
cwd: rootPath,
});
// 拷贝 setters 到 dist 目录
const targetPath = path_1.default.resolve(rootPath, 'dist');
const destPath = path_1.default.resolve(options.rootPath, options.destDir, settersFolder);
fs_extra_1.default.copySync(targetPath, destPath);
const setterList = Object.keys(setters).map((key) => {
const setter = {
name: key,
js: `${options.destDir}/${settersFolder}/${key}.js`,
};
const cssPath = `${options.destDir}/${settersFolder}/${key}.css`;
if (fs_extra_1.default.existsSync(path_1.default.resolve(options.rootPath, cssPath))) {
setter.css = cssPath;
}
return setter;
});
fs_extra_1.default.writeFileSync(path_1.default.resolve(options.rootPath, options.destDir, 'setters.json'), JSON.stringify(setterList, null, 2));
}
function buildIDE(options, watch = false, send) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if ((_a = options.ide) === null || _a === void 0 ? void 0 : _a.setters) {
yield buildSetters(options);
}
const DEFUALT_IDE_OPTIONS = {
entry: 'ide/index',
outDir: `${options.destDir}/ide`,
};
// eslint-disable-next-line prefer-object-spread
const ideOptions = Object.assign(DEFUALT_IDE_OPTIONS, options.ide);
if (!isExistEntry(ideOptions.entry, options.rootPath)) {
return;
}
yield viteBuildIde(ideOptions, options.rootPath, watch, send);
});
}
exports.buildIDE = buildIDE;