forked from callmecavs/bricks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
131 lines (102 loc) · 2.63 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
// imports
const json = require('./package.json')
const sync = require('browser-sync')
const del = require('del')
const fs = require('fs')
const gulp = require('gulp')
const mkdirp = require('mkdirp')
const notifier = require('node-notifier')
const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const commonjs = require('rollup-plugin-commonjs')
const resolve = require('rollup-plugin-node-resolve')
const uglify = require('rollup-plugin-uglify')
// error handler
const onError = function(error) {
notifier.notify({
'title': 'Error',
'message': 'Compilation failure.'
})
console.log(error)
this.emit('end')
}
// clean
gulp.task('clean', () => del('dist/**/*.js', 'dist/**/*.map'))
// attribution
const attribution =
`/*!
* Bricks.js ${ json.version } - ${ json.description }
* Copyright (c) ${ new Date().getFullYear() } ${ json.author } - ${ json.homepage }
* License: ${ json.license }
*/
`
// js
const read = {
entry: 'src/bricks.js',
sourceMap: true,
plugins: [
resolve({ jsnext: true }),
babel(),
uglify()
]
}
const write = {
format: 'umd',
exports: 'default',
moduleName: 'Bricks',
sourceMap: true
}
gulp.task('js', () => {
return rollup
.rollup(read)
.then(bundle => {
// generate the bundle
const files = bundle.generate(write)
// cache path to JS dist file
const dist = 'dist/bricks.min.js'
// write the attribution
fs.writeFileSync(dist, attribution)
fs.writeFileSync('site/src/js/bricks.min.js', attribution)
// write the JS and sourcemap
fs.appendFileSync(dist, files.code)
fs.appendFileSync('site/src/js/bricks.min.js', files.code)
fs.writeFileSync('dist/maps/bricks.js.map', files.map.toString())
})
})
// server
const server = sync.create()
const reload = sync.reload
const sendMaps = (req, res, next) => {
const filename = req.url.split('/').pop()
const extension = filename.split('.').pop()
if(extension === 'css' || extension === 'js') {
res.setHeader('X-SourceMap', '/maps/' + filename + '.map')
}
return next()
}
const options = {
notify: false,
server: {
baseDir: 'dist',
middleware: [
sendMaps
]
},
watchOptions: {
ignored: '*.map'
}
}
gulp.task('server', () => sync(options))
// watch
gulp.task('watch', () => {
gulp.watch('src/bricks.js', ['js', reload])
})
// build and default tasks
gulp.task('build', ['clean'], () => {
// create dist directories, if needed
mkdirp.sync('dist')
mkdirp.sync('dist/maps')
// run the tasks
gulp.start('js')
})
gulp.task('default', ['build', 'server', 'watch'])