-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
esbuild.config.js
169 lines (148 loc) · 4.05 KB
/
esbuild.config.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// @ts-check
import * as path from "path";
import * as glob from "glob"
import esbuild from "esbuild"
import * as fs from "fs"
import * as fsPromises from "fs/promises"
import chalk from "chalk"
const pkg = JSON.parse(fs.readFileSync("./package.json").toString())
const deps = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {})
]
const watchMode = process.argv.includes("--watch")
/**
* @return {import("esbuild").Plugin}
*/
function AppendCssStyles () {
return {
name: "append-css-styles",
setup(build) {
build.onStart(async () => {
const styles = await fsPromises.readFile(
path.join(process.cwd(), "src", "exports", "styles", "trix-core.css"),
{ encoding: "utf8" }
)
let date = new Date()
const {
hostStyles,
toolbarButtonStyles,
cursorStyles
} = await import(`./src/exports/styles/editor.js?cache=${date.toString()}`)
const banner = `/* THIS FILE IS AUTO-GENERATED. DO NOT EDIT BY HAND! */`
const editorCSS = `
/* src/exports/styles/editor.js:hostStyles */
${hostStyles.toString()}
/* src/exports/styles/editor.js:toolbarButtonStyles */
${cursorStyles.toString()}
/* src/exports/styles/editor.js:toolbarButtonStyles */
${toolbarButtonStyles.toString()}
`.trim()
const trixCSS = `
${styles.toString()}
${editorCSS}
`.trim()
await fsPromises.writeFile(path.join(process.cwd(), "src", "exports", "styles", "trix.css"), banner + "\n\n" + trixCSS)
await fsPromises.writeFile(path.join(process.cwd(), "src", "exports", "styles", "rhino-editor.css"), banner + "\n\n" + editorCSS)
})
}
}
}
/**
* @returns {import("esbuild").Plugin}
*/
function BuildTimer () {
return {
name: "build-timer",
setup(build) {
let startTime
build.onStart(() => {
startTime = Number(new Date())
})
build.onEnd(() => {
const endTime = Number(new Date())
const buildTime = endTime - startTime
console.log(chalk.green(`Build complete in ${buildTime}ms!`), `✨\n\n`)
})
}
}
}
;(async function () {
/**
* @type {import("esbuild").BuildOptions["entryPoints"]}
*/
const entries = {}
glob.sync("./src/exports/**/*.{ts,js,css}")
.forEach((file) => {
const key = path.relative(path.join("src", "exports"), path.join(path.dirname(file), path.basename(file, path.extname(file))))
const value = "." + path.sep + path.join(path.dirname(file), path.basename(file, path.extname(file)))
entries[key] = value
});
const defaultConfig = {
entryPoints: ["./src/exports/index.ts"],
sourcemap: true,
target: "es2020",
color: true,
bundle: true,
external: [],
plugins: [
AppendCssStyles(),
]
}
/**
* @type {Array<import("esbuild").BuildOptions>}
*/
const configs = [
{
...defaultConfig,
outfile: "exports/bundle/index.common.js",
format: "cjs",
minify: true,
},
{
...defaultConfig,
outfile: "exports/bundle/index.module.js",
format: "esm",
minify: true,
splitting: false,
},
{
...defaultConfig,
entryPoints: entries,
outdir: 'exports',
format: 'esm',
target: "es2020",
bundle: true,
external: deps,
splitting: true,
minify: false,
chunkNames: 'chunks/[name]-[hash]',
plugins: defaultConfig.plugins.concat([BuildTimer()])
},
{
...defaultConfig,
entryPoints: entries,
outdir: 'cdn/exports',
format: 'esm',
target: "es2020",
external: [],
splitting: true,
minify: false,
chunkNames: 'chunks/[name]-[hash]'
},
]
if (!watchMode) {
await Promise.all(configs.map((config) => esbuild.build(config)))
.catch((err) => {
console.error(err)
process.exit(1)
})
return
}
await Promise.all(configs.map(async (config) => {
const context = await esbuild.context(config)
return await context.watch()
})).catch((err) => {
console.error(err)
})
})()