-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
133 lines (118 loc) · 3.18 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
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")
function AppendCssStyles () {
return {
name: "append-css-styles",
setup(build) {
build.onStart(async () => {
const styles = await fsPromises.readFile(
path.join(process.cwd(), "src", "css", "richer-text-core.css"),
{ encoding: "utf8" }
)
const finalString = `/* THIS FILE IS AUTO-GENERATED. DO NOT EDIT BY HAND! */
${styles.toString()}
`
await fsPromises.mkdir(path.join(process.cwd(), "dist", "css"), { recursive: true })
await fsPromises.writeFile(path.join(process.cwd(), "dist", "css", "richer-text.css"), finalString)
})
}
}
}
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 () {
const entries = {}
glob.sync("./src/**/*.{js,css}")
.forEach((file) => {
const key = path.relative(path.join("src"), 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/index.js"],
sourcemap: true,
target: "es2020",
color: true,
bundle: true,
external: [],
plugins: [
AppendCssStyles(),
]
}
const configs = [
{
...defaultConfig,
outfile: "dist/bundle/index.common.js",
format: "cjs",
minify: true,
},
{
...defaultConfig,
outfile: "dist/bundle/index.module.js",
format: "esm",
minify: true,
splitting: false,
},
{
...defaultConfig,
entryPoints: entries,
outdir: 'dist',
format: 'esm',
target: "es2020",
external: deps,
splitting: true,
minify: false,
chunkNames: 'chunks/[name]-[hash]',
plugins: defaultConfig.plugins.concat([BuildTimer()])
},
// {
// ...defaultConfig,
// entryPoints: entries,
// outdir: 'cdn/dist',
// 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)
})
})()