-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathgulpfile.js
150 lines (136 loc) Β· 4.04 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const {Transform} = require('stream');
const babel = require('gulp-babel');
const gulp = require('gulp');
const path = require('path');
const {rimraf} = require('rimraf');
const swc = require('@swc/core');
const babelConfig = require('./babel.config.json');
const IGNORED_PACKAGES = [
'!packages/examples/**',
'!packages/core/integration-tests/**',
'!packages/core/workers/test/integration/**',
'!packages/core/test-utils/**',
'!packages/core/types/**',
'!packages/core/types-internal/**',
// These packages are bundled.
'!packages/core/codeframe/**',
'!packages/core/fs/**',
'!packages/core/package-manager/**',
'!packages/core/utils/**',
'!packages/reporters/cli/**',
'!packages/reporters/dev-server/**',
];
const paths = {
packageSrc: [
'packages/*/*/src/**/*.js',
'!**/dev-prelude.js',
...IGNORED_PACKAGES,
],
packageOther: ['packages/*/*/src/**/dev-prelude.js'],
packageJson: [
'packages/core/parcel/package.json',
'packages/utils/create-react-app/package.json',
'packages/utils/create-parcel/package.json',
'packages/dev/query/package.json',
'packages/dev/bundle-stats-cli/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/**').then(
() => cb(),
err => cb(err),
);
};
exports.default = exports.build = gulp.series(
gulp.parallel(buildBabel, buildRSC, copyOthers),
// Babel reads from package.json so update these after babel has run
paths.packageJson.map(
packageJsonPath =>
function updatePackageJson() {
return _updatePackageJson(packageJsonPath);
},
),
);
function buildBabel() {
return gulp
.src(paths.packageSrc)
.pipe(babel({...babelConfig, babelrcRoots: [__dirname + '/packages/*/*']}))
.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 buildRSC() {
return gulp
.src('packages/utils/rsc/src/*.tsx')
.pipe(
new TapStream(vinyl => {
let result = swc.transformSync(vinyl.contents.toString(), {
filename: vinyl.path,
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
target: 'esnext',
experimental: {
emitIsolatedDts: true,
},
},
});
let output = JSON.parse(result.output);
vinyl.contents = Buffer.from(output.__swc_isolated_declarations__);
}),
)
.pipe(renameStream(relative => relative.replace('.tsx', '.d.ts')))
.pipe(gulp.dest('packages/utils/rsc/lib'));
}
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));
});
}