@wbi/cli-service
Version:
local service for wb-cli projects
83 lines (74 loc) • 2.59 kB
JavaScript
/**
* Created by jerry.lin-wun on 2019/2/18.
* DllPlugin:生成动态链接库的配置
*/
const path = require('path')
const webpack = require('webpack')
const rm = require('rimraf')
const chalk = require('chalk') // 命令行窗口颜色美化
const ProgressBarPlugin = require('progress-bar-webpack-plugin') // 显示构建进度百分比
const utils = require('./utils')
const dllPath = utils.resolve('./dll')
const webpackConfig = {
mode: 'production',
entry: {
vendor: utils.wbConfig.dll
},
output: {
// 输出的动态链接库的文件名称,[name] 代表当前动态链接库的名称,
// 也就是 entry 中配置的 vendor
filename: '[name].dll.js',
// 存放动态链接库的文件夹
path: dllPath,
// 存放动态链接库的全局变量名称,例如对应 vendor 来说就是 _dll_vendor
// 之所以在前面加上 _dll_ 是为了防止全局变量冲突
library: '_dll_[name]'
},
plugins: [
// 显示构建进度条
new ProgressBarPlugin({
format: ' ' + chalk.cyan.bold('Build dll') + ' [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
clear: false
}),
// 将jQuery设置成全局
// 这有利于其他第三方库依赖jQuery时的调用
/*new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery'
}),*/
// 接入 DllPlugin
new webpack.DllPlugin({
// 动态链接库的全局变量名称,需要和 output.library 中保持一致
// 该字段的值也就是输出的 manifest.json 文件 中 name 字段的值
// 例如 vendor.manifest.json 中就有 "name": "_dll_vendor"
name: '_dll_[name]',
// 描述动态链接库的 manifest.json 文件输出时的文件名称
path: path.join(dllPath, '[name].manifest.json')
// context: dllPath
})
]
}
module.exports = function () {
rm(dllPath, err => {
if (err) throw err
webpack(webpackConfig, function (err, stats) {
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
if (stats.hasErrors()) {
console.log(chalk.red(' Build dll failed with errors.\n'))
process.exit(1)
}
console.log(chalk.cyan(' Build dll complete.\n'))
console.log(chalk.yellow(
' Tip: built files will be used to run in webpack.\n' +
' It is not rebuilt when the vendor dependent version remains unchanged.\n'
))
})
})
}