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

128 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2025-03-09 11:34: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 { name } = require('../package.json');
// const Visualizer = require('webpack-visualizer-plugin');
let { patch } = configJSON;
let { project, branch, provider } = patch || {};
module.exports = function(env, argv) {
let {
mode,
hash,
client,
fse,
buildPath,
outputPath,
srcDir = configJSON.srcDir || 'src',
patchList
} = env;
if (client === 'mobile') {
process.env.PROJECT_CLIENT = 'mobile';
}
let buildWithoutHTML = configJSON.buildWithoutHTML;
buildWithoutHTML && typeof buildWithoutHTML === 'string' && (buildWithoutHTML = [buildWithoutHTML]);
buildPath = buildPath || configJSON.buildEntryPath || `./${srcDir}/*/*/*/*/index.js`;
let prodConfig = {
mode: 'production',
entry: {},
output: {
path: path.resolve(__dirname, `../${outputPath || 'dist'}`),
publicPath: '../../../../',
library: '[name]',
libraryTarget: 'umd',
chunkFilename: '[name].js',
jsonpFunction: `webpackJsonp_${name}`,
globalObject: 'window',
},
devtool: false,
plugins: [
// 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].[contenthash:8].js' : '[name].js';
// 获取入口
let { entries, plugins, externals, rules } = buildEntry({ buildPath, buildWithoutHTML, hash, client, mode, fse, srcDir, patchList });
Object.assign(common.externals, externals);
//css隔离
if(rules.length){
Object.assign(common.module.rules, rules);
}
Object.assign(prodConfig.entry, entries);
prodConfig.plugins.push(...plugins);
prodConfig = merge(common, prodConfig);
return prodConfig;
};