forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
108 lines (97 loc) · 2.9 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
const {Transform} = require('stream');
const babel = require('gulp-babel');
const gulp = require('gulp');
const path = require('path');
const rimraf = require('rimraf');
const babelConfig = require('./babel.config.json');
const IGNORED_PACKAGES = [
'!packages/examples/**',
'!packages/core/integration-tests/**',
'!packages/core/workers/test/integration/**',
'!packages/core/is-v2-ready-yet/**',
'!packages/core/test-utils/**',
'!packages/core/types/**',
'!packages/utils/node-libs-browser/**',
];
const paths = {
packageSrc: [
'packages/*/*/src/**/*.js',
'!packages/*/scope-hoisting/src/helpers.js',
'!**/loaders/**',
'!**/prelude.js',
...IGNORED_PACKAGES,
],
packageOther: [
'packages/*/scope-hoisting/src/helpers.js',
'packages/*/*/src/**/loaders/**',
'packages/*/*/src/**/prelude.js',
'packages/*/dev-server/src/templates/**',
],
packageJson: 'packages/core/parcel/package.json',
packages: 'packages/',
};
/*
* "Taps" into the contents of a flowing stream, yielding chunks to the passed
* callback. Continues to pass data chunks down the stream.
*/
class TapStream extends Transform {
constructor(tap, options) {
super({...options, objectMode: true});
this._tap = tap;
}
_transform(chunk, encoding, callback) {
try {
this._tap(chunk);
callback(null, chunk);
} catch (err) {
callback(err);
}
}
}
exports.clean = function clean(cb) {
rimraf('packages/*/*/lib/**', cb);
};
exports.default = exports.build = gulp.series(
gulp.parallel(buildBabel, copyOthers),
// Babel reads from package.json so update these after babel has run
() => updatePackageJson(paths.packageJson),
);
function buildBabel() {
return gulp
.src(paths.packageSrc)
.pipe(babel(babelConfig))
.pipe(renameStream(relative => relative.replace('src', 'lib')))
.pipe(gulp.dest(paths.packages));
}
function copyOthers() {
return gulp
.src(paths.packageOther)
.pipe(renameStream(relative => relative.replace('src', 'lib')))
.pipe(gulp.dest(paths.packages));
}
function updatePackageJson(file) {
return gulp
.src(file)
.pipe(
new TapStream(vinyl => {
let json = JSON.parse(vinyl.contents);
// Replace all references to `src` in package.json bin entries
// `lib` equivalents.
if (typeof json.bin === 'object' && json.bin != null) {
for (let [binName, binPath] of Object.entries(json.bin)) {
json.bin[binName] = binPath.replace('src', 'lib');
}
} else if (typeof json.bin === 'string') {
json.bin = json.bin.replace('src', 'lib');
}
vinyl.contents = Buffer.from(JSON.stringify(json, null, 2));
}),
)
.pipe(gulp.dest(path.dirname(file)));
}
function renameStream(fn) {
return new TapStream(vinyl => {
let relative = path.relative(vinyl.base, vinyl.path);
vinyl.path = path.join(vinyl.base, fn(relative));
});
}