Skip to content

Commit 847ec8c

Browse files
authored
Merge pull request #271 from leezng/feat/esm
feat: support esm
2 parents 9818ec7 + d35d855 commit 847ec8c

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules/
33
example-dist/
44
dist/
55
lib/
6+
esm/
67
types/
78
npm-debug.log*
89
yarn-debug.log*

build/build.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ require('./check-versions')();
22

33
process.env.NODE_ENV = 'production';
44

5-
const fs = require('fs');
6-
const path = require('path');
75
const chalk = require('chalk');
86
const webpack = require('webpack');
9-
const { spawn } = require('child_process');
107
const webpackConfig = require('./webpack.prod.conf');
118

9+
const isEsm = process.env.ESM;
1210
const isExampleEnv = process.env.EXAMPLE_ENV;
1311

12+
const successText = {
13+
main: 'Build main sources complete.',
14+
esm: 'Build esm sources complete.',
15+
example: 'Build example page complete.',
16+
};
17+
1418
webpack(webpackConfig, (err, stats) => {
1519
if (err) throw err;
1620

@@ -29,7 +33,8 @@ webpack(webpackConfig, (err, stats) => {
2933
process.exit(1);
3034
}
3135

32-
console.log(chalk.cyan('Build sources complete.\n'));
36+
const text = isExampleEnv ? successText.example : isEsm ? successText.esm : successText.main;
37+
console.log(chalk.cyan(`${text}\n`));
3338

3439
if (isExampleEnv) {
3540
console.log(
@@ -38,13 +43,5 @@ webpack(webpackConfig, (err, stats) => {
3843
"Opening index.html over file:// won't work.\n",
3944
),
4045
);
41-
} else {
42-
const buildTypesProcess = spawn('npm', ['run', 'build:dts'], {
43-
stdio: 'inherit',
44-
});
45-
46-
buildTypesProcess.on('close', () => {
47-
console.log(chalk.cyan('Build types(.d.ts) complete.\n'));
48-
});
4946
}
5047
});

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
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",
9-
"build": "node build/build.js",
10+
"build": "npm run build:main && npm run build:esm && npm run build:dts",
11+
"build:main": "node build/build.js",
12+
"build:esm": "cross-env ESM=true node build/build.js",
1013
"build:example": "cross-env EXAMPLE_ENV=true node build/build.js",
1114
"build:dts": "tsc --p tsconfig.dts.json && tsc-alias -p ./tsconfig.dts.json",
1215
"test": "cypress open",
@@ -91,6 +94,7 @@
9194
],
9295
"files": [
9396
"lib",
97+
"esm",
9498
"types"
9599
],
96100
"peerDependencies": {

0 commit comments

Comments
 (0)