forked from silverstripe/silverstripe-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
527 lines (487 loc) · 16 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
const packageJson = require('./package.json');
const autoprefixer = require('autoprefixer');
const babelify = require('babelify'); // eslint-disable-line no-unused-vars
const browserify = require('browserify');
const eventStream = require('event-stream');
const glob = require('glob');
const gulp = require('gulp');
const coffee = require('gulp-coffee');
const concat = require('gulp-concat');
const merge = require('merge-stream');
const order = require('gulp-order');
const babel = require('gulp-babel');
const diff = require('gulp-diff');
const gulpif = require('gulp-if');
const notify = require('gulp-notify');
const postcss = require('gulp-postcss');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const gulpUtil = require('gulp-util');
const path = require('path');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const semver = require('semver');
const sprity = require('sprity');
const watchify = require('watchify');
const flatten = require('gulp-flatten');
const isDev = typeof process.env.npm_config_development !== 'undefined';
process.env.NODE_ENV = isDev ? 'development' : 'production';
const PATHS = {
MODULES: './node_modules',
ADMIN: './admin',
ADMIN_IMAGES: './admin/client/dist/images',
ADMIN_CSS_SRC: './admin/client/src/styles',
ADMIN_CSS_DIST: './admin/client/dist/styles',
ADMIN_THIRDPARTY: './admin/thirdparty',
ADMIN_JS_SRC: './admin/client/src',
ADMIN_JS_DIST: './admin/client/dist/js',
ADMIN_SPRITES_SRC: './admin/client/src/sprites',
ADMIN_SPRITES_DIST: './admin/client/dist/images/sprites',
FRAMEWORK: '.',
FRAMEWORK_CSS_SRC: './client/src/styles',
FRAMEWORK_CSS_DIST: './client/dist/styles',
FRAMEWORK_THIRDPARTY: './thirdparty',
INSTALL_CSS_SRC: './dev/install/client/src/styles',
INSTALL_CSS_DIST: './dev/install/client/dist/styles',
FRAMEWORK_JS_SRC: './client/src',
FRAMEWORK_JS_DIST: './client/dist/js',
};
// Map of *.scss locations to their compile target folders
const scssFolders = {
[PATHS.FRAMEWORK_CSS_SRC]: PATHS.FRAMEWORK_CSS_DIST,
[PATHS.ADMIN_CSS_SRC]: PATHS.ADMIN_CSS_DIST,
[PATHS.INSTALL_CSS_SRC]: PATHS.INSTALL_CSS_DIST,
};
const browserifyOptions = {
debug: true,
paths: [PATHS.ADMIN_JS_SRC, PATHS.FRAMEWORK_JS_SRC],
};
const babelifyOptions = {
presets: ['es2015', 'es2015-ie', 'react'],
plugins: ['transform-object-assign', 'transform-object-rest-spread'],
ignore: /(node_modules|thirdparty)/,
comments: false,
};
const uglifyOptions = {
mangle: false,
};
// Used for autoprefixing css properties (same as Bootstrap Aplha.2 defaults)
const supportedBrowsers = [
'Chrome >= 35',
'Firefox >= 31',
'Edge >= 12',
'Explorer >= 9',
'iOS >= 8',
'Safari >= 8',
'Android 2.3',
'Android >= 4',
'Opera >= 12',
];
const blueimpFileUploadConfig = {
src: `${PATHS.MODULES}/blueimp-file-upload`,
dest: `${PATHS.FRAMEWORK_THIRDPARTY}/jquery-fileupload`,
files: [
'/cors/jquery.postmessage-transport.js',
'/cors/jquery.xdr-transport.js',
'/jquery.fileupload-ui.js',
'/jquery.fileupload.js',
'/jquery.iframe-transport.js',
],
};
const blueimpLoadImageConfig = {
src: `${PATHS.MODULES}/blueimp-load-image`,
dest: `${PATHS.FRAMEWORK_THIRDPARTY}/javascript-loadimage`,
files: ['/load-image.js'],
};
const blueimpTmplConfig = {
src: `${PATHS.MODULES}/blueimp-tmpl`,
dest: `${PATHS.FRAMEWORK_THIRDPARTY}/javascript-templates`,
files: ['/tmpl.js'],
};
const jquerySizesConfig = {
src: `${PATHS.MODULES}/jquery-sizes`,
dest: `${PATHS.ADMIN_THIRDPARTY}/jsizes`,
files: ['/lib/jquery.sizes.js'],
};
const tinymceConfig = {
src: `${PATHS.MODULES}/tinymce`,
dest: `${PATHS.FRAMEWORK_THIRDPARTY}/tinymce`,
files: [
'/tinymce.min.js', // Exclude unminified file to keep repository size down
'/jquery.tinymce.min.js',
'/themes/**',
'/skins/**',
'/plugins/**',
],
};
/**
* Copies files from a source directory to a destination directory.
*
* @param object libConfig
* @param string libConfig.src - The source directory
* @param string libConfig.dest - The destination directory
* @param array libConfig.files - The list of files to copy from the source to
* the destination directory
*/
function copyFiles(libConfig) {
libConfig.files.forEach((file) => {
const dir = path.parse(file).dir;
gulp.src(libConfig.src + file)
.pipe(gulp.dest(libConfig.dest + dir));
});
}
/**
* Diffs files in a source directory against a destination directory.
*
* @param object libConfig
* @param string libConfig.src - The source directory
* @param string libConfig.dest - The destination directory
* @param array libConfig.files - The list of files to copy from the source
* to the destination directory
*/
function diffFiles(libConfig) {
libConfig.files.forEach((file) => {
const dir = path.parse(file).dir;
gulp.src(libConfig.src + file)
.pipe(diff(libConfig.dest + dir))
.pipe(diff.reporter({ fail: true, quiet: true }))
.on('error', () => {
console.error(new Error( // eslint-disable-line
`Sanity check failed. ${libConfig.dest}${file} has been modified.`
));
process.exit(1);
});
});
}
/**
* Transforms the passed JavaScript files to UMD modules.
*
* @param array files - The files to transform.
* @param string dest - The output directory.
* @return object
*/
function transformToUmd(files, dest) {
return eventStream.merge(files.map((file) => { // eslint-disable-line
return gulp.src(file)
.pipe(babel({
presets: ['es2015'],
moduleId: `ss.${path.parse(file).name}`,
plugins: ['transform-es2015-modules-umd'],
comments: false,
}))
.on('error', notify.onError({
message: 'Error: <%= error.message %>',
}))
.pipe(gulp.dest(dest));
}));
}
// Make sure the version of Node being used is valid.
if (!semver.satisfies(process.versions.node, packageJson.engines.node)) {
console.error( // eslint-disable-line
`Invalid Node.js version. You need to be using ${packageJson.engines.node}. ` +
'If you want to manage multiple Node.js versions try https://github.com/creationix/nvm'
);
process.exit(1);
}
if (isDev) {
browserifyOptions.cache = {};
browserifyOptions.packageCache = {};
browserifyOptions.plugin = [watchify];
}
gulp.task('build', ['umd', 'bundle']);
gulp.task('bundle', ['bundle-lib', 'bundle-legacy', 'bundle-framework']);
gulp.task('bundle-lib', function bundleLib() {
const bundleFileName = 'bundle-lib.js';
const es6 = browserify(Object.assign({}, browserifyOptions,
{ entries: `${PATHS.ADMIN_JS_SRC}/bundles/lib.js` }
))
.on('update', bundleLib)
.on('log', (msg) =>
gulpUtil.log('Finished', `bundled ${bundleFileName} ${msg}`)
)
.transform('babelify', babelifyOptions)
.require('deep-freeze-strict',
{ expose: 'deep-freeze-strict' }
)
.require('react',
{ expose: 'react' }
)
.require('react-addons-css-transition-group',
{ expose: 'react-addons-css-transition-group' }
)
.require('react-addons-test-utils',
{ expose: 'react-addons-test-utils' }
)
.require('react-dom',
{ expose: 'react-dom' }
)
.require('react-redux',
{ expose: 'react-redux' }
)
.require('redux',
{ expose: 'redux' }
)
.require('redux-thunk',
{ expose: 'redux-thunk' }
)
.require('react-router',
{ expose: 'react-router' }
)
.require('react-router-redux',
{ expose: 'react-router-redux' }
)
.require('page.js',
{ expose: 'page.js' }
)
.require(`${PATHS.MODULES}/bootstrap/dist/js/umd/collapse.js`,
{ expose: 'bootstrap-collapse' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Form/Form`,
{ expose: 'components/Form/Form' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Form/FormConstants`,
{ expose: 'components/Form/FormConstants' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/FormAction/FormAction`,
{ expose: 'components/FormAction/FormAction' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/FormBuilder/FormBuilder`,
{ expose: 'components/FormBuilder/FormBuilder' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridField`,
{ expose: 'components/GridField/GridField' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridFieldCell`,
{ expose: 'components/GridField/GridFieldCell' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridFieldHeader`,
{ expose: 'components/GridField/GridFieldHeader' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridFieldHeaderCell`,
{ expose: 'components/GridField/GridFieldHeaderCell' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridFieldRow`,
{ expose: 'components/GridField/GridFieldRow' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridFieldTable`,
{ expose: 'components/GridField/GridFieldTable' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/HiddenField/HiddenField`,
{ expose: 'components/HiddenField/HiddenField' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/TextField/TextField`,
{ expose: 'components/TextField/TextField' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Toolbar/Toolbar`,
{ expose: 'components/Toolbar/Toolbar' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Breadcrumb/Breadcrumb`,
{ expose: 'components/Breadcrumb/Breadcrumb' }
)
.require(`${PATHS.ADMIN_JS_SRC}/state/breadcrumbs/BreadcrumbsActions`,
{ expose: 'state/breadcrumbs/BreadcrumbsActions' }
)
.require(`${PATHS.FRAMEWORK_JS_SRC}/i18n.js`,
{ expose: 'i18n' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/Config`,
{ expose: 'lib/Config' }
)
.require(`${PATHS.FRAMEWORK_JS_SRC}/jQuery.js`,
{ expose: 'jQuery' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/ReducerRegister.js`,
{ expose: 'lib/ReducerRegister' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/ReactRouteRegister.js`,
{ expose: 'lib/ReactRouteRegister' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/Injector.js`,
{ expose: 'lib/Injector' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/Router.js`,
{ expose: 'lib/Router' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/SilverStripeComponent`,
{ expose: 'lib/SilverStripeComponent' }
)
.require(`${PATHS.ADMIN_JS_SRC}/lib/Backend`,
{ expose: 'lib/Backend' }
)
.bundle()
.on('error', notify.onError({ message: `${bundleFileName}: <%= error.message %>` }))
.pipe(source(bundleFileName))
.pipe(buffer());
const chosen = gulp.src([
`${PATHS.MODULES}/chosen/coffee/lib/*.coffee`,
`${PATHS.MODULES}/chosen/coffee/chosen.jquery.coffee`,
])
.pipe(concat('chosen.js'))
.pipe(coffee());
return merge(es6, chosen)
.pipe(order([`**/${bundleFileName}`, '**/chosen.js']))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(concat(bundleFileName, { newLine: '\r\n;\r\n' }))
.pipe(uglify(uglifyOptions))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(PATHS.ADMIN_JS_DIST));
});
gulp.task('bundle-legacy', function bundleLeftAndMain() {
const bundleFileName = 'bundle-legacy.js';
return browserify(Object.assign({}, browserifyOptions,
{ entries: `${PATHS.ADMIN_JS_SRC}/bundles/legacy.js` }
))
.on('update', bundleLeftAndMain)
.on('log', (msg) =>
gulpUtil.log('Finished', `bundled ${bundleFileName} ${msg}`)
)
.transform('babelify', babelifyOptions)
.external('config')
.external('jQuery')
.external('i18n')
.external('lib/Router')
.bundle()
.on('update', bundleLeftAndMain)
.on('error', notify.onError({ message: `${bundleFileName}: <%= error.message %>` }))
.pipe(source(bundleFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify(uglifyOptions))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(PATHS.ADMIN_JS_DIST));
});
gulp.task('bundle-framework', function bundleBoot() {
const bundleFileName = 'bundle-framework.js';
return browserify(Object.assign({}, browserifyOptions,
{ entries: `${PATHS.ADMIN_JS_SRC}/boot/index.js` }
))
.on('update', bundleBoot)
.on('log', (msg) => {
gulpUtil.log('Finished', `bundled ${bundleFileName} ${msg}`);
})
.transform('babelify', babelifyOptions)
.external('bootstrap-collapse')
.external('components/Breadcrumb/Breadcrumb')
.external('state/breadcrumbs/BreadcrumbsActions')
.external('components/FormAction/FormAction')
.external('components/FormBuilder/FormBuilder')
.external('components/GridField/GridField')
.external('components/Toolbar/Toolbar')
.external('deep-freeze-strict')
.external('i18n')
.external('jQuery')
.external('lib/Backend')
.external('lib/ReducerRegister')
.external('lib/ReactRouteRegister')
.external('lib/SilverStripeComponent')
.external('page.js')
.external('react-addons-test-utils')
.external('react-dom')
.external('react-redux')
.external('react-router-redux')
.external('react-router')
.external('react')
.external('redux-thunk')
.external('redux')
.bundle()
.on('update', bundleBoot)
.on('error', notify.onError({ message: `${bundleFileName}: <%= error.message %>` }))
.pipe(source(bundleFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify(uglifyOptions))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(PATHS.ADMIN_JS_DIST));
});
gulp.task('sanity', () => {
diffFiles(blueimpFileUploadConfig);
diffFiles(blueimpLoadImageConfig);
diffFiles(blueimpTmplConfig);
diffFiles(jquerySizesConfig);
diffFiles(tinymceConfig);
});
gulp.task('thirdparty', () => {
copyFiles(blueimpFileUploadConfig);
copyFiles(blueimpLoadImageConfig);
copyFiles(blueimpTmplConfig);
copyFiles(jquerySizesConfig);
copyFiles(tinymceConfig);
});
gulp.task('umd', ['umd-admin', 'umd-framework'], () => {
if (isDev) {
gulp.watch(`${PATHS.ADMIN_JS_SRC}/legacy/*.js`, ['umd-admin']);
gulp.watch(`${PATHS.FRAMEWORK_JS_SRC}/*.js`, ['umd-framework']);
}
});
gulp.task('umd-admin', () => {
const files = glob.sync(
`${PATHS.ADMIN_JS_SRC}/legacy/*.js`,
{ ignore: `${PATHS.ADMIN_JS_SRC}/LeftAndMain.!(Ping).js` }
);
return transformToUmd(files, PATHS.ADMIN_JS_DIST);
});
gulp.task('umd-framework', () => { // eslint-disable-line
return transformToUmd(glob.sync(
`${PATHS.FRAMEWORK_JS_SRC}/*.js`),
PATHS.FRAMEWORK_JS_DIST
);
});
/*
* Takes individual images and compiles them together into sprites
*/
gulp.task('sprites', () => { // eslint-disable-line
return sprity.src({
src: `${PATHS.ADMIN_SPRITES_SRC}/**/*.{png,jpg}`,
cssPath: '../images/sprites',
style: './_sprity.scss',
processor: 'sass',
split: true,
margin: 0,
})
.pipe(
gulpif(
'*.png',
gulp.dest(PATHS.ADMIN_SPRITES_DIST),
gulp.dest(`${PATHS.ADMIN_CSS_SRC}/legacy`)
)
);
});
gulp.task('css', ['compile:css'], () => {
if (isDev) {
Object.keys(scssFolders).forEach((sourceFolder) => {
gulp.watch(`${sourceFolder}/**/*.scss`, ['compile:css']);
});
}
});
/*
* Compiles scss into css
* Watches for changes if --development flag is given
*/
gulp.task('compile:css', () => {
const tasks = Object.keys(scssFolders).map((sourceFolder) => { // eslint-disable-line
const targetFolder = scssFolders[sourceFolder];
return gulp.src(`${sourceFolder}/**/*.scss`)
.pipe(sourcemaps.init())
.pipe(
sass({
outputStyle: 'compressed',
importer: (url, prev, done) => {
if (url.match(/^compass\//)) {
done({ file: 'client/src/styles/_compasscompat.scss' });
} else {
done();
}
},
})
.on('error', notify.onError({
message: 'Error: <%= error.message %>',
}))
)
.pipe(postcss([autoprefixer({ browsers: supportedBrowsers })]))
.pipe(sourcemaps.write())
.pipe(flatten()) // avoid legacy/ paths in CSS output
.pipe(gulp.dest(targetFolder));
});
return tasks;
});