From 8f809f823539a840b93e900c655a9c6c1b680c1f Mon Sep 17 00:00:00 2001 From: caoenze Date: Thu, 18 Jul 2024 15:57:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89terser-webpa?= =?UTF-8?q?ck-plugin=E9=85=8D=E7=BD=AE=20=E9=81=BF=E5=85=8Dbuild=E4=BA=A7?= =?UTF-8?q?=E7=89=A9=E5=87=BA=E7=8E=B0=E9=A2=9D=E5=A4=96=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=BA=A7=E7=89=A9;=20feat:=20=E6=96=B0=E5=A2=9Ebuild=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E6=B8=85=E9=99=A4dist=E7=9B=AE=E5=BD=95=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=20buildOptions.cleanDist;=20feat:=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Edev=E6=97=B6=20=E8=B0=83=E8=AF=95=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=20=E5=BC=80=E5=85=B3,dev=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E6=97=B6=E5=8F=AF=E4=B8=8D=E5=90=AF=E7=94=A8=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E6=9C=8D=E5=8A=A1;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli-utils/src/config.ts | 3 +- packages/plugin-build/package.json | 3 ++ packages/plugin-build/src/compiler.ts | 40 ++++++++++++------- .../plugin-build/src/config/hummer.config.ts | 11 ++++- .../src/config/tenon-react.config.ts | 11 ++++- .../plugin-build/src/config/tenon.config.ts | 11 ++++- packages/plugin-build/src/index.ts | 2 +- 7 files changed, 61 insertions(+), 20 deletions(-) diff --git a/packages/cli-utils/src/config.ts b/packages/cli-utils/src/config.ts index 49b4c7d..b83ea31 100644 --- a/packages/cli-utils/src/config.ts +++ b/packages/cli-utils/src/config.ts @@ -8,10 +8,11 @@ export enum ProjectType { } export interface IDevTool { - web?:boolean + web?:boolean // 是否打开预览网页 qrCode?:boolean devServerPort?: number webServerPort?: number + enableServer?: boolean // 是否开启调试服务(webserver devserver) } export interface ProjectConfig { diff --git a/packages/plugin-build/package.json b/packages/plugin-build/package.json index 6489582..10d3705 100644 --- a/packages/plugin-build/package.json +++ b/packages/plugin-build/package.json @@ -68,6 +68,9 @@ "webpack-plugin-jscc": "0.0.4", "ws": "^7.3.1" }, + "peerDependencies": { + "terser-webpack-plugin": "^5.3.7" + }, "devDependencies": { "@types/archiver": "^5.1.0", "@types/fs-extra": "^9.0.13", diff --git a/packages/plugin-build/src/compiler.ts b/packages/plugin-build/src/compiler.ts index e610783..a03b959 100644 --- a/packages/plugin-build/src/compiler.ts +++ b/packages/plugin-build/src/compiler.ts @@ -11,16 +11,29 @@ const qrcode = require('qrcode-terminal') export class Compiler { webpackConfig: any webConfig: any - private devTool: IDevTool = { web: true, qrCode: false } + buildOptions: any + private devTool: IDevTool = {} initConfig(config: any) { this.webpackConfig = config.webpackConfig this.webConfig = config.webConfig - this.devTool = config.devTool + this.devTool = { + web: true, + qrCode: false, + enableServer: true, + ...(config.devTool || {}) + } + this.buildOptions = { + cleanDist: true, + ...(config.buildOptions || {}) + } } build() { const rootDir = this.webpackConfig?.output?.path ?? path.join(process.cwd(), 'dist'); - fse.removeSync(rootDir); + // build默认移除dist目录逻辑 + if (this.buildOptions.cleanDist) { + fse.removeSync(rootDir); + } return new Promise((resolve, reject) => { // TODO build增加压缩 webpack({ @@ -44,10 +57,16 @@ export class Compiler { let { port, host } = await getServerConfig(); // 优先使用项目配置中的端口 port = this.devTool.devServerPort || port; + // TODO: openWeb确定openWeb配置 作用 if (this.webConfig?.openWeb === 'all') { - var webServer = new WebServer(host, port, rootDir, this.devTool); - var devServer = new DevServer(host, port, rootDir, webServer, this.devTool); - this.startWatchServer({ host, port, rootDir }, devServer); + if (this.devTool?.enableServer) { + // web模拟器 服务 + var webServer = new WebServer(host, port, rootDir, this.devTool); + // 调试服务 + var devServer = new DevServer(host, port, rootDir, webServer, this.devTool); + this.startWatchServer({ host, port, rootDir }, devServer); + } + this.buildWatch((stats: Stats) => { this.printStats(stats); }) @@ -83,15 +102,6 @@ export class Compiler { entrypoints: false }) + '\n\n' ) - // let output = stats?.toString({ - // colors: true, - // modules: false, - // children: false, - // chunks: false, - // chunkModules: false, - // entrypoints: false - // }) - // console.log(output) } startWatchServer({ host, port, rootDir }: any, devServer: any) { diff --git a/packages/plugin-build/src/config/hummer.config.ts b/packages/plugin-build/src/config/hummer.config.ts index fda2b6e..94bf5e9 100644 --- a/packages/plugin-build/src/config/hummer.config.ts +++ b/packages/plugin-build/src/config/hummer.config.ts @@ -4,6 +4,7 @@ import JsccPlugin from 'webpack-plugin-jscc' import { ProjectConfig } from '@hummer/cli-utils' import { BuildPlugin } from '../index' import { pathExistsSync } from 'fs-extra' +import TerserPlugin from 'terser-webpack-plugin' import path from 'path' const exec = require('child_process').execSync @@ -131,6 +132,14 @@ export default function getDefaultHummerConfiguration(isProduction: boolean, hmC "HUMMER_COMPILE_TYPE": JSON.stringify('HUMMER') // 注入编译类型 }), ...plugins - ] + ], + optimization: { + minimize: isProduction, + minimizer: [ + new TerserPlugin({ + extractComments: false,//不将注释提取到单独的文件中 + }), + ], + } } } \ No newline at end of file diff --git a/packages/plugin-build/src/config/tenon-react.config.ts b/packages/plugin-build/src/config/tenon-react.config.ts index 1a890b2..cf73a52 100644 --- a/packages/plugin-build/src/config/tenon-react.config.ts +++ b/packages/plugin-build/src/config/tenon-react.config.ts @@ -5,6 +5,7 @@ import {ProjectConfig} from '@hummer/cli-utils' import {getAssetsAddress} from '../utils/server' import * as path from 'path' import { BuildPlugin } from '..' +import TerserPlugin from 'terser-webpack-plugin' export default function getTenonReactConfiguration(isProduction: boolean, hmConfig:ProjectConfig, context: BuildPlugin): Configuration { let plugins:any = [] @@ -105,6 +106,14 @@ export default function getTenonReactConfiguration(isProduction: boolean, hmConf "HUMMER_COMPILE_TYPE": JSON.stringify('TENON_REACT') // 注入编译类型 }), ...plugins - ] + ], + optimization: { + minimize: isProduction, + minimizer: [ + new TerserPlugin({ + extractComments: false,//不将注释提取到单独的文件中 + }), + ], + } } } diff --git a/packages/plugin-build/src/config/tenon.config.ts b/packages/plugin-build/src/config/tenon.config.ts index 35710be..f7778c8 100644 --- a/packages/plugin-build/src/config/tenon.config.ts +++ b/packages/plugin-build/src/config/tenon.config.ts @@ -6,6 +6,7 @@ import { ProjectConfig } from '@hummer/cli-utils' import * as path from 'path' import { BuildPlugin } from '..' import { VueFileMapper, VueHandling } from './vue/vueFileMapper' +import TerserPlugin from 'terser-webpack-plugin' interface ModuleFilenameTemplateInfo { @@ -158,6 +159,14 @@ export default function getDefaultTenonConfiguration(isProduction: boolean, hmCo "HUMMER_COMPILE_TYPE": JSON.stringify('TENON_VUE') // 注入编译类型 }), ...plugins - ] + ], + optimization: { + minimize: isProduction, + minimizer: [ + new TerserPlugin({ + extractComments: false,//不将注释提取到单独的文件中 + }), + ], + } } } diff --git a/packages/plugin-build/src/index.ts b/packages/plugin-build/src/index.ts index 3ee737b..2ae1901 100644 --- a/packages/plugin-build/src/index.ts +++ b/packages/plugin-build/src/index.ts @@ -107,7 +107,7 @@ export class BuildPlugin extends Plugin { // 1. Read Project Config this.projectConfig = await getProjectConfig(Webpack, this.options); if (!this.projectConfig) { - error('hm.config.js 文件不规范,请检查!') + error('hm.config.js 文件不规范或当前执行路径下不存在hummer配置文件,请检查!') process.exit(); }