Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
110 lines (98 loc) · 3.47 KB
/
Copy pathwebpack.config.js
File metadata and controls
110 lines (98 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint no-param-reassign: 0 */
// This config is for building dist files
const { getWebpackConfig } = require('@ant-design/tools');
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('node:path');
function addLocales(config) {
const newConfig = { ...config }; // Avoid mutating the original config
let packageName = 'antd-with-locales';
if (newConfig.entry['antd.min']) {
packageName += '.min';
}
newConfig.entry[packageName] = './index-with-locales.ts';
newConfig.output.filename = '[name].js';
return newConfig;
}
function externalDayjs(config) {
const newConfig = { ...config }; // Shallow copy for safety
newConfig.externals.dayjs = {
root: 'dayjs',
commonjs2: 'dayjs',
commonjs: 'dayjs',
amd: 'dayjs',
};
return newConfig;
}
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,
gitService: 'github',
}),
new CircularDependencyPlugin({
failOnError: true,
}),
);
return newConfig;
}
let webpackConfig = getWebpackConfig(false, {
enabledReactCompiler: false,
});
const commonJsEntries = [
'index.ts',
'index-with-locales.ts',
'index-style-only.ts',
'alias/cssinjs.ts',
].map((entry) => path.resolve(__dirname, entry));
// Babel already strips TypeScript. Avoid ts-loader adding "use strict" to CommonJS entries, which changes module concatenation and increases the bundle size.
webpackConfig.forEach((config) => {
const jsRule = config.module.rules.find((rule) => rule.test?.test('index.js'));
const tsRule = config.module.rules.find((rule) => rule.test?.test('index.ts'));
jsRule.test = (filePath) => /\.jsx?$/.test(filePath) || commonJsEntries.includes(filePath);
tsRule.exclude = commonJsEntries;
});
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;
});
}
module.exports = [...webpackConfig];