ant-design/webpack.config.js

93 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2018-11-28 15:00:03 +08:00
/* eslint no-param-reassign: 0 */
2016-10-15 17:04:30 +08:00
// This config is for building dist files
const getWebpackConfig = require('@ant-design/tools/lib/getWebpackConfig');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { codecovWebpackPlugin } = require('@codecov/webpack-plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const DuplicatePackageCheckerPlugin = require('@madccc/duplicate-package-checker-webpack-plugin');
const path = require('path');
2021-09-01 10:56:50 +08:00
function addLocales(config) {
const newConfig = { ...config }; // Avoid mutating the original config
2017-02-17 09:36:42 +08:00
let packageName = 'antd-with-locales';
if (newConfig.entry['antd.min']) {
2017-02-17 09:36:42 +08:00
packageName += '.min';
}
newConfig.entry[packageName] = './index-with-locales.js';
newConfig.output.filename = '[name].js';
return newConfig;
2017-02-17 09:36:42 +08:00
}
2022-03-30 15:55:40 +08:00
function externalDayjs(config) {
const newConfig = { ...config }; // Shallow copy for safety
newConfig.externals.dayjs = {
2022-03-30 15:55:40 +08:00
root: 'dayjs',
commonjs2: 'dayjs',
commonjs: 'dayjs',
amd: 'dayjs',
};
return newConfig;
2022-03-30 15:55:40 +08:00
}
function externalCssinjs(config) {
const newConfig = { ...config }; // Shallow copy for safety
newConfig.resolve = newConfig.resolve || {};
newConfig.resolve.alias = newConfig.resolve.alias || {};
newConfig.resolve.alias['@ant-design/cssinjs'] = path.resolve(__dirname, 'alias/cssinjs');
return newConfig;
}
function addPluginsForProduction(config) {
const newConfig = { ...config }; // Shallow copy for safety
if (!process.env.CI || process.env.ANALYZER) {
newConfig.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: '../report.html',
})
);
}
if (newConfig.mode === 'production' && !process.env.PRODUCTION_ONLY) {
newConfig.plugins.push(
new DuplicatePackageCheckerPlugin({
verbose: true,
emitError: true,
})
);
}
newConfig.plugins.push(
codecovWebpackPlugin({
enableBundleAnalysis: process.env.CODECOV_TOKEN !== undefined,
bundleName: 'antd.min',
uploadToken: process.env.CODECOV_TOKEN,
}),
new CircularDependencyPlugin({
failOnError: true,
})
);
return newConfig;
}
let webpackConfig = getWebpackConfig(false);
if (process.env.PRODUCTION_ONLY) {
console.log('🍐 Build production only');
webpackConfig = webpackConfig.filter((config) => config.mode === 'production');
}
if (process.env.RUN_ENV === 'PRODUCTION') {
webpackConfig = webpackConfig.map((config) => {
let newConfig = addLocales(config);
newConfig = externalDayjs(newConfig);
newConfig = externalCssinjs(newConfig);
newConfig.optimization.usedExports = true;
newConfig = addPluginsForProduction(newConfig);
return newConfig;
});
2017-10-19 19:06:16 +08:00
}
module.exports = [...webpackConfig];