Skip to content

Commit aae9a48

Browse files
authored
chore: bundle esm output (#5772)
1 parent 09c5a3e commit aae9a48

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

antd-tools/getWebpackConfig.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const imageOptions = {
2222
limit: 10000,
2323
};
2424

25-
function getWebpackConfig(modules) {
25+
function getWebpackConfig(modules, esm = false) {
2626
const pkg = require(getProjectPath('package.json'));
2727
const babelConfig = require('./getBabelCommonConfig')(modules || false);
2828

@@ -222,10 +222,10 @@ All rights reserved.
222222
commonjs2: 'vue',
223223
commonjs: 'vue',
224224
amd: 'vue',
225+
module: 'vue',
225226
},
226227
};
227-
config.output.library = distFileBaseName;
228-
config.output.libraryTarget = 'umd';
228+
229229
config.optimization = {
230230
minimizer: [
231231
new TerserPlugin({
@@ -236,11 +236,27 @@ All rights reserved.
236236
}),
237237
],
238238
};
239+
if (esm) {
240+
config.experiments = {
241+
...config.experiments,
242+
outputModule: true,
243+
};
244+
config.output.chunkFormat = 'module';
245+
config.output.library = {
246+
type: 'module',
247+
};
248+
config.target = 'es2019';
249+
} else {
250+
config.output.libraryTarget = 'umd';
251+
config.output.library = distFileBaseName;
252+
}
253+
254+
const entryName = esm ? `${distFileBaseName}.esm` : distFileBaseName;
239255

240256
// Development
241257
const uncompressedConfig = merge({}, config, {
242258
entry: {
243-
[distFileBaseName]: entry,
259+
[entryName]: entry,
244260
},
245261
mode: 'development',
246262
plugins: [
@@ -253,7 +269,7 @@ All rights reserved.
253269
// Production
254270
const prodConfig = merge({}, config, {
255271
entry: {
256-
[`${distFileBaseName}.min`]: entry,
272+
[`${entryName}.min`]: entry,
257273
},
258274
mode: 'production',
259275
plugins: [

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
require('./index-style-only');
1+
import './index-style-only';
22

3-
module.exports = require('./components');
3+
export * from './components';

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
"ts-loader": "^9.1.0",
244244
"typescript": "~4.5.2",
245245
"umi-request": "^1.3.5",
246+
"unified": "9.2.2",
246247
"url-loader": "^3.0.0",
247248
"vite": "^2.9.13",
248249
"vue": "^3.2.0",

webpack.build.conf.js

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function externalDayjs(config) {
4646
commonjs2: 'dayjs',
4747
commonjs: 'dayjs',
4848
amd: 'dayjs',
49+
module: 'dayjs',
4950
};
5051
}
5152

@@ -113,6 +114,7 @@ const legacyEntryVars = {
113114
'root-entry-name': 'default',
114115
};
115116
const webpackConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
117+
const webpackESMConfig = injectLessVariables(getWebpackConfig(false, true), legacyEntryVars);
116118
const webpackDarkConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
117119
const webpackCompactConfig = injectLessVariables(getWebpackConfig(false), legacyEntryVars);
118120
const webpackVariableConfig = injectLessVariables(getWebpackConfig(false), {
@@ -161,6 +163,7 @@ if (process.env.RUN_ENV === 'PRODUCTION') {
161163

162164
module.exports = [
163165
...webpackConfig,
166+
...webpackESMConfig,
164167
...webpackDarkConfig,
165168
...webpackCompactConfig,
166169
...webpackVariableConfig,

0 commit comments

Comments
 (0)