From c5ea668e883723b1772fa25d2c13cc42a11f93e2 Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Sun, 15 Oct 2023 20:44:54 -0500 Subject: [PATCH] chore: provide esm output file in dist (#6966) --- antd-tools/getWebpackConfig.js | 30 +++++++++++++++++++++++------- webpack.build.conf.js | 3 ++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/antd-tools/getWebpackConfig.js b/antd-tools/getWebpackConfig.js index 380b276ab..a45bb011f 100644 --- a/antd-tools/getWebpackConfig.js +++ b/antd-tools/getWebpackConfig.js @@ -22,7 +22,7 @@ const imageOptions = { limit: 10000, }; -function getWebpackConfig(modules) { +function getWebpackConfig(modules, esm = false) { const pkg = require(getProjectPath('package.json')); const babelConfig = require('./getBabelCommonConfig')(modules || false); @@ -185,7 +185,7 @@ All rights reserved. }; if (process.env.RUN_ENV === 'PRODUCTION') { - const entry = ['./index']; + let entry = ['./index']; config.externals = [ { vue: { @@ -197,9 +197,25 @@ All rights reserved. }, }, ]; - config.output.library = distFileBaseName; - config.output.libraryTarget = 'umd'; - config.output.globalObject = 'this'; + if (esm) { + entry = ['./index.esm']; + config.experiments = { + ...config.experiments, + outputModule: true, + }; + config.output.chunkFormat = 'module'; + config.output.library = { + type: 'module', + }; + config.target = 'es2019'; + } else { + config.output.libraryTarget = 'umd'; + config.output.library = distFileBaseName; + config.output.globalObject = 'this'; + } + + const entryName = esm ? `${distFileBaseName}.esm` : distFileBaseName; + config.optimization = { minimizer: [ new TerserPlugin({ @@ -213,7 +229,7 @@ All rights reserved. // Development const uncompressedConfig = merge({}, config, { entry: { - [distFileBaseName]: entry, + [entryName]: entry, }, mode: 'development', plugins: [ @@ -226,7 +242,7 @@ All rights reserved. // Production const prodConfig = merge({}, config, { entry: { - [`${distFileBaseName}.min`]: entry, + [`${entryName}.min`]: entry, }, mode: 'production', plugins: [ diff --git a/webpack.build.conf.js b/webpack.build.conf.js index de7b9d91e..69ab4ac1e 100644 --- a/webpack.build.conf.js +++ b/webpack.build.conf.js @@ -39,6 +39,7 @@ function externalDayjs(config) { } const webpackConfig = getWebpackConfig(false); +const webpackESMConfig = getWebpackConfig(false, true); if (process.env.RUN_ENV === 'PRODUCTION') { webpackConfig.forEach(config => { @@ -72,4 +73,4 @@ if (process.env.RUN_ENV === 'PRODUCTION') { }); } -module.exports = [...webpackConfig]; +module.exports = [...webpackConfig, ...webpackESMConfig];