forked from andypf/json-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.mjs
132 lines (119 loc) · 2.83 KB
/
esbuild.config.mjs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as esbuild from "esbuild"
import { rmSync, readFileSync } from "node:fs"
import { resolve } from "node:path"
import postcss from "postcss"
import postcssMinify from "postcss-minify"
// helpers for console log
const green = "\x1b[32m%s\x1b[0m"
const yellow = "\x1b[33m%s\x1b[0m"
const clear = "\x1B[2J"
const isProduction = process.env.NODE_ENV === "production"
const args = process.argv.slice(2).reduce((map, item) => {
if (item.startsWith("--")) {
let [name, value] = item.replace(/^-+/, "").split("=")
if (value === "true") value = true
else if (value === "false") value = false
map[name] = value || true
}
return map
}, {})
await rmSync("demo/build", { recursive: true, force: true })
let cssPlugin = {
name: "minify-css",
setup(build) {
build.onResolve({ filter: /^.*\.css$/ }, (args) => {
return {
path: resolve(args.resolveDir, args.path),
namespace: "minify-css",
}
})
build.onLoad({ filter: /.*/, namespace: "minify-css" }, async (args) => {
const source = readFileSync(args.path, "utf-8")
const css = await postcss(postcssMinify())
.process(source, { from: undefined })
.then((result) => result.css)
return {
contents: css,
loader: "text",
}
})
},
}
// default options
const options = {
external: isProduction ? ["react"] : [],
bundle: true,
minify: isProduction,
loader: {
".css": "text",
},
plugins: [cssPlugin],
}
if (isProduction) {
await rmSync("dist", { recursive: true, force: true })
const prodOptions = {
...options,
entryPoints: {
index: "src/index.js",
"react/JsonViewer": "src/react/JsonViewer.jsx",
},
}
// ESM
await esbuild.build({
...prodOptions,
outdir: `dist/esm/`,
format: "esm",
})
// CommonJS
await esbuild.build({
...prodOptions,
outdir: `dist/cjs/`,
format: "cjs",
})
// IIFE
await esbuild.build({
...prodOptions,
entryPoints: {
index: "src/index.js",
},
outdir: `dist/iife/`,
format: "iife",
})
}
// Themse
await esbuild.build({
...options,
entryPoints: ["src/themes.js"],
outfile: `demo/build/themes.js`,
format: "esm",
})
let ctx = await esbuild.context({
...options,
entryPoints: ["src/index.js"],
outfile: `demo/build/index.js`,
format: "iife",
plugins: [
cssPlugin,
{
name: "start/end",
setup(build) {
build.onStart(() => {
console.log(clear)
console.log(yellow, "Compiling...")
})
build.onEnd(() => console.log(green, "Done!"))
},
},
],
})
if (args.watch) {
let { host, port } = await ctx.serve({
servedir: "./demo",
host: "0.0.0.0",
port: parseInt(process.env.PORT || 3000),
})
console.log("server on " + host + ":" + port)
} else {
await ctx.rebuild()
await ctx.dispose()
}