-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ts
119 lines (105 loc) · 3.42 KB
/
build.ts
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
/*
This script was taken from hono https://github.com/honojs/hono/blob/main/build.ts
Everything below is theres
*/
/*
This script is heavily inspired by `built.ts` used in @kaze-style/react.
https://github.com/taishinaritomi/kaze-style/blob/main/scripts/build.ts
MIT License
Copyright (c) 2022 Taishi Naritomi
*/
import { exec } from "child_process"
import fs from "fs"
import path from "path"
import { build } from "esbuild"
import type { Plugin, PluginBuild, BuildOptions } from "esbuild"
import glob from "glob"
// Plugin to fix CommonJS imports by appending .cjs to require statements
const fixCjsImportsPlugin = (): Plugin => ({
name: "fix-cjs-imports",
setup(build: PluginBuild) {
// Run in the onEnd hook after all files have been written
build.onEnd((result) => {
// Only proceed if the build is successful
if (result.errors.length === 0) {
// Get the output directory from the build options
const outdir = build.initialOptions.outdir
if (!outdir) return
// Find all .cjs files in the output directory
const files = glob.sync(`${outdir}/**/*.cjs`)
files.forEach((file) => {
let content = fs.readFileSync(file, "utf8")
// Replace all require('./something') with require('./something.cjs')
content = content.replace(/require\(["'](\.[^"']+)["']\)/g, (match, importPath) => {
// Don't add .cjs if it already has an extension
if (path.extname(importPath) !== "") {
return match
}
return `require('${importPath}.cjs')`
})
fs.writeFileSync(file, content)
})
}
})
}
})
const entryPoints = glob.sync("./src/**/*.ts", {
ignore: ["./src/**/*.test.ts", "./src/mod.ts", "./src/middleware.ts", "./src/deno/**/*.ts"]
})
/*
This plugin is inspired by the following.
https://github.com/evanw/esbuild/issues/622#issuecomment-769462611
*/
const addExtension = (extension: string = ".js", fileExtension: string = ".ts"): Plugin => ({
name: "add-extension",
setup(build: PluginBuild) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
const p = path.join(args.resolveDir, args.path)
let tsPath = `${p}${fileExtension}`
let importPath = ""
if (fs.existsSync(tsPath)) {
importPath = args.path + extension
} else {
tsPath = path.join(args.resolveDir, args.path, `index${fileExtension}`)
if (fs.existsSync(tsPath)) {
if (args.path.endsWith("/")) {
importPath = `${args.path}index${extension}`
} else {
importPath = `${args.path}/index${extension}`
}
}
}
return { path: importPath, external: true }
}
})
}
})
const commonOptions: BuildOptions = {
entryPoints,
logLevel: "info",
platform: "node",
tsconfig: "tsconfig.json",
target: "es2022"
}
const cjsBuild = () =>
build({
...commonOptions,
outbase: "./src",
outdir: "./dist/cjs",
format: "cjs",
outExtension: { ".js": ".cjs" },
plugins: [fixCjsImportsPlugin()],
tsconfig: "tsconfig.cjs.json"
})
const esmBuild = () =>
build({
...commonOptions,
bundle: true,
outbase: "./src",
outdir: "./dist",
format: "esm",
plugins: [addExtension(".js")]
})
Promise.all([esmBuild(), cjsBuild()])
exec(`tsc --emitDeclarationOnly --declaration --project tsconfig.build.json`)