2018-03-18 19:59:38 +08:00
|
|
|
// This config is for building dist files
|
2022-03-12 09:56:32 +08:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const RemovePlugin = require('remove-files-webpack-plugin');
|
2021-09-25 16:51:32 +08:00
|
|
|
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
|
2022-03-12 09:56:32 +08:00
|
|
|
const { ESBuildMinifyPlugin } = require('esbuild-loader');
|
|
|
|
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
|
2020-03-15 13:12:47 +08:00
|
|
|
const getWebpackConfig = require('./antd-tools/getWebpackConfig');
|
2021-01-14 17:33:17 +08:00
|
|
|
const darkVars = require('./scripts/dark-vars');
|
2022-03-12 09:56:32 +08:00
|
|
|
const compactVars = require('./scripts/compact-vars');
|
|
|
|
function injectLessVariables(config, variables) {
|
|
|
|
(Array.isArray(config) ? config : [config]).forEach(conf => {
|
|
|
|
conf.module.rules.forEach(rule => {
|
|
|
|
// filter less rule
|
|
|
|
if (rule.test instanceof RegExp && rule.test.test('.less')) {
|
|
|
|
const lessRule = rule.use[rule.use.length - 1];
|
|
|
|
if (lessRule.options.lessOptions) {
|
|
|
|
lessRule.options.lessOptions.modifyVars = {
|
|
|
|
...lessRule.options.lessOptions.modifyVars,
|
|
|
|
...variables,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
lessRule.options.modifyVars = {
|
|
|
|
...lessRule.options.modifyVars,
|
|
|
|
...variables,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
2018-03-18 19:59:38 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function addLocales(webpackConfig) {
|
|
|
|
let packageName = 'antd-with-locales';
|
2018-03-18 19:59:38 +08:00
|
|
|
if (webpackConfig.entry['antd.min']) {
|
2019-01-12 11:33:27 +08:00
|
|
|
packageName += '.min';
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
webpackConfig.entry[packageName] = './index-with-locales.js';
|
|
|
|
webpackConfig.output.filename = '[name].js';
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
function externalDayjs(config) {
|
2022-08-07 15:10:53 +08:00
|
|
|
config.externals.push({
|
|
|
|
dayjs: {
|
|
|
|
root: 'dayjs',
|
|
|
|
commonjs2: 'dayjs',
|
|
|
|
commonjs: 'dayjs',
|
|
|
|
amd: 'dayjs',
|
2022-08-07 20:03:09 +08:00
|
|
|
module: 'dayjs',
|
2022-08-07 15:10:53 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
config.externals.push(function ({ _context, request }, callback) {
|
|
|
|
if (/^dayjs\/plugin\//.test(request)) {
|
2023-02-17 22:01:30 +08:00
|
|
|
const name = request.replace(/\//g, '_');
|
2022-08-07 15:10:53 +08:00
|
|
|
return callback(null, {
|
|
|
|
root: name,
|
|
|
|
commonjs2: name,
|
|
|
|
commonjs: name,
|
|
|
|
amd: name,
|
2022-08-07 20:03:09 +08:00
|
|
|
module: name,
|
2022-08-07 15:10:53 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
function injectWarningCondition(config) {
|
|
|
|
config.module.rules.forEach(rule => {
|
|
|
|
// Remove devWarning if needed
|
|
|
|
if (rule.test.test('test.tsx')) {
|
|
|
|
rule.use = [
|
|
|
|
...rule.use,
|
|
|
|
{
|
|
|
|
loader: 'string-replace-loader',
|
|
|
|
options: {
|
|
|
|
search: 'devWarning(',
|
|
|
|
replace: "if (process.env.NODE_ENV !== 'production') devWarning(",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
function processWebpackThemeConfig(themeConfig, theme, vars) {
|
|
|
|
themeConfig.forEach(config => {
|
|
|
|
externalDayjs(config);
|
|
|
|
|
|
|
|
// rename default entry to ${theme} entry
|
|
|
|
Object.keys(config.entry).forEach(entryName => {
|
2022-03-12 09:56:32 +08:00
|
|
|
const originPath = config.entry[entryName];
|
|
|
|
let replacedPath = [...originPath];
|
|
|
|
|
|
|
|
// We will replace `./index` to `./index-style-only` since theme dist only use style file
|
|
|
|
if (originPath.length === 1 && originPath[0] === './index') {
|
|
|
|
replacedPath = ['./index-style-only'];
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(chalk.red('🆘 Seems entry has changed! It should be `./index`'));
|
|
|
|
}
|
|
|
|
|
|
|
|
config.entry[entryName.replace('antd', `antd.${theme}`)] = replacedPath;
|
2021-09-25 16:51:32 +08:00
|
|
|
delete config.entry[entryName];
|
|
|
|
});
|
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
// apply ${theme} less variables
|
|
|
|
injectLessVariables(config, vars);
|
2021-09-25 16:51:32 +08:00
|
|
|
// ignore emit ${theme} entry js & js.map file
|
2022-03-12 09:56:32 +08:00
|
|
|
config.plugins.push(
|
|
|
|
new RemovePlugin({
|
|
|
|
after: {
|
|
|
|
root: './dist',
|
|
|
|
include: [
|
|
|
|
`antd.${theme}.js`,
|
|
|
|
`antd.${theme}.js.map`,
|
|
|
|
`antd.${theme}.min.js`,
|
|
|
|
`antd.${theme}.min.js.map`,
|
|
|
|
],
|
|
|
|
log: false,
|
|
|
|
logWarning: false,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
const legacyEntryVars = {
|
|
|
|
'root-entry-name': 'default',
|
|
|
|
};
|
|
|
|
const webpackConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
|
2022-07-02 21:02:48 +08:00
|
|
|
const webpackESMConfig = injectLessVariables(getWebpackConfig(false, true), legacyEntryVars);
|
2022-03-12 09:56:32 +08:00
|
|
|
const webpackDarkConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
|
|
|
|
const webpackCompactConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
|
|
|
|
const webpackVariableConfig = injectLessVariables(getWebpackConfig(false), {
|
|
|
|
'root-entry-name': 'variable',
|
|
|
|
});
|
|
|
|
|
|
|
|
webpackConfig.forEach(config => {
|
|
|
|
injectWarningCondition(config);
|
|
|
|
});
|
2021-01-14 17:33:17 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
if (process.env.RUN_ENV === 'PRODUCTION') {
|
|
|
|
webpackConfig.forEach(config => {
|
|
|
|
externalDayjs(config);
|
|
|
|
addLocales(config);
|
|
|
|
// Reduce non-minified dist files size
|
|
|
|
config.optimization.usedExports = true;
|
2022-03-12 09:56:32 +08:00
|
|
|
// use esbuild
|
|
|
|
if (process.env.ESBUILD || process.env.CSB_REPO) {
|
|
|
|
config.optimization.minimizer[0] = new ESBuildMinifyPlugin({
|
|
|
|
target: 'es2015',
|
|
|
|
css: true,
|
|
|
|
});
|
|
|
|
}
|
2021-09-25 16:51:32 +08:00
|
|
|
config.plugins.push(
|
|
|
|
new BundleAnalyzerPlugin({
|
|
|
|
analyzerMode: 'static',
|
|
|
|
openAnalyzer: false,
|
|
|
|
reportFilename: '../report.html',
|
|
|
|
}),
|
|
|
|
);
|
2022-03-12 09:56:32 +08:00
|
|
|
|
|
|
|
if (!process.env.NO_DUP_CHECK) {
|
|
|
|
config.plugins.push(
|
|
|
|
new DuplicatePackageCheckerPlugin({
|
|
|
|
verbose: true,
|
|
|
|
emitError: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2021-01-14 17:33:17 +08:00
|
|
|
});
|
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
processWebpackThemeConfig(webpackDarkConfig, 'dark', darkVars);
|
2022-03-12 09:56:32 +08:00
|
|
|
processWebpackThemeConfig(webpackCompactConfig, 'compact', compactVars);
|
|
|
|
processWebpackThemeConfig(webpackVariableConfig, 'variable', {});
|
2021-09-25 16:51:32 +08:00
|
|
|
}
|
2021-01-14 17:33:17 +08:00
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
module.exports = [
|
|
|
|
...webpackConfig,
|
2022-07-02 21:02:48 +08:00
|
|
|
...webpackESMConfig,
|
2022-03-12 09:56:32 +08:00
|
|
|
...webpackDarkConfig,
|
|
|
|
...webpackCompactConfig,
|
|
|
|
...webpackVariableConfig,
|
|
|
|
];
|