This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
forked from ashevtcov/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
236 lines (217 loc) · 5.95 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
/**
* 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';
var babel = require('gulp-babel');
var del = require('del');
var cleanCSS = require('gulp-clean-css');
var concatCSS = require('gulp-concat-css');
var derequire = require('gulp-derequire');
var flatten = require('gulp-flatten');
var gulp = require('gulp');
var gulpUtil = require('gulp-util');
var header = require('gulp-header');
var packageData = require('./package.json');
var rename = require('gulp-rename');
var runSequence = require('run-sequence');
var through = require('through2');
var webpackStream = require('webpack-stream');
var fbjsConfigurePreset = require('babel-preset-fbjs/configure');
var gulpCheckDependencies = require('fbjs-scripts/gulp/check-dependencies');
var moduleMap = require('./scripts/module-map');
var paths = {
dist: 'dist',
lib: 'lib',
src: [
'src/**/*.js',
'!src/**/__tests__/**/*.js',
'!src/**/__mocks__/**/*.js',
],
css: [
'src/**/*.css',
],
};
var babelOptsJS = {
presets: [
fbjsConfigurePreset({
stripDEV: true,
rewriteModules: {map: moduleMap},
}),
],
};
var babelOptsFlow = {
presets: [
fbjsConfigurePreset({
target: 'flow',
rewriteModules: {map: moduleMap},
}),
],
};
var COPYRIGHT_HEADER = `/**
* Draft 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.
*/
`;
var buildDist = function(opts) {
var webpackOpts = {
debug: opts.debug,
externals: {
immutable: {
root: 'Immutable',
commonjs2: 'immutable',
commonjs: 'immutable',
amd: 'immutable',
},
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom',
},
},
output: {
filename: opts.output,
libraryTarget: 'umd',
library: 'Draft',
},
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) {
gulpUtil.log('webpack', '\n' + stats.toString({colors: true}));
}
});
};
gulp.task('clean', function() {
return del([paths.dist, paths.lib]);
});
gulp.task('modules', function() {
return gulp
.src(paths.src)
.pipe(babel(babelOptsJS))
.pipe(flatten())
.pipe(gulp.dest(paths.lib));
});
gulp.task('flow', function() {
return gulp
.src(paths.src)
.pipe(babel(babelOptsFlow))
.pipe(flatten())
.pipe(rename({extname: '.js.flow'}))
.pipe(gulp.dest(paths.lib));
});
gulp.task('css', function() {
return gulp
.src(paths.css)
.pipe(through.obj(function(file, encoding, callback) {
var contents = file.contents.toString();
var replaced = contents.replace(
// Regex based on MakeHasteCssModuleTransform: ignores comments,
// strings, and URLs
/\/\*.*?\*\/|'(?:\\.|[^'])*'|"(?:\\.|[^"])*"|url\([^)]*\)|(\.(?:public\/)?[\w-]*\/{1,2}[\w-]+)/g,
function(match, cls) {
if (cls) {
return cls.replace(/\//g, '-');
} else {
return match;
}
}
);
replaced = replaced.replace(
// MakeHasteCssVariablesTransform
/\bvar\(([\w-]+)\)/g,
function(match, name) {
var vars = {
'fig-secondary-text': '#9197a3',
'fig-light-20': '#bdc1c9',
};
if (vars[name]) {
return vars[name];
} else {
throw new Error('Unknown CSS variable ' + name);
}
}
);
file.contents = new Buffer(replaced);
callback(null, file);
}))
.pipe(concatCSS('Draft.css'))
// Avoid rewriting rules *just in case*, just compress
.pipe(cleanCSS({advanced: false}))
.pipe(header(COPYRIGHT_HEADER, {version: packageData.version}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('dist', ['modules', 'css'], function() {
var opts = {
debug: true,
output: 'Draft.js',
};
return gulp.src('./lib/Draft.js')
.pipe(buildDist(opts))
.pipe(derequire())
.pipe(header(COPYRIGHT_HEADER, {version: packageData.version}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('dist:min', ['modules'], function() {
var opts = {
debug: false,
output: 'Draft.min.js',
};
return gulp.src('./lib/Draft.js')
.pipe(buildDist(opts))
.pipe(header(COPYRIGHT_HEADER, {version: packageData.version}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('check-dependencies', function() {
return gulp
.src('package.json')
.pipe(gulpCheckDependencies());
});
gulp.task('watch', function() {
gulp.watch(paths.src, ['modules']);
});
gulp.task('dev', function() {
gulp.watch(paths.src, ['modules', 'dist']);
});
gulp.task('default', function(cb) {
runSequence('check-dependencies', 'clean', ['modules', 'flow'], ['dist', 'dist:min'], cb);
});