forked from RoundingWellOS/backbone.eventrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·190 lines (164 loc) · 5.12 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
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const del = require('del');
const path = require('path');
const rollup = require('rollup').rollup;
const multiEntry = require('rollup-plugin-multi-entry');
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const babel = require('rollup-plugin-babel');
const json = require('rollup-plugin-json');
const preset = require('babel-preset-es2015-rollup');
const manifest = require('./package.json');
const config = manifest.babelBoilerplateOptions;
const mochaGlobals = require('./test/.globals.json').globals;
const mainFile = manifest.main;
const destinationFolder = path.dirname(mainFile);
const exportFileName = path.basename(mainFile, path.extname(mainFile));
// Remove the built files
gulp.task('clean', function(cb) {
del([destinationFolder], cb);
});
// Remove our temporary files
gulp.task('clean-tmp', function(cb) {
del(['tmp'], cb);
});
const onError = $.notify.onError('Error: <%= error.message %>');
function lint(files) {
return gulp.src(files)
.pipe($.plumber(onError))
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError());
}
// Lint our source code
gulp.task('lint-src', function() {
return lint(['src/**/*.js']);
});
// Lint our test code
gulp.task('lint-test', function() {
return lint(['test/**/*.js']);
});
function getBanner() {
const banner = ['/**',
` * ${ manifest.name } - ${ manifest.description }`,
` * @version v${ manifest.version }`,
` * @link ${ manifest.homepage }`,
` * @license ${ manifest.license }`,
' */',
''].join('\n');
return banner;
}
function _generate(bundle, expVarName) {
const intro = getBanner();
return bundle.generate({
format: 'umd',
moduleName: expVarName,
sourceMap: true,
banner: intro,
globals: {
'backbone': 'Backbone',
'underscore': '_',
'backbone.radio': 'Radio'
}
});
}
function bundleCode(entryFileName, expVarName) {
return rollup({
entry: `src/${ entryFileName }.js`,
external: ['underscore', 'backbone', 'backbone.radio'],
plugins: [
babel({
sourceMaps: true,
presets: [preset],
babelrc: false
})
]
}).then(function(bundle) {
return _generate(bundle, expVarName);
}).then(gen => {
gen.code += `\n//# sourceMappingURL=${ gen.map.toUrl() }`;
return gen;
});
}
function _buildLib(entryFileName, destFolder, expFileName, expVarName) {
return bundleCode(entryFileName, expVarName).then(function(gen) {
gen.code = gen.code.replace('<%VERSION%>', manifest.version);
return $.file(`${ expFileName }.js`, gen.code, { src: true })
.pipe($.plumber())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(`${ expFileName }.min.js`))
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.uglify({
preserveComments: 'license'
}))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destFolder));
});
}
gulp.task('build-lib', ['lint-src', 'clean'], function() {
return _buildLib(config.entryFileName, destinationFolder, exportFileName, config.exportVarName);
});
function bundleTest() {
return rollup({
entry: ['./test/setup/browser.js', './test/unit/**/*.js'],
plugins: [
multiEntry.default(),
nodeResolve({ main: true }),
commonjs(),
json(),
babel({
sourceMaps: true,
presets: [preset],
babelrc: false,
exclude: 'node_modules/**'
})
]
}).then(function(bundle) {
return bundle.write({
format: 'iife',
sourceMap: true,
moduleName: 'Tests',
dest: './tmp/__spec-build.js'
});
}).then($.livereload.changed('./tmp/__spec-build.js'));
}
function browserWatch() {
$.livereload.listen({ port: 35729, host: 'localhost', start: true });
gulp.watch(['src/**/*.js', 'test/**/*.js'], ['browser-bundle']);
}
function _registerBabel() {
require('babel-register');
}
gulp.task('coverage', ['lint-src', 'lint-test'], function(done) {
_registerBabel();
gulp.src(['src/**/*.js'])
.pipe($.babelIstanbul())
.pipe($.babelIstanbul.hookRequire())
.on('finish', function() {
return test()
.pipe($.babelIstanbul.writeReports())
.on('end', done);
});
});
function test() {
return gulp.src(['test/setup/node.js', 'test/unit/**/*.js'], { read: false })
.pipe($.mocha({ reporter: 'dot', globals: mochaGlobals }));
}
// Lint and run our tests
gulp.task('test', ['lint-src', 'lint-test'], function() {
_registerBabel();
return test();
});
// Run the headless unit tests as you make changes.
gulp.task('watch', function() {
gulp.watch(['src/**/*', 'test/**/*', '.jshintrc', 'test/.jshintrc'], ['test']);
});
gulp.task('browser-bundle', ['lint-src', 'lint-test'], bundleTest);
gulp.task('test-browser', ['browser-bundle'], browserWatch);
gulp.task('build', ['build-lib']);
// An alias of test
gulp.task('default', ['test']);