-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
166 lines (152 loc) · 5.35 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
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
import { generateDtsBundle } from "dts-bundle-generator"
import { build as esbuild, transform } from "esbuild"
import { readFile, writeFile } from "fs/promises"
import pkg from "./package.json" assert { type: "json" }
const globalName = "dsabp",
entry = "./src/index.ts",
outFolder = "./dist/",
excludeFromSrcMap = /fflate/
/** @type {import("esbuild").BuildOptions} */
const commonOpts = {
entryPoints: [entry],
banner: {
js: `/*! ${pkg.name} v${pkg.version} @license ${pkg.license} ${pkg.homepage} */`
},
bundle: true,
target: "es2022"
}
function getPl(type) {
if (type == "node") return [{
replacePlValues: [
["import.meta.url", "__filename"],
[/.+\/\/ @build_browser-only.*\n?/g, ""],
[/(?:[\t ]+)\/\*[\s\S]+?\*\/ ?/g, ""], // multi line comments
]
}]
if (type == "browser") return [
srcMapExcludePl(excludeFromSrcMap),
{
replacePlValues: [
[/.+\/\/ @build_node-only.*\n?/g, ""],
[/(?:[\t ]+)\/\*[\s\S]+?\*\/ ?/g, ""]
]
}
]
if (type == "browser.min") return [
srcMapExcludePl(excludeFromSrcMap),
{
replacePlValues: [
[/.+\/\/ @build_node-only.*\n?/g, ""]
]
}
]
}
async function build(platform, format, sourcemap, minify) {
const ext = `${minify ? ".min" : ""}.${platform + format == "nodecjs" ? "c" : ""}js`
const outfile = `${outFolder}${platform}/${format}/index${ext}`
console.info(time(), outfile)
const scriptUrlCode = (platform == "browser" && format != "esm") ? await getScriptUrlCode(ext) : undefined
await esbuild({
...commonOpts, outfile,
platform, format, globalName,
minify, sourcemap,
plugins: (platform == "browser" ? (minify ? getPl("browser.min") : getPl("browser")) : getPl("node")).map(o => {
// merges replace values and runs replacePl only once - something is wrong with multiple plugins using onLoad...
if (!o.replacePlValues) return o
o.replacePlValues.unshift(["null // @build_bundleInfo", `{format:"${format}",globalName:"${globalName}"}`])
if (platform == "browser" && format != "esm") {
// save scriptUrl at top if the script uses import.meta, then replace import.meta with scriptUrl
o.replacePlValues.unshift([
/([\s\S]*import\.meta\.url[\s\S]*)/g,
scriptUrlCode + "$1"
], ["import.meta.url", "scriptUrl"])
}
return replacePl(...o.replacePlValues)
})
})
}
await build("node", "cjs", true)
await build("browser", "esm", true)
await build("browser", "esm", true, true)
await build("browser", "iife", true)
await build("browser", "iife", true, true)
await readFile("./README.md", "utf8").then(async contents => {
await writeFile("./README.md", contents.replace(
/[^\s]+(?=<!--@build_tarball-->)/g,
`https://registry.npmjs.org/${pkg.name}/-/${pkg.name}-${pkg.version}.tgz`
))
})
console.info(`${time()} ${outFolder}index.d.ts`)
await writeFile(`${outFolder}index.d.ts`, generateDtsBundle([{
filePath: entry,
output: {
umdModuleName: globalName,
noBanner: true
}
}])[0])
console.info(time(), "finished")
//
async function getScriptUrlCode(ext) {
ext = ext.replaceAll(".", "\\.")
return (await transform(
(() => { /* eslint-disable */
var scriptUrl = globalThis.document?.currentScript?.src
??
(globalThis.window && (window.chrome ?? window.browser)?.runtime
// browser extensions can load the ESM bundle as a module in some ways and the import.meta.url will work fine
// but if it is IIFE and loaded using manifest (not with script element), document.currentScript will be null
// so we need to find the absolute path to the content script from the manifest
// scripting.registerContentScripts needs to be handled differently but that's pointless
? ((regexPath = REGEX, b = window.chrome ?? window.browser) => {
let urlPatternToRegEx = p => new RegExp("^"
+ p.replaceAll("/", "\\/").replaceAll(".", "\\.") // escape . and /
.replaceAll("*:\\/\\/", "https?:\\/\\/") // *:// -> https?
.replaceAll("*\\.", ".*\\.") // *. -> .*\.
.replaceAll("\\.*", "\\..*") // \.* -> \..*
.replaceAll("\\/*", "\\/.*") // /* -> /.*
.replace(/(?<![\/.])\*(?!\.)/g, ".*") // abc*abc -> abc.*abc, no . or / near *
+ "$")
for (let cs of b.runtime.getManifest().content_scripts)
for (let match of cs.matches)
if (urlPatternToRegEx(match).test(location.href))
for (let j of cs.js)
if (regexPath.test(j))
return b.runtime.getURL(j)
})()
: null
)
}) /* eslint-enable */
.toString().slice(7, -1).replace("REGEX", `/.*dsabp.*\\/.*?(index|dsabp)${ext}$|dsabp${ext}$/`)
, { minify: true })
).code
}
/** @param {...[regex: RegExp, replacement: string]} patterns */
function replacePl(...patterns) {
return {
name: "replace",
setup(build) {
build.onLoad({ filter: /.*.ts/ }, async (args) => {
let contents = await readFile(args.path, "utf8")
for (const p of patterns)
contents = contents.replaceAll(p[0], p[1])
return { contents, loader: "default" }
})
}
}
}
/** @param {RegExp} fileFilter */
function srcMapExcludePl(fileFilter) {
return {
name: "srcMapExclude",
setup(build) {
build.onLoad({ filter: fileFilter }, async (args) => {
const contents = await readFile(args.path, "utf8")
+ "\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ=="
return { contents, loader: "default" }
})
},
}
}
function time() {
return new Date().toLocaleTimeString()
}