This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
/
gulpfile.js
255 lines (226 loc) · 6.67 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @noflow
* @oncall i18n_fbt_oss
*/
'use strict';
const {PLUGINS} = require('./babelPlugins');
const moduleMap = require('./moduleMap');
const babelPluginFbtGulp = require('./packages/babel-plugin-fbt/gulpfile');
const {version} = require('./packages/fbt/package.json');
const setGeneratedFilePragmas = require('./setGeneratedFilePragmas');
const del = require('del');
const gulp = require('gulp');
const babel = require('gulp-babel');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const derequire = require('gulp-derequire');
const flatten = require('gulp-flatten');
const header = require('gulp-header');
const gulpif = require('gulp-if');
const once = require('gulp-once');
const rename = require('gulp-rename');
const rewriteFlowtypedModules = require('gulp-rewrite-flowtyped-modules');
const gulpUtil = require('gulp-util');
const webpackStream = require('webpack-stream');
const paths = {
published: 'packages/fbt',
dist: 'packages/fbt/dist',
lib: 'packages/fbt/lib',
license: 'LICENSE',
runtime: [
// Individually listing subfolders of `runtime` to allow watching through these symlinks
'runtime/nonfb/**/*.js',
'runtime/shared/**/*.js',
'runtime/shared_deps/**/*.js',
'!runtime/**/__tests__/*',
'!runtime/**/__mocks__/*',
],
runtimeTests: ['runtime/nonfb/**/__tests__/*'],
runtimeMocks: ['runtime/nonfb/**/__mocks__/*'],
typedModules: ['flow-types/typed-js-modules/*.flow'],
css: ['runtime/**/*.css'],
};
const COPYRIGHT = 'Copyright (c) Meta Platforms, Inc. and affiliates.';
const ONCALL_ID = 'i18n_fbt_oss';
const COPYRIGHT_HEADER = `/**
* fbt v<%= version %>
*
* ${COPYRIGHT}
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @${'generated'}
* @${'nolint'}
* @${'nogrep'}
* @oncall ${ONCALL_ID}
*/
`;
const buildDist = function (opts) {
const webpackOpts = {
externals: {},
output: {
filename: opts.output,
libraryTarget: 'umd',
library: 'fbt',
},
plugins: [
new webpackStream.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(
opts.debug ? 'development' : 'production',
),
}),
new webpackStream.webpack.LoaderOptionsPlugin({
debug: opts.debug,
}),
],
optimization: {
minimize: !opts.debug,
},
};
return webpackStream(webpackOpts, null, function (err, stats) {
if (err) {
throw new gulpUtil.PluginError('webpack', err);
}
if (stats.compilation.errors.length) {
gulpUtil.log('webpack', '\n' + stats.toString({colors: true}));
}
});
};
const copyLicense = () =>
gulp.src(paths.license).pipe(gulp.dest(paths.published));
gulp.task('license', gulp.series(copyLicense));
function flatLib(job) {
return job
.pipe(flatten())
.pipe(setGeneratedFilePragmas(ONCALL_ID))
.pipe(gulp.dest(paths.lib));
}
const buildModules = () =>
flatLib(
gulp
.src(paths.runtime, {follow: true})
.pipe(once())
.pipe(
babel({
plugins: [
...PLUGINS,
require('@babel/plugin-syntax-jsx'),
require('babel-plugin-fbt'),
require('babel-plugin-fbt-runtime'),
[
require('babel-preset-fbjs/plugins/rewrite-modules'),
{map: moduleMap},
],
require('@babel/plugin-transform-react-jsx'),
],
}),
),
);
gulp.task('modules', gulp.series(babelPluginFbtGulp.build, buildModules));
const babelTestPresets = {
plugins: [
...PLUGINS,
'@babel/plugin-syntax-jsx',
// TODO #81682213 - Bring in shared runtime tests
// The fbtCommon map below is only applicable to fbt-test.js, which doesn't
// yet run in github
['babel-plugin-fbt', {fbtCommon: {Accept: '...'}}],
'babel-plugin-fbt-runtime',
'@babel/plugin-transform-react-jsx',
],
};
const transformTests = (src, dest) =>
gulp
.src(src, {follow: true})
.pipe(once())
.pipe(babel(babelTestPresets))
.pipe(flatten())
.pipe(setGeneratedFilePragmas())
.pipe(gulp.dest(dest));
const buildRuntimeTests = () =>
transformTests(paths.runtimeTests, paths.lib + '/__tests__');
const buildRuntimeMocks = () =>
transformTests(paths.runtimeMocks, paths.lib + '/__mocks__');
gulp.task(
'test-modules',
gulp.series(
babelPluginFbtGulp.build,
gulp.parallel(buildRuntimeTests, buildRuntimeMocks),
),
);
// Copy raw source with rewritten modules to *.js.flow
const buildRuntimeFlowJS = () =>
flatLib(
gulp
.src(paths.runtime, {follow: true})
.pipe(rename({extname: '.js.flow'}))
.pipe(once())
.pipe(rewriteFlowtypedModules({map: moduleMap})),
);
const copyFlowTypedModules = () =>
flatLib(gulp.src(paths.typedModules, {follow: true}));
gulp.task('flow', gulp.parallel(buildRuntimeFlowJS, copyFlowTypedModules));
const buildCSS = () =>
gulp
.src(paths.css, {follow: true})
.pipe(once())
.pipe(concat('fbt.css'))
.pipe(cleanCSS({advanced: false}))
.pipe(header(COPYRIGHT_HEADER, {version}))
.pipe(gulp.dest(paths.lib));
gulp.task('css', gulp.series(buildCSS));
const buildDistTask = () =>
gulp
.src('./packages/fbt/lib/FbtPublic.js')
.pipe(buildDist({debug: true, output: 'fbt.js'}))
.pipe(derequire())
.pipe(gulpif('*.js', header(COPYRIGHT_HEADER, {version})))
.pipe(gulp.dest(paths.dist));
gulp.task('dist', gulp.series('modules', 'css', buildDistTask));
const buildDistMinTask = () =>
gulp
.src('./packages/fbt/lib/FbtPublic.js')
.pipe(buildDist({debug: false, output: 'fbt.min.js'}))
.pipe(gulpif('*.js', header(COPYRIGHT_HEADER, {version})))
.pipe(gulp.dest(paths.dist));
gulp.task('dist:min', gulp.series('modules', buildDistMinTask));
const cleanTask = () =>
del([
'.checksums',
paths.published + '/*',
'!' + paths.published + '/package.json',
'!' + paths.published + '/README.md',
]);
gulp.task('clean', gulp.parallel(babelPluginFbtGulp.clean, cleanTask));
gulp.task(
'build-runtime',
gulp.series(
gulp.parallel('license', 'modules', 'test-modules', 'flow'),
gulp.series('dist', 'dist:min'),
),
);
gulp.task('watch-runtime', () => {
gulp.watch(
[paths.license].concat(
paths.runtime,
paths.runtimeTests,
paths.runtimeMocks,
paths.typedModules,
paths.css,
),
{
cwd: __dirname,
ignoreInitial: false,
},
function watchRuntimeFbt(done) {
gulp.task('build-runtime')(done);
},
);
});