forked from facebook/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
314 lines (299 loc) · 9.03 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const babel = require('gulp-babel');
const babelOptions = require('./scripts/getBabelOptions')({
moduleMap: {
'babel-core': 'babel-core',
'babel-generator': 'babel-generator',
'babel-polyfill': 'babel-polyfill',
'babel-runtime/helpers/asyncToGenerator': 'babel-runtime/helpers/asyncToGenerator',
'babel-runtime/helpers/classCallCheck': 'babel-runtime/helpers/classCallCheck',
'babel-runtime/helpers/defineProperty': 'babel-runtime/helpers/defineProperty',
'babel-runtime/helpers/extends': 'babel-runtime/helpers/extends',
'babel-runtime/helpers/inherits': 'babel-runtime/helpers/inherits',
'babel-runtime/helpers/possibleConstructorReturn': 'babel-runtime/helpers/possibleConstructorReturn',
'babel-runtime/helpers/toConsumableArray': 'babel-runtime/helpers/toConsumableArray',
'babel-traverse': 'babel-traverse',
'babel-types': 'babel-types',
'babylon': 'babylon',
'chalk': 'chalk',
'child_process': 'child_process',
'crypto': 'crypto',
'fb-watchman': 'fb-watchman',
'fs': 'fs',
'graphql': 'graphql',
'immutable': 'immutable',
'net': 'net',
'path': 'path',
'process': 'process',
'prop-types': 'prop-types',
'React': 'react',
'ReactDOM': 'react-dom',
'ReactNative': 'react-native',
'RelayRuntime': 'relay-runtime',
'signedsource': 'signedsource',
'StaticContainer.react': 'react-static-container',
'util': 'util',
'yargs': 'yargs',
},
plugins: [
'transform-flow-strip-types',
['transform-runtime', {'polyfill': false}],
],
postPlugins: [
'transform-async-to-generator',
'transform-es2015-modules-commonjs',
],
});
const del = require('del');
const derequire = require('gulp-derequire');
const es = require('event-stream');
const flatten = require('gulp-flatten');
const fs = require('fs');
const gulp = require('gulp');
const chmod = require('gulp-chmod');
const gulpUtil = require('gulp-util');
const header = require('gulp-header');
const path = require('path');
const runSequence = require('run-sequence');
const webpackStream = require('webpack-stream');
const SCRIPT_HASHBANG = '#!/usr/bin/env node\n';
const DEVELOPMENT_HEADER = [
'/**',
' * Relay v' + process.env.npm_package_version,
' */',
].join('\n') + '\n';
const PRODUCTION_HEADER = [
'/**',
' * Relay v' + process.env.npm_package_version,
' *',
' * Copyright (c) 2013-present, Facebook, Inc.',
' * All rights reserved.',
' *',
' * This source code is licensed under the BSD-style license found in the',
' * LICENSE file in the root directory of this source tree. An additional grant',
' * of patent rights can be found in the PATENTS file in the same directory.',
' *',
' */',
].join('\n') + '\n';
const buildDist = function(filename, opts, isProduction) {
const webpackOpts = {
debug: !isProduction,
externals: [/^[-/a-zA-Z0-9]+$/],
target: opts.target,
node: {
fs: 'empty',
net: 'empty',
path: 'empty',
child_process: 'empty',
util: 'empty',
},
output: {
filename: filename,
libraryTarget: opts.libraryTarget,
library: opts.libraryName,
},
plugins: [
new webpackStream.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(
isProduction ? 'production' : 'development'
),
}),
new webpackStream.webpack.optimize.OccurenceOrderPlugin(),
new webpackStream.webpack.optimize.DedupePlugin(),
],
};
if (isProduction && !opts.noMinify) {
webpackOpts.plugins.push(
new webpackStream.webpack.optimize.UglifyJsPlugin({
compress: {
hoist_vars: true,
screw_ie8: true,
warnings: false,
},
})
);
}
return webpackStream(webpackOpts, null, function(err, stats) {
if (err) {
throw new gulpUtil.PluginError('webpack', err);
}
if (stats.compilation.errors.length) {
throw new gulpUtil.PluginError('webpack', stats.toString());
}
});
};
// Paths from package-root
const PACKAGES = 'packages';
const DIST = 'dist';
const builds = [
{
package: 'babel-plugin-relay',
exports: {
index: 'BabelPluginRelay.js',
},
bundles: [
{
entry: 'BabelPluginRelay.js',
output: 'babel-plugin-relay',
libraryName: 'BabelPluginRelay',
libraryTarget: 'commonjs2',
target: 'node',
},
],
},
{
package: 'react-relay',
exports: {
classic: 'ReactRelayClassicExports.js',
compat: 'ReactRelayCompatPublic.js',
index: 'ReactRelayPublic.js',
},
bundles: [
{
entry: 'ReactRelayClassicExports.js',
output: 'react-relay-classic',
libraryName: 'ReactRelayClassic',
libraryTarget: 'umd',
},
{
entry: 'ReactRelayCompatPublic.js',
output: 'react-relay-compat',
libraryName: 'ReactRelayCompat',
libraryTarget: 'umd',
},
{
entry: 'ReactRelayPublic.js',
output: 'react-relay',
libraryName: 'ReactRelay',
libraryTarget: 'umd',
},
],
},
{
package: 'relay-compiler',
exports: {
index: 'RelayCompilerPublic.js',
},
bundles: [
{
entry: 'RelayCompilerPublic.js',
output: 'relay-compiler',
libraryName: 'RelayCompiler',
libraryTarget: 'commonjs2',
target: 'node',
noMinify: true, // Note: uglify can't yet handle modern JS
},
],
bins: [
{
entry: 'RelayCompilerBin.js',
output: 'relay-compiler',
libraryTarget: 'commonjs2',
target: 'node',
},
],
},
{
package: 'relay-runtime',
exports: {
index: 'RelayRuntime.js',
},
bundles: [
{
entry: 'RelayRuntime.js',
output: 'relay-runtime',
libraryName: 'RelayRuntime',
libraryTarget: 'umd',
},
],
},
];
gulp.task('clean', function() {
return del(DIST);
});
gulp.task('modules', function() {
return es.merge(builds.map(build =>
gulp.src([
'*' + PACKAGES + '/' + build.package + '/**/*.js',
// TODO: this is not a great way to share utility functions.
'*' + PACKAGES + '/react-relay/classic/tools/*.js',
'*' + PACKAGES + '/react-relay/classic/util/*.js',
'*' + PACKAGES + '/react-relay/classic/__forks__/interface/*.js',
'*' + PACKAGES + '/react-relay/classic/interface/*.js',
'*' + PACKAGES + '/relay-runtime/util/*.js',
'!' + PACKAGES + '/**/__tests__/**/*.js',
'!' + PACKAGES + '/**/__mocks__/**/*.js',
]).pipe(babel(babelOptions))
.pipe(flatten())
.pipe(gulp.dest(path.join(DIST, build.package, 'lib')))
));
});
gulp.task('copy-files', function() {
return es.merge(builds.map(build =>
gulp.src([
'LICENSE',
'PATENTS',
'*' + PACKAGES + '/' + build.package + '/*',
'!' + PACKAGES + '/' + build.package + '/**/*.js',
]).pipe(flatten())
.pipe(gulp.dest(path.join(DIST, build.package)))
));
});
gulp.task('exports', ['copy-files', 'modules'], function() {
builds.map(build =>
Object.keys(build.exports).map(exportName =>
fs.writeFileSync(
path.join(DIST, build.package, exportName + '.js'),
PRODUCTION_HEADER +
`\nmodule.exports = require('./lib/${build.exports[exportName]}');`
)
)
);
});
gulp.task('bins', ['modules'], function() {
const buildsWithBins = builds.filter(build => build.bins);
return es.merge(buildsWithBins.map(build =>
es.merge(build.bins.map(bin =>
gulp.src(path.join(DIST, build.package, 'lib', bin.entry))
.pipe(buildDist(bin.output, bin, /* isProduction */ false))
.pipe(header(SCRIPT_HASHBANG + PRODUCTION_HEADER))
.pipe(chmod(0o755))
.pipe(gulp.dest(path.join(DIST, build.package, 'bin')))
))
));
});
gulp.task('bundles', ['modules'], function() {
return es.merge(builds.map(build =>
es.merge(build.bundles.map(bundle =>
gulp.src(path.join(DIST, build.package, 'lib', bundle.entry))
.pipe(buildDist(bundle.output + '.js', bundle, /* isProduction */ false))
.pipe(derequire())
.pipe(header(DEVELOPMENT_HEADER))
.pipe(gulp.dest(path.join(DIST, build.package)))
))
));
});
gulp.task('bundles:min', ['modules'], function() {
return es.merge(builds.map(build =>
es.merge(build.bundles.map(bundle =>
gulp.src(path.join(DIST, build.package, 'lib', bundle.entry))
.pipe(buildDist(bundle.output + '.min.js', bundle, /* isProduction */ true))
.pipe(header(PRODUCTION_HEADER))
.pipe(gulp.dest(path.join(DIST, build.package)))
))
));
});
gulp.task('watch', function() {
gulp.watch(PACKAGES + '/**/*.js', ['exports', 'bundles']);
});
gulp.task('default', function(cb) {
runSequence('clean', ['exports', 'bins', 'bundles', 'bundles:min'], cb);
});