-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
102 lines (93 loc) · 3.22 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
101
102
#!/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 inputCode = fss.readFileSync('src/index.js', 'utf8')
const globalName = 'z'
const name = 'zaftig'
const srcName = name + '.js'
const outName = name + '.min.js'
const es5SrcName = name + '.es5.js'
const es5OutName = name + '.es5.min.js'
// const p = (...args) => (console.log(...args), args[0])
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.${globalName} = factory()
})(typeof self !== 'undefined' ? self : this, function() {
'use strict'
var exports = {}
${code}
return exports
})
`
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(/^/gm, ' ')
)
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(async () => {
const bubleOut = buble.transform(moduleToBrowser(inputCode), {
modules: false,
transforms: { dangerousForOf: true }
})
const terserOut = await Terser.minify(
{ [srcName]: inputCode },
{
module: true,
ecma: 2020,
mangle: { properties: { regex: /^_/ } },
sourceMap: { filename: outName, url: outName + '.map' }
}
)
const bubleTerserOut = await Terser.minify(
{ [es5SrcName]: bubleOut.code },
{
ecma: 5,
mangle: { properties: { regex: /^_/ } },
sourceMap: { filename: es5OutName, url: es5OutName + '.map' }
}
)
// copy main source file
fs.writeFile(path.join('dist', srcName), inputCode)
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)
})