-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.js
More file actions
96 lines (81 loc) · 2.07 KB
/
build.js
File metadata and controls
96 lines (81 loc) · 2.07 KB
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
const {promisify} = require('util')
const fs = require('fs')
const {rollup} = require('rollup')
const babel = require('rollup-plugin-babel')
const commonjs = require('rollup-plugin-commonjs')
const nodeResolve = require('rollup-plugin-node-resolve')
const filesize = require('rollup-plugin-filesize')
const uglify = require('rollup-plugin-uglify')
const readFile = promisify(fs.readFile)
const targets = {
cjs: 'dist/vanilla-commons.js',
es: 'dist/vanilla-commons.es.js',
umd: 'dist/vanilla-commons.umd.js',
min: 'dist/vanilla-commons.umd.min.js'
}
async function getDependencies() {
const pkg = JSON.parse(await readFile('./package.json'))
return Object.keys(pkg.dependencies || {})
}
async function generateBundle(format) {
const defaultPlugins = [
nodeResolve(),
commonjs({
include: 'node_modules/**'
}),
babel({
babelrc: false,
presets: [
[
'env',
{
modules: false
}
],
'stage-0'
],
plugins: ['external-helpers']
}),
filesize()
]
const plugins =
format === 'min' ? defaultPlugins.concat(uglify()) : defaultPlugins
const basicConfig = {
entry: 'lib/index.js',
plugins
}
const customConfig = ['cjs', 'es'].includes(format) ?
{external: await getDependencies()} :
{}
return rollup(Object.assign(basicConfig, customConfig))
}
function writeBundle(bundle, format) {
return bundle.write({
dest: targets[format],
format: format === 'min' ? 'umd' : format,
moduleName: 'commons'
})
}
async function buildGeneral() {
const bundle = await generateBundle('es')
return Promise.all([
await writeBundle(bundle, 'es'),
await writeBundle(bundle, 'cjs')
])
}
async function buildUmd() {
const bundle = await generateBundle('umd')
return writeBundle(bundle, 'umd')
}
async function buildMin() {
const bundle = await generateBundle('min')
return writeBundle(bundle, 'min')
}
async function run() {
try {
Promise.all([buildGeneral(), buildUmd(), buildMin()])
} catch (err) {
console.error(err)
}
}
run()