-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
127 lines (106 loc) · 2.56 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
const gulp = require('gulp')
const webpack = require('webpack')
const babel = require('gulp-babel')
const del = require('del')
const log = require('fancy-log')
const path = require('path')
const fs = require('fs')
const uglify = require('uglify-js')
const COMPILE_SRC = './src/**/*.js'
const COMPILE_LIB = './lib'
const COMPILE_ES = './es' // es modules
const FILE = 'linear.algebra.js'
const DIST = path.join(__dirname, '/dist')
const LINEAR_ALG = DIST + '/' + FILE
const FILE_MIN = 'linear.algebra.min.js'
const FILE_MAP = 'linear.algebra.min.map'
const clean = () => {
return del([
'dist/**/*',
'lib/**/*',
'es/**/*'
])
}
const compile = () => {
return gulp.src(COMPILE_SRC)
.pipe(babel())
.pipe(gulp.dest(COMPILE_LIB))
}
// create a single instance of the compiler to allow caching
const compiler = webpack(require('./webpack.config.js'))
const bundle = (done) => {
compiler.run(function (err, stats) {
if (err) {
log(err)
done(err)
}
const info = stats.toJson()
if (stats.hasWarnings()) {
log('Webpack warnings:\n' + info.warnings.join('\n'))
}
if (stats.hasErrors()) {
log('Webpack errors:\n' + info.errors.join('\n'))
done(new Error('Compile failed'))
}
log('bundled ' + LINEAR_ALG)
done()
})
}
const uglifyConfig = {
sourceMap: {
filename: FILE,
url: FILE_MAP
},
output: {
comments: /@license/
}
}
function minify (done) {
const oldCwd = process.cwd()
process.chdir(DIST)
try {
const result = uglify.minify({
'linear-algebra.js': fs.readFileSync(FILE, 'utf8')
}, uglifyConfig)
if (result.error) {
throw result.error
}
fs.writeFileSync(FILE_MIN, result.code)
fs.writeFileSync(FILE_MAP, result.map)
log('Minified ' + FILE_MIN)
log('Mapped ' + FILE_MAP)
} finally {
process.chdir(oldCwd)
}
done()
}
function compileESModules () {
const babelOptions = JSON.parse(String(fs.readFileSync('.babelrc')))
return gulp.src(COMPILE_SRC)
.pipe(babel({
...babelOptions,
presets: [
['@babel/preset-env', {
modules: false
}]
]
}))
.pipe(gulp.dest(COMPILE_ES))
}
gulp.task('watch', () => {
const files = ['package.json', 'src/*.js']
const options = {
// ignore version.js else we get an infinite loop since it's updated during bundle
ignored: /version\.js/,
ignoreInitial: false,
delay: 100
}
gulp.watch(files, options, gulp.parallel(bundle, compile))
})
gulp.task('default', gulp.series(
clean,
compile,
compileESModules,
bundle,
minify
))