Skip to content

Commit

Permalink
feat: 自定义terser-webpack-plugin配置 避免build产物出现额外注释产物;
Browse files Browse the repository at this point in the history
feat: 新增build时不清除dist目录配置 buildOptions.cleanDist;
feat: 新增dev时 调试预览服务 开关,dev构建时可不启用调试预览服务;
  • Loading branch information
AdamCaoQAQ committed Jul 18, 2024
1 parent bc0f428 commit 8f809f8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/cli-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 25 additions & 15 deletions packages/plugin-build/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve, reject) => {
// TODO build增加压缩
webpack({
Expand All @@ -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);
})
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion packages/plugin-build/src/config/hummer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,//不将注释提取到单独的文件中
}),
],
}
}
}
11 changes: 10 additions & 1 deletion packages/plugin-build/src/config/tenon-react.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,//不将注释提取到单独的文件中
}),
],
}
}
}
11 changes: 10 additions & 1 deletion packages/plugin-build/src/config/tenon.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,//不将注释提取到单独的文件中
}),
],
}
}
}
2 changes: 1 addition & 1 deletion packages/plugin-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 8f809f8

Please # to comment.