Skip to content

Commit e272ed8

Browse files
author
zhaozhiwen
committed
feat(build): 打包es module 规范模块
1 parent 6f41fa4 commit e272ed8

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

.babelrc.js

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
module.exports = {
2-
presets: ['@babel/env', '@babel/typescript', '@babel/react'],
3-
plugins: [
4-
'@babel/proposal-class-properties',
5-
[
6-
'@babel/plugin-transform-runtime',
7-
{
8-
corejs: 3,
9-
helpers: true,
10-
},
11-
],
12-
],
2+
presets: ['@babel/typescript', '@babel/react'],
3+
plugins: ['@babel/proposal-class-properties'],
4+
env: {
5+
CJS: {
6+
presets: [['@babel/env']],
7+
plugins: [
8+
[
9+
'@babel/plugin-transform-runtime',
10+
{
11+
corejs: 3,
12+
helpers: true,
13+
},
14+
],
15+
],
16+
},
17+
ESM: {
18+
presets: [
19+
[
20+
'@babel/env',
21+
{
22+
modules: false,
23+
},
24+
],
25+
],
26+
plugins: [
27+
[
28+
'@babel/plugin-transform-runtime',
29+
{
30+
corejs: 3,
31+
helpers: true,
32+
useESModules: true,
33+
},
34+
],
35+
],
36+
},
37+
},
1338
};

gulpfile.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,39 @@ const paths = {
1111
scripts: ['components/**/*.{ts,tsx}', '!components/**/demo/*.{ts,tsx}'],
1212
};
1313

14-
function compileCJS() {
15-
const { dest, scripts } = paths;
14+
/**
15+
* 编译脚本文件
16+
* @param {*} babelEnv babel环境变量
17+
* @param {*} destDir 目标目录
18+
*/
19+
function compileScripts(babelEnv, destDir) {
20+
const { scripts } = paths;
21+
process.env.BABEL_ENV = babelEnv;
1622
return gulp
1723
.src(scripts)
1824
.pipe(babel()) // 使用gulp-babel处理
19-
.pipe(gulp.dest(dest.lib));
25+
.pipe(gulp.dest(destDir));
26+
}
27+
28+
/**
29+
* 编译cjs
30+
*/
31+
function compileCJS() {
32+
const { dest } = paths;
33+
return compileScripts('CJS', dest.lib);
2034
}
2135

22-
const build = gulp.parallel(compileCJS);
36+
/**
37+
* 编译esm
38+
*/
39+
function compileESM() {
40+
const { dest } = paths;
41+
return compileScripts('ESM', dest.esm);
42+
}
43+
44+
const buildScripts = gulp.series(compileCJS, compileESM);
45+
46+
const build = gulp.parallel(buildScripts);
2347

2448
exports.build = build;
2549

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"typings": "types/index.d.ts",
66
"main": "lib/index.js",
7+
"module": "esm/index.js",
78
"scripts": {
89
"dev": "docz dev",
910
"start": "npm run dev",

0 commit comments

Comments
 (0)