-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
67 lines (54 loc) · 1.2 KB
/
build.js
File metadata and controls
67 lines (54 loc) · 1.2 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
'use strict'
const babel = require('@babel/core')
const del = require('del')
const fs = require('fs')
const SRC = 'definit.js'
const ES = 'definit.es.js'
const CJS = 'definit.cjs.js'
const babelPlugins = [
['@babel/plugin-check-constants', {loose: true}],
['@babel/plugin-transform-block-scoping', {loose: true}],
]
const cjsBabelPlugins = [
...babelPlugins,
['@babel/plugin-transform-modules-commonjs', {strict: true, noInterop: true}],
]
function main() {
const cmd = process.argv[2]
if (!cmd) {
build()
}
else if (cmd === 'watch') {
watch()
buildOrReport()
}
else {
throw Error(`Unrecognized command: ${cmd}`)
}
}
function clear() {
del.sync(ES)
del.sync(CJS)
}
function build() {
clear()
const t0 = Date.now()
const {code: esCode} = babel.transformFileSync(SRC, {plugins: babelPlugins})
fs.writeFileSync(ES, esCode)
const {code: cjsCode} = babel.transform(esCode, {plugins: cjsBabelPlugins})
fs.writeFileSync(CJS, cjsCode)
const t1 = Date.now()
console.info(`Built in ${t1 - t0}ms`)
}
function watch() {
fs.watch(SRC, buildOrReport)
}
function buildOrReport() {
try {
build()
}
catch (err) {
console.error(err)
}
}
main()