-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbuild.js
67 lines (55 loc) · 2.16 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
const fs = require("fs-extra")
const execa = require("execa")
const minimist = require("minimist")
const stdio = ["ignore", "inherit", "pipe"]
const opts = { stdio }
const {
_: [packageName],
target
} = minimist(process.argv.slice(2))
// build to publish needs to do more things so it's slower
// for the CI run and local testing this is not necessary
const isPublish = target === "publish"
// for running tests in CI we need CJS only
const isTest = target === "test"
const run = async () => {
// TSDX converts passed name argument to lowercase for file name
const pkgPrefix = `${packageName.toLowerCase()}.`
const tempMove = name => fs.move(`dist/${pkgPrefix}${name}`, `temp/${pkgPrefix}${name}`)
const moveTemp = name => fs.move(`temp/${pkgPrefix}${name}`, `dist/${pkgPrefix}${name}`)
const build = (format, env) => {
const args = ["build", "--name", packageName, "--format", format]
if (env) {
args.push("--env", env)
}
return execa("tsdx", args, opts)
}
if (isPublish) {
await fs.emptyDir("temp")
// build dev/prod ESM bundles that can be consumed in browser without NODE_ENV annoyance
// and these builds cannot be run in parallel because tsdx doesn't allow to change target dir
await build("esm", "development")
// tsdx will purge dist folder, so it's necessary to move these
await tempMove(`esm.development.js`)
await tempMove(`esm.development.js.map`)
// cannot build these concurrently
await build("esm", "production")
await tempMove(`esm.production.min.js`)
await tempMove(`esm.production.min.js.map`)
}
await build(isTest ? "cjs" : "esm,cjs,umd").catch(err => {
throw new Error(`build failed: ${err.stderr}`)
})
if (isPublish) {
// move ESM bundles back to dist folder and remove temp
await moveTemp(`esm.development.js`)
await moveTemp(`esm.development.js.map`)
await moveTemp(`esm.production.min.js`)
await moveTemp(`esm.production.min.js.map`)
await fs.remove("temp")
}
}
run().catch(err => {
console.error(err)
process.exit(1)
})