forked from immutable-js/immutable-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
316 lines (283 loc) · 8.51 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
315
316
var browserify = require('browserify');
var browserSync = require('browser-sync');
var buffer = require('vinyl-buffer');
var child_process = require('child_process');
var concat = require('gulp-concat');
var del = require('del');
var filter = require('gulp-filter');
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var header = require('gulp-header');
var Immutable = require('./');
var less = require('gulp-less');
var mkdirp = require('mkdirp');
var path = require('path');
var React = require('react/addons');
var reactTools = require('react-tools');
var sequence = require('run-sequence');
var size = require('gulp-size');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var uglify = require('gulp-uglify');
var vm = require('vm');
function requireFresh(path) {
delete require.cache[require.resolve(path)];
return require(path);
}
var SRC_DIR = './pages/src/';
var BUILD_DIR = './pages/out/';
gulp.task('clean', function (done) {
return del([
BUILD_DIR,
], {force: true});
});
gulp.task('readme', function() {
var genMarkdownDoc = requireFresh('./pages/lib/genMarkdownDoc');
var readmePath = path.join(__dirname, './README.md');
var fileContents = fs.readFileSync(readmePath, 'utf8');
var writePath = path.join(__dirname, './pages/generated/readme.json');
var contents = JSON.stringify(genMarkdownDoc(fileContents));
mkdirp.sync(path.dirname(writePath));
fs.writeFileSync(writePath, contents);
});
gulp.task('typedefs', function() {
var genTypeDefData = requireFresh('./pages/lib/genTypeDefData');
var typeDefPath = path.join(__dirname, './type-definitions/Immutable.d.ts');
var fileContents = fs.readFileSync(typeDefPath, 'utf8');
var fileSource = fileContents.replace(
'module \'immutable\'',
'module Immutable'
);
var writePath = path.join(__dirname, './pages/generated/immutable.d.json');
var contents = JSON.stringify(genTypeDefData(typeDefPath, fileSource));
mkdirp.sync(path.dirname(writePath));
fs.writeFileSync(writePath, contents);
});
gulp.task('js', gulpJS(''));
gulp.task('js-docs', gulpJS('docs/'));
function gulpJS(subDir) {
var reactGlobalModulePath = path.relative(
path.resolve(SRC_DIR+subDir),
path.resolve('./resources/react-global.js')
);
var immutableGlobalModulePath = path.relative(
path.resolve(SRC_DIR+subDir),
path.resolve('./resources/immutable-global.js')
);
return function() {
return browserify({
debug: true,
basedir: SRC_DIR+subDir,
})
.add('./src/index.js')
.require('./src/index.js')
.require(reactGlobalModulePath, { expose: 'react' })
.require(immutableGlobalModulePath, { expose: 'immutable' })
// Helpful when developing with no wifi
// .require('react', { expose: 'react' })
// .require('immutable', { expose: 'immutable' })
.transform(reactTransformify)
.bundle()
.on('error', handleError)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true,
}))
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest(BUILD_DIR+subDir))
.pipe(filter('**/*.js'))
.pipe(size({ showFiles: true }))
.on('error', handleError);
}
}
gulp.task('pre-render', gulpPreRender(''));
gulp.task('pre-render-docs', gulpPreRender('docs/'));
function gulpPreRender(subDir) {
return function () {
return gulp.src(SRC_DIR+subDir+'index.html')
.pipe(preRender(subDir))
.pipe(size({ showFiles: true }))
.pipe(gulp.dest(BUILD_DIR+subDir))
.on('error', handleError);
}
}
gulp.task('less', gulpLess(''));
gulp.task('less-docs', gulpLess('docs/'));
function gulpLess(subDir) {
return function () {
return gulp.src(SRC_DIR+subDir+'src/*.less')
.pipe(sourcemaps.init())
.pipe(less({
compress: true
}))
.on('error', handleError)
.pipe(concat('bundle.css'))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest(BUILD_DIR+subDir))
.pipe(filter('**/*.css'))
.pipe(size({ showFiles: true }))
.pipe(browserSync.reload({ stream:true }))
.on('error', handleError);
}
}
gulp.task('statics', gulpStatics(''));
gulp.task('statics-docs', gulpStatics('docs/'));
function gulpStatics(subDir) {
return function() {
return gulp.src(SRC_DIR+subDir+'static/**/*')
.pipe(gulp.dest(BUILD_DIR+subDir+'static'))
.on('error', handleError)
.pipe(browserSync.reload({ stream:true }))
.on('error', handleError);
}
}
gulp.task('build', function (done) {
sequence(
['typedefs'],
['readme'],
['js', 'js-docs', 'less', 'less-docs', 'statics', 'statics-docs'],
['pre-render', 'pre-render-docs'],
done
);
});
gulp.task('default', function (done) {
sequence('clean', 'build', done);
});
// watch files for changes and reload
gulp.task('dev', ['default'], function() {
browserSync({
port: 8040,
server: {
baseDir: BUILD_DIR
}
});
gulp.watch('./README.md', ['build']);
gulp.watch('./pages/lib/**/*.js', ['build']);
gulp.watch('./pages/src/**/*.less', ['less', 'less-docs']);
gulp.watch('./pages/src/src/**/*.js', ['rebuild-js']);
gulp.watch('./pages/src/docs/src/**/*.js', ['rebuild-js-docs']);
gulp.watch('./pages/src/**/*.html', ['pre-render', 'pre-render-docs']);
gulp.watch('./pages/src/static/**/*', ['statics', 'statics-docs']);
gulp.watch('./type-definitions/*', function () {
sequence('typedefs', 'rebuild-js-docs');
});
});
gulp.task('rebuild-js', function (done) {
sequence('js', ['pre-render'], function () {
browserSync.reload();
done();
});
});
gulp.task('rebuild-js-docs', function (done) {
sequence('js-docs', ['pre-render-docs'], function () {
browserSync.reload();
done();
});
});
function handleError(error) {
gutil.log(error.message);
}
function preRender(subDir) {
return through.obj(function(file, enc, cb) {
var src = file.contents.toString(enc);
var components = [];
src = src.replace(
/<!--\s*React\(\s*(.*)\s*\)\s*-->/g,
function (_, component) {
var id = 'r' + components.length;
components.push(component);
try {
return (
'<div id="' + id + '">'+
vm.runInNewContext(
fs.readFileSync(BUILD_DIR+subDir+'bundle.js') + // ugly
'\nrequire("react").renderToString('+
'require("react").createElement(require(component)))',
{
global: {
React: React,
Immutable: Immutable
},
window: {},
component: component,
console: console,
}
) +
'</div>'
);
} catch (error) {
return '<div id="' + id + '">' + error.message + '</div>';
}
}
);
if (components.length) {
src = src.replace(
/<!--\s*ReactRender\(\)\s*-->/g,
'<script>' + components.map(function (component, index) {
return (
'var React = require("react");'+
'React.render('+
'React.createElement(require("'+component+'")),'+
'document.getElementById("r' + (index) + '")'+
');'
);
}) + '</script>'
);
}
file.contents = new Buffer(src, enc);
this.push(file);
cb();
});
}
function reactTransform() {
var parseError;
return through.obj(function(file, enc, cb) {
if (path.extname(file.path) !== '.js') {
this.push(file);
return cb();
}
try {
file.contents = new Buffer(reactTools.transform(
file.contents.toString(enc),
{harmony: true}
), enc);
this.push(file);
cb();
} catch (error) {
parseError = new gutil.PluginError('transform', {
message: file.relative + ' : ' + error.message,
showStack: false
});
cb();
}
}, function (done) {
parseError && this.emit('error', parseError);
done();
});
}
function reactTransformify(filePath) {
if (path.extname(filePath) !== '.js') {
return through();
}
var code = '';
var parseError;
return through.obj(function(file, enc, cb) {
code += file;
cb();
}, function (done) {
try {
this.push(reactTools.transform(code, {harmony:true}));
} catch (error) {
parseError = new gutil.PluginError('transform', {
message: error.message,
showStack: false
});
}
parseError && this.emit('error', parseError);
done();
});
}