-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGruntfile.js
79 lines (71 loc) · 2.14 KB
/
Gruntfile.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
const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const terser = require('rollup-plugin-terser').terser
const fs = require('fs')
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt)
var pkg = grunt.file.readJSON('package.json')
var file = pkg['jsnext:main']
var main = fs.readFileSync(file, 'utf-8')
var v1 = main.match(/version: '([\d.]+)'/)[1]
if (v1 !== pkg.version) {
console.log(`Updating version in ${file}: ${v1} => ${pkg.version}`)
main = main.replace(`version: '${v1}'`, `version: '${pkg.version}'`)
fs.writeFileSync(file, main)
}
var year = new Date().getFullYear()
year = year === 2016 ? year : `2016-${year}`
var banner = `/*!
* ${pkg.name} v${pkg.version}
* (c) ${year} Greg Slepak
* @license MIT License
*/`
function pick (o, props) {
var x = {}
props.forEach(k => { x[k] = o[k] })
return x
}
grunt.initConfig({
pkg: pkg,
checkDependencies: { this: { options: { install: true } } },
exec: {
eslint: 'node ./node_modules/eslint/bin/eslint.js "**/*.js"'
},
rollup: {
umd: {
input: 'src/index.js',
plugins: [babel({ exclude: './node_modules/**' })],
output: {
file: `dist/${pkg.name}.js`,
format: 'umd',
name: camelCase(pkg.name),
banner
}
},
ugly: {
input: 'src/index.js',
plugins: [
babel({ exclude: './node_modules/**' }),
terser({ output: { preamble: banner } })
],
output: {
file: `dist/${pkg.name}.min.js`,
format: 'umd',
name: camelCase(pkg.name)
}
}
}
})
grunt.registerMultiTask('rollup', async function () {
// grunt.log.writeln(this.target + ': ', this.data)
var done = this.async()
// https://rollupjs.org/guide/en#javascript-api
const bundle = await rollup.rollup(pick(this.data, ['input', 'plugins']))
await bundle.write(this.data.output)
done()
})
grunt.registerTask('default', ['exec:eslint', 'rollup'])
}
function camelCase (s) {
return s.replace(/(?:^|[-_/])(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}