-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
147 lines (126 loc) · 4.2 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
/*============================================*\
* Require Gulp Modules
\*============================================*/
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const es6ify = require('es6ify');
const uglify = require('gulp-uglifyjs');
const browserify = require('browserify');
const filter = require('gulp-filter');
const extractor = require('gulp-extract-sourcemap')
const del = require('del');
const glob = require('glob');
const path = require('path');
const isparta = require('isparta');
const watchify = require('watchify');
const vinylBuffer = require('vinyl-buffer');
const runSequence = require('run-sequence');
const vinylSource = require('vinyl-source-stream');
const browserSync = require('browser-sync');
const manifest = require('./package.json');
const destinationFolder = './dist';
const builds = require('./src/packages.json');
const fs = require('fs');
const mkdirp = require('mkdirp');
const config = manifest.babelBoilerplateOptions;
/*============================================*\
* Task Definitions
\*============================================*/
gulp.task('clean', del.bind(undefined, [destinationFolder]));
// Send a notification when JSCS fails,
// so that you know your changes didn't build
function jscsNotify(file) {
if (!file.jscs) { return; }
return file.jscs.success ? false : 'JSCS failed';
}
function createLintTask(taskName, files) {
gulp.task(taskName, function() {
return gulp.src(files)
.pipe($.plumber())
.pipe($.jscs())
.pipe($.notify(jscsNotify));
});
}
createLintTask('lint-src', ['src/**/*.js']);
createLintTask('lint-test', ['test/**/*.js']);
var _builds = [];
builds.forEach(function (build) {
var key = build.key.toLowerCase();
// Clean the target folder
gulp.task('clean-' + key, del.bind(undefined, [destinationFolder + '/' + key]));
// Clean the source and place in the target folder
gulp.task('build-' + key, ['lint-src', 'clean-' + key], buildComplete.bind(undefined, build));
// Watch all the JS files and run lint and build when a change is made.
gulp.task('watch-' + key, ['lint-src', 'build-' + key], function () {
gulp.watch('src/**/*.js', ['build-' + key]);
});
// Serve, watch, lint, and build the
gulp.task('serve-' + key, ['lint-src', 'build-' + key], function () {
browserSync({
notify: false,
port: 3030,
server: {
baseDir: ['demos/' + key],
routes: {
'/shared': 'demos/shared',
'/dist': 'dist/' + key,
'/bower_components': 'bower_components'
}
}
});
gulp.watch(['src/**/*.js', './demos/' + key], ['lint-src', 'build-' + key]).on('change', browserSync.reload);;
});
});
gulp.task('build', builds.map(function (b) {return 'build-' + b.key.toLowerCase()}));
function buildComplete(build, done) {
_builds.push(build);
var targetDir = destinationFolder + '/' + build.key.toLowerCase();
var exchange = {
source_map: {
file: build.key.toLowerCase() + '.min.js.map',
root: '/',
orig: ''
}
};
mkdirp.sync(targetDir);
browserify({
debug: true
})
.add(es6ify.runtime)
.transform(es6ify)
.add(build.entry)
.bundle()
.pipe(vinylSource(build.key.toLowerCase() + '.js'))
.pipe(vinylBuffer())
.pipe(extractor({
basedir: destinationFolder,
removeSourcesContent: true
}))
.on('postextract', function(sourceMap){
exchange.source_map.orig = sourceMap;
fs.writeFileSync(targetDir + '/' + build.key.toLowerCase() + '.min.js');
})
.pipe( filter('**/*.js') )
.pipe(gulp.dest(targetDir))
.pipe(uglify(build.key.toLowerCase() + '.min.js', {
outSourceMap: true,
basePath: destinationFolder,
output: {
source_map: exchange.source_map
}
}))
.pipe(gulp.dest(targetDir))
.on('end', done);
}
gulp.task('list', function () {
console.log('Run build-[key] to build demo for production.');
console.log('Run serve-[key] to serve demo in browser.');
console.log('Run watch-[key] to build and watch the demo for changes.');
console.log('Keys:');
var l = builds.map(function (build) {
return build.key;
});
console.log(l.join('\n'));
});
// An alias of test
gulp.task('default', ['test']);