-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
100 lines (89 loc) · 3.03 KB
/
build.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
#!/usr/bin/env node
/* global require, Buffer */
const fss = require('fs')
const fs = fss.promises
const path = require('path')
const zlib = require('zlib')
const Terser = require('terser')
const buble = require('buble')
const input = fss.readFileSync('microh.js', 'utf8')
const name = 'microh'
const srcName = name + '.js'
const outName = name + '.min.js'
const es5SrcName = srcName.replace('js', 'es5.js')
const es5OutName = outName.replace('min', 'es5.min')
const terserOut = Terser.minify(
{ [srcName]: input },
{
module: true,
ecma: 8,
sourceMap: { filename: outName, url: outName + '.map' }
}
)
const umdBoiler = code =>
`
;(function(root, factory) {
if(typeof define === 'function' && define.amd) define([], factory)
else if(typeof module === 'object' && module.exports) module.exports = factory()
else root['${name}'] = factory()
})(typeof self !== 'undefined' ? self : this, function() {
'use strict'
var exports = {}
${code}
return exports
})
`.trimStart()
const moduleToBrowser = code =>
umdBoiler(
code
.replace(/export\s+(const|let|var)\s+([^\s]+)/gi, '$1 $2 = exports.$2')
.replace(/export\s+\{([^}]+)\}/, (_, names) =>
names
.split(',')
.map(name => `exports.${name} = ${name}`)
.join('\n')
)
// .replace(/export\s+default\s+([^\s]+)/gi, 'exports = Object.assign($1, exports)')
.replace(/export\s+default\s+([^\s]+)/gi, 'exports = $1') // since no named exports for now don't depend on object.assign
.replace(/^/gm, ' ')
)
const bubleOut = buble.transform(moduleToBrowser(input), {
modules: false,
transforms: { dangerousForOf: true }
})
const bubleTerserOut = Terser.minify(
{ [es5SrcName]: bubleOut.code },
{ ecma: 5, sourceMap: { filename: es5OutName, url: es5OutName + '.map' } }
)
const reportDiff = (path, newCode) => {
if (!fss.existsSync(path)) return
const beforeSize = fss.statSync(path).size
const beforeGzip = zlib.gzipSync(fss.readFileSync(path)).byteLength
const afterSize = newCode.length
const afterGzip = zlib.gzipSync(Buffer.from(newCode, 'utf-8')).byteLength
console.log(
[
'== ' + path + ' ==',
'Before: ' + beforeSize + ' (gzip ' + beforeGzip + ')',
' After: ' + afterSize + ' (gzip ' + afterGzip + ')',
' Diff: ' + (afterSize - beforeSize) + ' (gzip ' + (afterGzip - beforeGzip) + ')',
''.padStart(path.length + 6, '='),
''
].join('\n')
)
}
fs.mkdir('dist', { recursive: true }).then(() => {
// copy main source file
fs.writeFile(path.join('dist', srcName), input)
fs.writeFile(path.join('dist', es5SrcName), bubleOut.code)
// write min file and source map
const outPath = path.join('dist', outName)
reportDiff(outPath, terserOut.code)
fs.writeFile(outPath, terserOut.code)
fs.writeFile(outPath + '.map', terserOut.map)
// write out transpiled source
const es5OutPath = path.join('dist', es5OutName)
reportDiff(es5OutPath, bubleTerserOut.code)
fs.writeFile(es5OutPath, bubleTerserOut.code)
fs.writeFile(es5OutPath + '.map', bubleTerserOut.map)
})