Skip to content

Commit

Permalink
feat(build): 打包es module 规范模块
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozhiwen committed Dec 25, 2019
1 parent 6f41fa4 commit e272ed8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
47 changes: 36 additions & 11 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
module.exports = {
presets: ['@babel/env', '@babel/typescript', '@babel/react'],
plugins: [
'@babel/proposal-class-properties',
[
'@babel/plugin-transform-runtime',
{
corejs: 3,
helpers: true,
},
],
],
presets: ['@babel/typescript', '@babel/react'],
plugins: ['@babel/proposal-class-properties'],
env: {
CJS: {
presets: [['@babel/env']],
plugins: [
[
'@babel/plugin-transform-runtime',
{
corejs: 3,
helpers: true,
},
],
],
},
ESM: {
presets: [
[
'@babel/env',
{
modules: false,
},
],
],
plugins: [
[
'@babel/plugin-transform-runtime',
{
corejs: 3,
helpers: true,
useESModules: true,
},
],
],
},
},
};
32 changes: 28 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,39 @@ const paths = {
scripts: ['components/**/*.{ts,tsx}', '!components/**/demo/*.{ts,tsx}'],
};

function compileCJS() {
const { dest, scripts } = paths;
/**
* 编译脚本文件
* @param {*} babelEnv babel环境变量
* @param {*} destDir 目标目录
*/
function compileScripts(babelEnv, destDir) {
const { scripts } = paths;
process.env.BABEL_ENV = babelEnv;
return gulp
.src(scripts)
.pipe(babel()) // 使用gulp-babel处理
.pipe(gulp.dest(dest.lib));
.pipe(gulp.dest(destDir));
}

/**
* 编译cjs
*/
function compileCJS() {
const { dest } = paths;
return compileScripts('CJS', dest.lib);
}

const build = gulp.parallel(compileCJS);
/**
* 编译esm
*/
function compileESM() {
const { dest } = paths;
return compileScripts('ESM', dest.esm);
}

const buildScripts = gulp.series(compileCJS, compileESM);

const build = gulp.parallel(buildScripts);

exports.build = build;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"typings": "types/index.d.ts",
"main": "lib/index.js",
"module": "esm/index.js",
"scripts": {
"dev": "docz dev",
"start": "npm run dev",
Expand Down

0 comments on commit e272ed8

Please sign in to comment.