-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbundle.js
98 lines (76 loc) · 2.79 KB
/
bundle.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
import browserify from 'browserify'
import chalk from 'chalk'
import exorcist from 'exorcist'
import fs from 'fs'
import mkdirp from 'mkdirp'
import rollupify from 'rollupify'
import uglifyJs from 'uglify-js'
export default function bundle(opts) {
const domain = chalk.dim(`[${opts.domain}]`)
console.log(domain, `PacPan is getting your Panels app "${opts.expose}" ready to go :)`)
console.time('pacpan-bundle')
const b = browserify({
debug: true,
entries: [opts.entry]
})
// entry point of our app, panels needs this to require it
b.require(opts.entry, {expose: opts.expose})
// expose the app dependencies
b.require(opts.requires)
// rollupify the bundle
b.transform(rollupify, {config: opts.rollupConfig})
// declare our build's externals
opts.externals.forEach(dep => b.external(dep))
// make sure the bundle directory exists
mkdirp.sync(opts.bundle)
// determine the bundle's full path
const out = `${opts.expose.replace(/[@\/]/g, '')}-${opts.version}`
const outJs = `${out}.js`
const outJsMap = `${outJs}.map`
const outJsMin = `${out}.min.js`
const outJsMinMap = `${outJsMin}.map`
function minify() {
const minified = uglifyJs.minify(`${opts.bundle}/${outJs}`, {
compress: {
screw_ie8: true
},
inSourceMap: `${opts.bundle}/${outJsMap}`,
mangle: {
screw_ie8: true
},
outSourceMap: outJsMinMap
})
// write the minified code
const codeStream = fs.createWriteStream(`${opts.bundle}/${outJsMin}`, 'utf8')
codeStream.write(minified.code, () => codeStream.end())
// write the minified map code
const mapStream = fs.createWriteStream(`${opts.bundle}/${outJsMinMap}`, 'utf8')
mapStream.write(minified.map, () => mapStream.end())
}
function buildIndexHtml() {
const out = fs.createWriteStream(`${opts.bundle}/index.html`, 'utf8')
const html = fs.readFileSync(`${__dirname}/playground.html`).toString()
.replace(
'<script src=/panels.js></script>\n',
`<script src=/${outJsMin}></script>\n<script src=https://cdn.uxtemple.com/panels.js></script>\n`
)
out.write(html, () => out.end())
}
function buildPanelsJson() {
const out = fs.createWriteStream(`${opts.bundle}/panels.json`, 'utf8')
const json = fs.readFileSync(`${__dirname}/panels.json`).toString().replace('app.js', outJsMin)
out.write(json)
out.end()
}
b.bundle()
.pipe(exorcist(`${opts.bundle}/${outJsMap}`, outJsMap))
.pipe(fs.createWriteStream(`${opts.bundle}/${outJs}`), 'utf8')
.on('finish', () => {
// minify()
// buildIndexHtml()
// buildPanelsJson()
console.timeEnd('pacpan-bundle')
console.log(domain, `PacPan just finished. Your bundle is at ${opts.bundle}:`)
console.log(fs.readdirSync(opts.bundle).join(', '))
})
}