forked from handsontable/formula-parser
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.babel.js
237 lines (201 loc) · 6.35 KB
/
gulpfile.babel.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
import del from 'del';
import glob from 'glob';
import gulp from 'gulp';
import jison from 'gulp-jison';
import loadPlugins from 'gulp-load-plugins';
import path from 'path';
import source from 'vinyl-source-stream';
import webpack from 'webpack';
import webpackStream from 'webpack-stream';
import yargs from 'yargs';
import {Instrumenter} from 'isparta';
import mochaGlobals from './test/setup/.globals';
import manifest from './package.json';
// Load all of our Gulp plugins
const $ = loadPlugins();
// Gather the library data from `package.json`
const config = manifest.babelBoilerplateOptions;
const mainFile = manifest.main;
const destinationFolder = path.dirname(mainFile);
const exportFileName = path.basename(mainFile, path.extname(mainFile));
function cleanDist(done) {
del([destinationFolder]).then(() => done());
}
function cleanTmp(done) {
del(['tmp']).then(() => done());
}
function onError() {
$.util.beep();
}
// Generate parser
function generateParser() {
return gulp.src('./src/grammar-parser/grammar-parser.jison')
.pipe(jison({moduleType: 'commonjs'}))
.pipe(gulp.dest('./src/grammar-parser'));
}
// Lint a set of files
function lint(files) {
return gulp.src(files)
.pipe($.plumber())
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError())
.pipe($.jscs())
.pipe($.jscs.reporter())
.pipe($.jscs.reporter('fail'))
.on('error', onError);
}
function lintSrc() {
return lint(['src/**/*.js', '!src/grammar-parser/grammar-parser.js']);
}
function lintTest() {
return lint('test/**/*.js');
}
function lintGulpfile() {
return lint('gulpfile.babel.js');
}
function build() {
return gulp.src(path.join(config.entryFileName + '.js'))
.pipe($.plumber())
.pipe(webpackStream({
output: {
filename: exportFileName + '.js',
libraryTarget: 'umd',
library: config.mainVarName
},
module: {
loaders: [
{
test: /\/numbro\/languages\/(([a-z]+\-[A-Z]+)|index)\.js$/,
loader: require.resolve('null-loader')
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: require.resolve('babel-loader'),
babelrc: false,
query: {
presets: [require.resolve('babel-preset-es2015')]
}
}
],
},
devtool: 'source-map',
target: 'node',
}))
.pipe(gulp.dest(destinationFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(exportFileName + '.min.js'))
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.uglify())
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destinationFolder));
}
function _mocha() {
const envs = $.env.set({
NODE_ENV: 'test'
});
return gulp.src(['test/setup/node.js', 'test/unit/**/*.js', 'test/integration/**/*.js'], {read: false})
.pipe(envs)
.pipe($.mocha({
reporter: 'dot',
globals: Object.keys(mochaGlobals.globals),
ignoreLeaks: false,
grep: yargs.argv.grep
}));
}
function _registerBabel() {
require('babel-register');
}
function test() {
_registerBabel();
return _mocha();
}
function coverage(done) {
_registerBabel();
gulp.src(['src/**/*.js', '!src/grammar-parser/*'])
.pipe($.istanbul({instrumenter: Instrumenter}))
.pipe($.istanbul.hookRequire())
.on('finish', () => {
return test()
.pipe($.istanbul.writeReports())
.on('end', done);
});
}
const watchFiles = ['src/**/*', 'test/**/*', 'package.json', '**/.eslintrc', '.jscsrc'];
// Run the headless tests as you make changes.
function watch() {
gulp.watch(watchFiles, ['test']);
}
function testBrowser() {
// Our testing bundle is made up of our tests, which
// should individually load up pieces of our application.
// We also include the browser setup file.
const unitTestFiles = glob.sync('./test/unit/**/*.js');
const integrationTestFiles = glob.sync('./test/integration/**/*.js');
const allFiles = ['./test/setup/browser.js'].concat(unitTestFiles, integrationTestFiles);
// Lets us differentiate between the first build and subsequent builds
var firstBuild = true;
// This empty stream might seem like a hack, but we need to specify all of our files through
// the `entry` option of webpack. Otherwise, it ignores whatever file(s) are placed in here.
return gulp.src('')
.pipe($.plumber())
.pipe(webpackStream({
watch: true,
entry: allFiles,
output: {
filename: '__spec-build.js'
},
module: {
loaders: [
// This is what allows us to author in future JavaScript
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
// This allows the test setup scripts to load `package.json`
{ test: /\.json$/, exclude: /node_modules/, loader: 'json-loader' }
]
},
plugins: [
// By default, webpack does `n=>n` compilation with entry files. This concatenates
// them into a single chunk.
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })
],
devtool: 'inline-source-map'
}, null, function() {
if (firstBuild) {
$.livereload.listen({port: 35729, host: 'localhost', start: true});
var watcher = gulp.watch(watchFiles, ['lint']);
} else {
$.livereload.reload('./tmp/__spec-build.js');
}
firstBuild = false;
}))
.pipe(gulp.dest('./tmp'));
}
// Remove the built files
gulp.task('clean', cleanDist);
// Remove our temporary files
gulp.task('clean-tmp', cleanTmp);
// Lint our source code
gulp.task('lint-src', lintSrc);
// Lint our test code
gulp.task('lint-test', lintTest);
// Lint this file
gulp.task('lint-gulpfile', lintGulpfile);
// Lint everything
gulp.task('lint', ['lint-src', 'lint-test', 'lint-gulpfile']);
// Build two versions of the library
gulp.task('build', ['lint', 'clean'], build);
// Build two versions of the library (without linting)
gulp.task('_build', build);
// Lint and run our tests
gulp.task('test', ['lint'], test);
// Set up coverage
gulp.task('coverage', coverage);
// Set up a livereload environment for our spec runner `test/runner.html`
gulp.task('test-browser', ['lint', 'clean-tmp'], testBrowser);
// Run the headless tests as you make changes
gulp.task('watch', watch);
// Generate parser
gulp.task('generate-parser', generateParser);
// An alias of test
gulp.task('default', ['test']);