From cded2c8a4fa5a4c0c400b37197131a907e5a6eca Mon Sep 17 00:00:00 2001 From: rex Date: Wed, 3 Apr 2019 10:16:15 +0800 Subject: [PATCH] [new feature]: compile es5 dist fix #1469 --- build/compiler.js | 61 +++++++++++++++++++++++++++++------------------ package.json | 5 ++-- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/build/compiler.js b/build/compiler.js index d6d35b93..538c1f58 100644 --- a/build/compiler.js +++ b/build/compiler.js @@ -6,16 +6,21 @@ const insert = require('gulp-insert'); const rename = require('gulp-rename'); const postcss = require('gulp-postcss'); -const tsProject = ts.createProject(path.resolve(__dirname, '../tsconfig.json')); +const tsConfig = path.resolve(__dirname, '../tsconfig.json'); const isProduction = process.env.NODE_ENV === 'production'; const src = path.join(__dirname, '../packages'); -const dist = path.join(__dirname, isProduction ? '../dist' : '../example/dist'); +const libDir = path.resolve(__dirname, '../lib'); +const esDir = path.resolve(__dirname, '../dist'); +const exampleDir = path.resolve(__dirname, '../example/dist'); -function copy(ext) { - return gulp.src(`${src}/**/*.${ext}`).pipe(gulp.dest(dist)); -} +const libConfig = { + target: 'es5', + lib: ['es2015', 'es2017', 'dom'], + module: 'commonjs', + declaration: false +}; -function compileLess() { +const compileLess = dist => () => gulp .src(`${src}/**/*.less`) .pipe(less()) @@ -34,26 +39,36 @@ function compileLess() { }) ) .pipe(gulp.dest(dist)); -} -function compileTs() { - tsProject +const compileTs = (dist, config) => () => { + const tsProject = ts.createProject(tsConfig, config); + return tsProject .src() .pipe(tsProject()) .on('error', () => {}) .pipe(gulp.dest(dist)); +}; + +const copy = (dist, ext) => () => + gulp.src(`${src}/**/*.${ext}`).pipe(gulp.dest(dist)); + +const compile = (dist, config) => + gulp.parallel( + compileTs(dist, config), + compileLess(dist), + copy(dist, 'wxml'), + copy(dist, 'wxs'), + copy(dist, 'json') + ); + +if (isProduction) { + gulp.series(compile(esDir), compile(libDir, libConfig))(); +} else { + compile(exampleDir)(); + + gulp.watch(`${src}/**/*.ts`, compileTs(exampleDir)); + gulp.watch(`${src}/**/*.less`, compileLess(exampleDir)); + gulp.watch(`${src}/**/*.wxml`, copy(exampleDir, 'wxml')); + gulp.watch(`${src}/**/*.wxs`, copy(exampleDir, 'wxs')); + gulp.watch(`${src}/**/*.json`, copy(exampleDir, 'json')); } - -const compileWxml = () => copy('wxml'); -const compileJson = () => copy('json'); -const compileWxs = () => copy('wxs'); - -if (!isProduction) { - gulp.watch(`${src}/**/*.ts`, compileTs); - gulp.watch(`${src}/**/*.less`, compileLess); - gulp.watch(`${src}/**/*.wxml`, compileWxml); - gulp.watch(`${src}/**/*.wxs`, compileWxs); - gulp.watch(`${src}/**/*.json`, compileJson); -} - -gulp.parallel(compileTs, compileLess, compileWxml, compileJson, compileWxs)(); diff --git a/package.json b/package.json index 444d1592..ae30cb43 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,13 @@ "dev": "node build/dev.js", "lint": "eslint ./packages --ext .js", "release": "sh build/release.sh", - "build:lib": "yarn && rm -rf dist && NODE_ENV=production node build/compiler.js", + "build:lib": "yarn && rm -rf dist && rm -rf lib && NODE_ENV=production node build/compiler.js", "build:site": "rm -rf docs/dist && cross-env NODE_ENV=production webpack --config build/webpack.doc.prd.js && gh-pages -d docs/dist", "build:changelog": "vant-doc changelog --tag v0.5.0 changelog.generated.md" }, "files": [ - "dist" + "dist", + "lib" ], "repository": { "type": "git",