Skip to content

Commit e37573a

Browse files
committed
feat: support esm
1 parent 83391ad commit e37573a

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

build/webpack.prod.conf.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
1010
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1111
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
1212

13+
const isEsm = process.env.ESM;
1314
const isExampleEnv = process.env.EXAMPLE_ENV;
14-
const distPath = '../lib';
15+
const distPath = isEsm ? '../esm' : '../lib';
1516

1617
const env = process.env.NODE_ENV === 'testing' ? require('../config/test.env') : config.build.env;
1718

@@ -32,21 +33,35 @@ const webpackConfig = merge(baseWebpackConfig, {
3233
});
3334

3435
if (!isExampleEnv) {
35-
webpackConfig.entry = {
36-
'vue-json-pretty': './src/index.ts',
37-
};
3836
webpackConfig.output = {
3937
path: path.resolve(__dirname, distPath),
4038
filename: `${distPath}/[name].js`,
41-
globalObject: 'this',
42-
library: 'VueJsonPretty',
43-
libraryTarget: 'umd',
4439
};
40+
if (isEsm) {
41+
webpackConfig.entry = {
42+
'vue-json-pretty': './src/index.ts',
43+
};
44+
webpackConfig.experiments = {
45+
outputModule: true,
46+
};
47+
webpackConfig.output.library = { type: 'module' };
48+
webpackConfig.output.chunkFormat = 'module';
49+
webpackConfig.target = 'es2019';
50+
} else {
51+
webpackConfig.entry = {
52+
'vue-json-pretty': './src/index.ts',
53+
};
54+
webpackConfig.output.globalObject = 'this';
55+
webpackConfig.output.library = 'VueJsonPretty';
56+
webpackConfig.output.libraryTarget = 'umd';
57+
}
4558
webpackConfig.externals = {
4659
vue: {
4760
root: 'Vue',
48-
commonjs: 'vue',
4961
commonjs2: 'vue',
62+
commonjs: 'vue',
63+
amd: 'vue',
64+
module: 'vue',
5065
},
5166
};
5267
webpackConfig.plugins.push(

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"description": "A JSON tree view component that is easy to use and also supports data selection.",
55
"author": "leezng <im.leezng@gmail.com>",
66
"main": "lib/vue-json-pretty.js",
7+
"module": "esm/vue-json-pretty.js",
78
"scripts": {
89
"dev": "node build/dev-server.js",
910
"build": "node build/build.js",
11+
"build:esm": "cross-env ESM=true node build/build.js",
1012
"build:example": "cross-env EXAMPLE_ENV=true node build/build.js",
1113
"build:dts": "tsc --p tsconfig.dts.json && tsc-alias -p ./tsconfig.dts.json",
1214
"test": "cypress open",

0 commit comments

Comments
 (0)