forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
480 lines (414 loc) · 12.7 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
/** Regular npm dependendencies */
var _ = require('lodash');
var argv = require('minimist')(process.argv.slice(2));
var changelog = require('conventional-changelog');
var fs = require('fs');
var glob = require('glob').sync;
var gulp = require('gulp');
var karma = require('karma').server;
var lazypipe = require('lazypipe');
var mergeStream = require('merge-stream');
var path = require('path');
var pkg = require('./package.json');
var series = require('stream-series');
var through2 = require('through2');
/** Gulp dependencies */
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var filter = require('gulp-filter');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var insert = require('gulp-insert');
var jshint = require('gulp-jshint');
var minifyCss = require('gulp-minify-css');
var ngAnnotate = require('gulp-ng-annotate');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var webserver = require('gulp-webserver');
/** Local dependencies */
var buildConfig = require('./config/build.config');
var utils = require('./scripts/gulp-utils.js');
/** Arguments */
var IS_RELEASE_BUILD = !!argv.release;
var IS_DEMO_BUILD = (!!argv.module || !!argv.m || !!argv.c);
var BUILD_MODE = argv.mode;
var VERSION = argv.version || pkg.version;
/** Grab-bag of build configuration. */
var config = {
banner:
'/*!\n' +
' * Angular Material Design\n' +
' * https://github.com/angular/material\n' +
' * @license MIT\n' +
' * v' + VERSION + '\n' +
' */\n',
jsBaseFiles: [
'src/core/**/*.js',
'!src/core/**/*.spec.js'
],
jsFiles: [
'src/**/*.js'
],
themeBaseFiles: [
'src/core/style/variables.scss',
'src/core/style/mixins.scss'
],
scssBaseFiles: [
'src/core/style/color-palette.scss',
'src/core/style/variables.scss',
'src/core/style/mixins.scss',
'src/core/style/structure.scss',
'src/core/style/layout.scss'
],
scssStandaloneFiles: [
'src/core/style/layout.scss'
],
paths: 'src/{components,services}/**',
outputDir: 'dist/'
};
var LR_PORT = argv.port || argv.p || 8080;
var buildModes = {
'closure': {
transform: utils.addClosurePrefixes,
outputDir: path.join(config.outputDir, 'modules/closure') + path.sep,
useBower: false
},
'demos': {
transform: gutil.noop,
outputDir: path.join(config.outputDir, 'demos') + path.sep,
useBower: false
},
'default': {
transform: gutil.noop,
outputDir: path.join(config.outputDir, 'modules/js') + path.sep,
useBower: true
}
};
IS_DEMO_BUILD && (BUILD_MODE="demos");
BUILD_MODE = buildModes[BUILD_MODE] || buildModes['default'];
if (IS_RELEASE_BUILD) {
console.log(
gutil.colors.red('--release:'),
'Building release version (minified)...'
);
}
require('./docs/gulpfile')(gulp, IS_RELEASE_BUILD);
gulp.task('default', ['build']);
gulp.task('validate', ['jshint', 'karma']);
gulp.task('changelog', function(done) {
changelog({
repository: 'https://github.com/angular/material',
version: VERSION,
file: 'CHANGELOG.md'
}, function(err, log) {
fs.writeFileSync(__dirname + '/CHANGELOG.md', log);
});
});
gulp.task('jshint', function() {
return gulp.src(
config.jsFiles
)
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter(require('jshint-summary')({
fileColCol: ',bold',
positionCol: ',bold',
codeCol: 'green,bold',
reasonCol: 'cyan'
})))
.pipe(jshint.reporter('fail'));
});
/** *****************************************
*
* Tasks for Karma Test
*
** ***************************************** */
gulp.task('karma', function(done) {
var karmaConfig = {
singleRun: true,
autoWatch: false,
browsers : argv.browsers ? argv.browsers.trim().split(',') : ['Chrome'],
configFile: __dirname + '/config/karma.conf.js'
};
gutil.log('Running unit tests on unminified source.');
karma.start(karmaConfig, testMinified);
function testMinified() {
gutil.log('Running unit tests on minified source.');
buildJs(true);
karmaConfig.releaseMode = true;
karma.start(karmaConfig, done);
}
});
gulp.task('karma-watch', function(done) {
karma.start({
singleRun:false,
autoWatch:true,
configFile: __dirname + '/config/karma.conf.js'
},done);
});
gulp.task('karma-sauce', function(done) {
karma.start(require('./config/karma-sauce.conf.js'), done);
});
/** *****************************************
*
* Project-wide Build Tasks
*
** ***************************************** */
gulp.task('build', ['build-scss', 'build-js']);
gulp.task('build-all-modules', function() {
return gulp.src(['src/components/*', 'src/core/'])
.pipe(through2.obj(function(folder, enc, next) {
var moduleId = folder.path.indexOf('components') > -1 ?
'material.components.' + path.basename(folder.path) :
'material.' + path.basename(folder.path);
var stream;
if (IS_RELEASE_BUILD && BUILD_MODE.useBower) {
stream = mergeStream(buildModule(moduleId, true), buildModule(moduleId, false));
} else {
stream = buildModule(moduleId, false);
}
stream.on('end', function() {
next();
});
}));
});
function buildModule(module, isRelease) {
if ( module.indexOf(".") < 0) {
module = "material.components." + module;
}
var name = module.split('.').pop();
gutil.log('Building ' + module + (isRelease && ' minified' || '') + ' ...');
return utils.filesForModule(module)
.pipe(filterNonCodeFiles())
.pipe(gulpif('*.scss', buildModuleStyles(name)))
.pipe(gulpif('*.js', buildModuleJs(name)))
.pipe(BUILD_MODE.transform())
.pipe(insert.prepend(config.banner))
.pipe(gulpif(isRelease, buildMin()))
.pipe(gulp.dest(BUILD_MODE.outputDir + name));
function buildMin() {
return lazypipe()
.pipe(gulpif, /.css$/, minifyCss(), uglify({ preserveComments: 'some' }))
.pipe(rename, function(path) {
path.extname = path.extname
.replace(/.js$/, '.min.js')
.replace(/.css$/, '.min.css');
})
.pipe(utils.buildModuleBower, name, VERSION)
();
}
function buildModuleJs(name) {
return lazypipe()
.pipe(plumber)
.pipe(ngAnnotate)
.pipe(concat, name + '.js')
();
}
function buildModuleStyles(name) {
var files = [];
config.themeBaseFiles.forEach(function(fileGlob) {
files = files.concat(glob(fileGlob, { cwd: __dirname }));
});
var baseStyles = files.map(function(fileName) {
return fs.readFileSync(fileName, 'utf8').toString();
}).join('\n');
return lazypipe()
.pipe(insert.prepend, baseStyles)
.pipe(gulpif, /theme.scss/,
rename(name + '-default-theme.scss'), concat(name + '.scss')
)
.pipe(sass)
.pipe(autoprefix)
(); // invoke the returning fn to create our pipe
}
}
/** *****************************************
*
* Tasks for Watch features
*
* `gulp watch` - Watch, build and deploy all modules and css
* `gulp watch-demo -c textfield` - Watch, build and deploy the 'textfield' demos
* `gulp watch-demo -m material.components.textfield` - Watch, build and deploy the 'textfield' module's demos
*
** ***************************************** */
gulp.task('watch', ['build'], function() {
gulp.watch('src/**/*', ['build']);
});
gulp.task('watch-demo', ['build-demo'], function() {
var module = readModuleArg();
var name = module.split('.').pop();
var dir = "/dist/demos/"+name.trim();
gutil.log('\n',
'-- Rebuilding', dir, 'when source files change...\n',
'--', gutil.colors.green('Hint:'), 'Run',
gutil.colors.cyan('`gulp server`'),
'to start a livereload server in root, then navigate to\n',
'--', gutil.colors.green('"dist/demos/' + name + '/"'), 'in your browser to develop.'
);
return gulp.watch('src/**/*', ['build-demo']);
});
gulp.task('server', function() {
return gulp.src('.')
.pipe(webserver({
livereload: true,
port: LR_PORT,
directoryListing: true
}));
});
/** *****************************************
*
* Tasks and functions for module Javascript
*
** ***************************************** */
gulp.task('build-js', function() {
return buildJs(IS_RELEASE_BUILD);
});
gulp.task('build-js-release', function() {
buildJs(true);
});
/**
* Builds the entire component library javascript.
* @param {boolean} isRelease Whether to build in release mode.
*/
function buildJs(isRelease) {
gutil.log("building js files...");
var jsBuildStream = gulp.src(
config.jsBaseFiles.concat([path.join(config.paths, '*.js')])
)
.pipe(filterNonCodeFiles())
.pipe(utils.buildNgMaterialDefinition())
.pipe(insert.prepend(config.banner))
.pipe(plumber())
.pipe(ngAnnotate());
var themeBuildStream = gulp.src(
config.themeBaseFiles.concat(path.join(config.paths, '*-theme.scss'))
)
.pipe(concat('default-theme.scss'))
.pipe(utils.hoistScssVariables())
.pipe(sass())
.pipe(gulp.dest('dist'))
.pipe(utils.cssToNgConstant('material.core', '$MD_THEME_CSS'));
return series(jsBuildStream, themeBuildStream)
.pipe(concat('angular-material.js'))
.pipe(gulp.dest(config.outputDir))
.pipe(gulpif(isRelease, lazypipe()
.pipe(uglify, { preserveComments: 'some' })
.pipe(rename, { extname: '.min.js' })
.pipe(gulp.dest, config.outputDir)
()
));
}
/** *****************************************
*
* Component Demos Tasks and commands
*
* Build and deploy demos for a module. This is
* useful for debugging a specific module independent of
* the docs build; which has all modules and all demos loaded.
*
* To build, watch for changes, and launch web server :
*
* ```sh
* gulp watch-demo -c button
* gulp server
* ```
*
* To build demo files:
*
* ```sh
* gulp build-demo -c button
* ```
*
** ***************************************** */
gulp.task('build-demo', ['build', 'build-module-demo'], function() {
return buildModule(readModuleArg(), false);
});
gulp.task('build-module-demo', function() {
var mod = readModuleArg();
var name = mod.split('.').pop();
var demoIndexTemplate = fs.readFileSync(
__dirname + '/docs/config/template/demo-index.template.html', 'utf8'
).toString();
gutil.log('Building demos for', mod, '...');
return utils.readModuleDemos(mod, function() {
return lazypipe()
.pipe(gulpif, /.css$/, sass())
.pipe(gulpif, /.css$/, autoprefix())
.pipe(gulp.dest, BUILD_MODE.outputDir + name)
();
})
.pipe(through2.obj(function(demo, enc, next) {
fs.writeFileSync(
path.resolve(BUILD_MODE.outputDir, name, demo.name, 'index.html'),
_.template(demoIndexTemplate, demo)
);
next();
}));
});
/** *****************************************
*
* Tasks and functions for Themes and CSS
*
** ***************************************** */
gulp.task('build-scss', function() {
var scssGlob = path.join(config.paths, '*.scss');
gutil.log("Building css files...");
var streams = [];
streams.push(
gulp.src(config.scssBaseFiles.concat(scssGlob))
.pipe(filterNonCodeFiles())
.pipe(filter(['**', '!**/*-theme.scss'])) // remove once ported
.pipe(concat('angular-material.scss'))
// .pipe(insert.append(defaultThemeContents))
.pipe(sass())
.pipe(autoprefix())
.pipe(insert.prepend(config.banner))
.pipe(gulp.dest(config.outputDir))
.pipe(gulpif(IS_RELEASE_BUILD, lazypipe()
.pipe(minifyCss)
.pipe(rename, {extname: '.min.css'})
.pipe(gulp.dest, config.outputDir)
()
))
);
if (IS_RELEASE_BUILD) {
var baseVars = fs.readFileSync('src/core/style/variables.scss', 'utf8').toString();
streams.push(
gulp.src(config.scssStandaloneFiles)
.pipe(insert.prepend(baseVars))
.pipe(sass())
.pipe(autoprefix())
.pipe(insert.prepend(config.banner))
.pipe(rename({prefix: 'angular-material-'}))
.pipe(gulp.dest(path.join(config.outputDir, 'modules', 'css')))
);
}
return series(streams);
});
/** *****************************************
*
* Internal helper functions
*
** ***************************************** */
function readModuleArg() {
var module = argv.c ? 'material.components.' + argv.c : (argv.module || argv.m);
if (!module) {
gutil.log('\nProvide a compnent argument via `-c`:',
'\nExample: -c toast');
gutil.log('\nOr provide a module argument via `--module` or `-m`.',
'\nExample: --module=material.components.toast or -m material.components.dialog');
process.exit(1);
}
return module;
}
function filterNonCodeFiles() {
return filter(function(file) {
return !/demo|module\.json|\.spec.js|README/.test(file.path);
});
}
function autoprefix() {
return autoprefixer({browsers: [
'last 2 versions', 'last 4 Android versions'
]});
}