forked from facebook/relay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
177 lines (165 loc) · 5.26 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
/**
* 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-runtime/helpers/extends': 'babel-runtime/helpers/extends',
'babel-runtime/core-js/promise': 'fbjs/lib/Promise',
'babel-runtime/core-js/json/stringify': 'babel-runtime/core-js/json/stringify',
'babel-runtime/helpers/classCallCheck': 'babel-runtime/helpers/classCallCheck',
'babel-runtime/helpers/possibleConstructorReturn': 'babel-runtime/helpers/possibleConstructorReturn',
'babel-runtime/helpers/inherits': 'babel-runtime/helpers/inherits',
'babel-runtime/core-js/object/assign': 'babel-runtime/core-js/object/assign',
'babel-runtime/core-js/object/keys': 'babel-runtime/core-js/object/keys',
'babel-runtime/core-js/object/get-own-property-names': 'babel-runtime/core-js/object/get-own-property-names',
'babel-runtime/core-js/array/from': 'babel-runtime/core-js/array/from',
'babel-runtime/core-js/object/freeze': 'babel-runtime/core-js/object/freeze',
'babel-runtime/core-js/object/is-frozen': 'babel-runtime/core-js/object/is-frozen',
'babel-runtime/core-js/map': 'babel-runtime/core-js/map',
'babel-runtime/core-js/weak-map': 'babel-runtime/core-js/weak-map',
'babel-runtime/helpers/defineProperty': 'babel-runtime/helpers/defineProperty',
'React': 'react',
'ReactDOM': 'react-dom',
'ReactNative': 'react-native',
'StaticContainer.react': 'react-static-container',
},
plugins: [
'transform-runtime',
],
});
const del = require('del');
const derequire = require('gulp-derequire');
const flatten = require('gulp-flatten');
const gulp = require('gulp');
const gulpUtil = require('gulp-util');
const header = require('gulp-header');
const runSequence = require('run-sequence');
const webpackStream = require('webpack-stream');
const DEVELOPMENT_HEADER = [
'/**',
' * Relay v<%= version %>',
' */',
].join('\n') + '\n';
const PRODUCTION_HEADER = [
'/**',
' * Relay v<%= 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(opts) {
const webpackOpts = {
debug: opts.debug,
externals: {
'react': 'React',
'react-dom': 'ReactDOM',
},
output: {
filename: opts.output,
libraryTarget: 'umd',
library: 'Relay',
},
plugins: [
new webpackStream.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(
opts.debug ? 'development' : 'production'
),
}),
new webpackStream.webpack.optimize.OccurenceOrderPlugin(),
new webpackStream.webpack.optimize.DedupePlugin(),
],
};
if (!opts.debug) {
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());
}
});
};
const paths = {
dist: 'dist',
entry: 'lib/Relay.js',
lib: 'lib',
src: [
'*src/**/*.js',
'!src/**/__tests__/**/*.js',
'!src/**/__mocks__/**/*.js',
],
};
gulp.task('clean', function() {
return del([paths.dist, paths.lib]);
});
gulp.task('modules', function() {
return gulp
.src(paths.src)
.pipe(babel(babelOptions))
.pipe(flatten())
.pipe(gulp.dest(paths.lib));
});
gulp.task('dist', ['modules'], function() {
const distOpts = {
debug: true,
output: 'relay.js',
};
return gulp.src(paths.entry)
.pipe(buildDist(distOpts))
.pipe(derequire())
.pipe(header(DEVELOPMENT_HEADER, {
version: process.env.npm_package_version,
}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('dist:min', ['modules'], function() {
const distOpts = {
debug: false,
output: 'relay.min.js',
};
return gulp.src(paths.entry)
.pipe(buildDist(distOpts))
.pipe(header(PRODUCTION_HEADER, {
version: process.env.npm_package_version,
}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('website:check-version', function(cb) {
const version = require('./package').version;
const websiteVersion = require('./website/core/SiteData').version;
if (websiteVersion !== version) {
return cb(
new Error('Website version does not match package.json. Saw ' + websiteVersion + ' but expected ' + version)
);
}
cb();
});
gulp.task('watch', function() {
gulp.watch(paths.src, ['modules']);
});
gulp.task('default', function(cb) {
runSequence('clean', 'website:check-version', ['dist', 'dist:min'], cb);
});