UNPKG

ko

Version:
85 lines (84 loc) 2.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const webpack_1 = __importDefault(require("webpack")); const webpack_2 = __importDefault(require("../webpack")); const factory_1 = require("./factory"); const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin")); const esbuild_loader_1 = require("esbuild-loader"); class Build extends factory_1.ActionFactory { constructor(service) { super(service); } async generateConfig() { this.webpackConfig = new webpack_2.default(this.service); const extraConfig = { optimization: { minimize: true, minimizer: [ new terser_webpack_plugin_1.default({ terserOptions: { compress: { ...this.service.config.experiment?.compress, }, }, }), ...(this.service.config.experiment?.minimizer ? [ new esbuild_loader_1.ESBuildMinifyPlugin({ target: 'es2015', css: true, }), ] : ['...']), ], }, }; const ret = this.webpackConfig.merge(extraConfig); const plugins = await this.service.apply({ key: this.service.hookKeySet.WEBPACK_PLUGIN, context: ret.plugins, }); ret.plugins = plugins; await this.service.apply({ key: this.service.hookKeySet.MODIFY_WEBPACK, context: ret, }); return ret; } registerCommand() { const cmdName = 'build'; this.service.commander.registerCommand({ name: cmdName, description: 'build project', options: [ { flags: '--hash', description: 'output file name with hash', }, ], }); this.service.commander.bindAction(cmdName, this.action.bind(this)); } async action(cliOpts) { process.title = 'ko-build'; process.env.NODE_ENV = 'production'; this.service.freezeCliOptsWith(cliOpts); const config = await this.generateConfig(); (0, webpack_1.default)(config, (error, stats) => { if (stats && stats.hasErrors()) { throw stats.toString({ logging: 'error', colors: true, }); } if (error) { throw error; } this.successStdout('ko build completed!'); }); } } exports.default = Build;