This repository has been archived by the owner on May 25, 2021. It is now read-only.
forked from facebook/fbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
219 lines (191 loc) · 5.79 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
/**
* Copyright (c) Facebook, Inc. and its 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
*/
'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 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 rewriteModules = 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: [
'runtime/**/*.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) Facebook, Inc. and its affiliates.';
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.
*/
`;
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(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(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 flowCheck = () =>
flatLib(
gulp
.src(paths.runtime, {follow: true})
.pipe(rename({extname: '.js.flow'}))
.pipe(rewriteModules({map: moduleMap})),
);
const copyFlowTypedModules = () =>
flatLib(gulp.src(paths.typedModules, {follow: true}));
gulp.task('flow', gulp.parallel(flowCheck, copyFlowTypedModules));
const buildCSS = () =>
gulp
.src(paths.css, {follow: true})
.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'),
),
);