ncpub-multipage-demo-2005/config/webpack.prod.config.js

111 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2024-09-04 09:11:19 +08:00
/*
* @Author: liyxt
* @Date: 2019-07-02 10:02:16
* @LastEditors: liyxt
* @LastEditTime: 2020-04-14 14:31:30
* @Description: file content
*/
/**
* 生产环境配置
*/
const webpack = require('webpack');
const common = require('./webpack.common');
const path = require('path');
const merge = require('webpack-merge');
const configJSON = require('../config.json');
const buildEntry = require('./buildEntry');
const TerserPlugin = require('terser-webpack-plugin');
const OutputSourcePlugin = require('./OutputSourcePlugin');
// const Visualizer = require('webpack-visualizer-plugin');
let { patch } = configJSON;
let { project, branch, provider } = patch || {};
let isPatch = process.env.npm_config_isPatch;
module.exports = function(env, argv) {
let { mode, hash, client, fse, buildPath, outputPath } = env;
if (client === 'mobile') {
process.env.PROJECT_CLIENT = 'mobile';
}
buildPath = buildPath || configJSON.buildEntryPath || './src/*/*/*/*/index.js';
let buildWithoutHTML = configJSON.buildWithoutHTML;
buildWithoutHTML && typeof buildWithoutHTML === 'string' && (buildWithoutHTML = [buildWithoutHTML]);
let prodConfig = {
mode: 'production',
entry: {},
output: {
path: path.resolve(__dirname, `../${outputPath || 'dist'}`),
publicPath: '../../../../',
library: '[name]',
libraryTarget: 'umd',
chunkFilename: '[name].js'
},
devtool: false,
plugins: [
// new FileListPlugin(),
// new ChunkListPlugin(),
new webpack.BannerPlugin({
banner:
'@ncctag ' +
JSON.stringify({
project,
branch,
provider,
date: new Date().toLocaleString()
}), // 其值为字符串,将作为注释存在
raw: false, // 如果值为 true将直出不会被作为注释
entryOnly: false // 如果值为 true将只在入口 chunks 文件中添加
}),
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(mode),
ISMA: configJSON.isMA,
LOGIN_INFO: JSON.stringify(configJSON.directConnectInfo),
MA_INFO: JSON.stringify(configJSON.maInfo)
}),
new OutputSourcePlugin({ output: '__SOURCE__CODE__' }),
// new Visualizer()
],
optimization: {
minimize: true, // 是否启用压缩
splitChunks: {
automaticNameDelimiter: '_'
},
minimizer: [
new TerserPlugin({
parallel: 4,
sourceMap: true,
extractComments: false,
terserOptions: {
// compress: {
// drop_console: true
// },
output: {
comments: /@ncctag/i
}
}
})
]
}
};
if (hash === 'false') {
hash = false;
} else if (hash === 'true') {
hash = true;
}
// test模式加source-map调试
mode === 'test' && (prodConfig.devtool = 'source-map');
// 节点加hash参照不加hash
prodConfig.output.filename = hash ? '[name].[hash:8].js' : '[name].js';
// 获取入口
let { entries, plugins, externals } = buildEntry({ buildPath, buildWithoutHTML, hash, client, mode, fse });
Object.assign(common.externals, externals);
Object.assign(prodConfig.entry, entries);
prodConfig.plugins.push(...plugins);
prodConfig = merge(common, prodConfig);
return prodConfig;
};