This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
120 lines (102 loc) · 3.3 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @description 构建脚本
* @author: minfive
* @createDate: 2018-06-24
* @lastModify minfive
* @lastDate: 2018-06-24
*/
const del = require('del');
const path = require('path');
const gulp = require('gulp');
const chalk = require('chalk');
const webpack = require('webpack');
const through = require('through2');
const gulpCache = require('gulp-cached');
const gulpNewer = require('gulp-newer');
const gulpBabel = require('gulp-babel');
const gulpIgnore = require('gulp-ignore');
const pkg = require('./package.json');
const FROM_DIR = './src';
const OUT_DIR = process.env.OUTPUT_DIST || './dist';
function webpackFile(srcList, isShortPath = false) {
function shortPath(src) {
let nowPathObj = path.parse(src);
let dirs = nowPathObj.dir.split('/');
return path.format({
...nowPathObj,
base: dirs.pop() + '.js',
dir: dirs.join('/')
});
}
return new Promise((resolve, reject) => {
webpack(
srcList.map(src => {
return {
entry: src,
output: {
path: path.join(__dirname, OUT_DIR),
libraryTarget: 'commonjs-module',
filename: isShortPath ? shortPath(src) : src
},
optimization: {
minimize: false
}
};
}),
(err, stats) => {
if (err || stats.hasErrors()) {
throw err;
}
resolve();
}
);
});
}
function runtimeFile() {
const helpers = new Set();
return through.obj(async(file, encoding, callback) => {
let inputHelpers = file.babel.usedHelpers
.filter(helper => {
if (helpers.has(helper)) {
return false;
} else {
helpers.add(helper);
return true;
}
})
.map(helper => `babel-runtime/helpers/${helper}.js`);
await webpackFile(inputHelpers);
let dirTree = file.relative.split(path.sep);
dirTree.pop();
if (dirTree.length > 0) {
let str = file.contents.toString()
.replace(/from '\/babel-runtime\//g, `from '${dirTree.reduce(res => res + '../', '')}babel-runtime/`);
file.contents = Buffer.from(str);
}
inputHelpers.forEach(helper => console.log(chalk.green('input runtime success: ' + helper)));
callback(null, file);
});
}
gulp.task('clean', () => del([OUT_DIR]));
gulp.task('js', () => {
return gulp.src(`${FROM_DIR}/**/*.js`)
.pipe(gulpCache(`${pkg.name}-js`))
.pipe(gulpBabel())
.pipe(runtimeFile())
.pipe(gulp.dest(OUT_DIR));
});
gulp.task('other', () => {
return gulp.src(`${FROM_DIR}/**`)
.pipe(gulpIgnore.exclude(file => path.extname(file.path) === '.js'))
.pipe(gulpNewer(OUT_DIR))
.pipe(gulp.dest(OUT_DIR));
});
gulp.task('dev', ['clean'], () => {
gulp.start('build');
gulp.watch(`${FROM_DIR}/**`, ['js', 'other']);
});
gulp.task('build', ['clean'], () => {
webpackFile(['babel-runtime/regenerator/index.js'], true);
gulp.start('js');
gulp.start('other');
});