-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
96 lines (78 loc) · 2.28 KB
/
Copy pathgulpfile.js
File metadata and controls
96 lines (78 loc) · 2.28 KB
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
const gulp = require('gulp');
const path = require('path');
const fs = require('fs');
const babel = require('gulp-babel');
const DIST_DIR = 'dist';
const rootPath = path.join(process.cwd(), 'packages');
let babelConfig;
try {
babelConfig = fs.readFileSync(path.join(process.cwd(), '/.babelrc'), 'utf8');
babelConfig = JSON.parse(babelConfig);
} catch (e) {
babelConfig = {};
babelConfig.presets = [
"@babel/typescript",
["@babel/preset-env", {
"targets": {
"browsers": [">0.25%", "ie > 10"]
},
}],
];
babelConfig.plugins = ['@babel/plugin-proposal-class-properties','@babel/plugin-transform-runtime','@babel/plugin-syntax-dynamic-import']
}
const packages = fs.readdirSync(rootPath).map(item => {
return {
name: item,
src: path.join(rootPath, item),
};
});
function findPackageByFile(filePath) {
return packages.filter(item => {
return new RegExp(`^${item.src}`).test(filePath);
})[0];
}
function gernerateProductionTask() {
const tasks = [];
packages.forEach(item => {
tasks.push(buildScript(`${item.src}/src/**/*.ts`, path.join(item.src, DIST_DIR)));
});
return tasks;
}
// 开发时构建
function gernerateBuildTask() {
const tasks = [];
babelConfig.ignore = ['**/*.d.ts'];
packages.forEach(item => {
tasks.push(buildScript(`${item.src}/src/**/*.ts`, path.join(item.src, DIST_DIR)));
});
return tasks;
}
function buildScript(src, dest) {
return function() {
return gulp
.src(src)
.pipe(babel(babelConfig))
.pipe(gulp.dest(dest));
};
}
function watch() {
const paths = {
scripts: {
src: path.join(rootPath, '**/src/**/*.ts'),
},
};
const watcher = gulp.watch(paths.scripts.src);
console.log('start watch ', paths.scripts.src, '...');
function handleChange(filePath) {
console.log(`File ${filePath} was changed`);
const package = findPackageByFile(filePath);
if (package) {
const relativePath = path.relative(path.join(package.src, 'dist'), path.dirname(filePath));
buildScript(filePath, path.join(package.src, DIST_DIR, relativePath))();
}
}
watcher.on('change', handleChange);
watcher.on('add', handleChange);
}
exports.build = gulp.series(...gernerateProductionTask());
exports.watch = gulp.series(...gernerateBuildTask(), watch);