-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathesbuild.ts
More file actions
231 lines (199 loc) · 5.89 KB
/
esbuild.ts
File metadata and controls
231 lines (199 loc) · 5.89 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { denoPlugins } from "@luca/esbuild-deno-loader";
import type { Plugin as EsbuildPlugin } from "esbuild";
import * as path from "@std/path";
export interface FreshBundleOptions {
dev: boolean;
cwd: string;
buildId: string;
outDir: string;
denoJsonPath: string;
entryPoints: Record<string, string>;
target: string | string[];
jsxImportSource?: string;
}
export interface BuildOutput {
entryToChunk: Map<string, string>;
dependencies: Map<string, string[]>;
files: Array<{ hash: string | null; contents: Uint8Array; path: string }>;
}
let esbuild: null | typeof import("esbuild") = null;
const PREACT_ENV = Deno.env.get("PREACT_PATH");
export async function bundleJs(
options: FreshBundleOptions,
): Promise<BuildOutput> {
if (esbuild === null) {
await startEsbuild();
}
try {
await Deno.mkdir(options.cwd, { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.AlreadyExists)) {
throw err;
}
}
const bundle = await esbuild!.build({
entryPoints: options.entryPoints,
platform: "browser",
target: options.target,
format: "esm",
bundle: true,
splitting: true,
treeShaking: true,
sourcemap: options.dev ? "linked" : false,
minify: !options.dev,
logOverride: {
"suspicious-nullish-coalescing": "silent",
},
jsxDev: options.dev,
jsx: "automatic",
jsxImportSource: options.jsxImportSource ?? "preact",
absWorkingDir: options.cwd,
outdir: ".",
write: false,
metafile: true,
define: {
"process.env.NODE_ENV": JSON.stringify(
options.dev ? "development" : "production",
),
},
plugins: [
preactDebugger(PREACT_ENV),
buildIdPlugin(options.buildId),
windowsPathFixer(),
...denoPlugins({ configPath: options.denoJsonPath }),
],
});
const files: BuildOutput["files"] = [];
for (let i = 0; i < bundle.outputFiles.length; i++) {
const outputFile = bundle.outputFiles[i];
const relative = path.relative(options.cwd, outputFile.path);
files.push({
path: relative,
contents: outputFile.contents,
hash: outputFile.hash,
});
}
files.push({
path: "metafile.json",
contents: new TextEncoder().encode(JSON.stringify(bundle.metafile)),
hash: null,
});
const entryToChunk = new Map<string, string>();
const dependencies = new Map<string, string[]>();
const entryToName = new Map(
Array.from(Object.entries(options.entryPoints)).map(
(entry) => [entry[1], entry[0]],
),
);
if (bundle.metafile) {
const metaOutputs = new Map(Object.entries(bundle.metafile.outputs));
for (const [entryPath, entry] of metaOutputs.entries()) {
const imports = entry.imports
.filter(({ kind }) => kind === "import-statement")
.map(({ path }) => path);
dependencies.set(entryPath, imports);
if (entryPath !== "fresh-runtime.js" && entry.entryPoint !== undefined) {
let filePath = "";
// Resolve back specifiers to original url. This is necessary
// to get JSR dependencies to match what we specified as
// an entry point to esbuild.
if (
entry.entryPoint.startsWith("https://") ||
entry.entryPoint.startsWith("http://")
) {
const basename = path.basename(entryPath, path.extname(entryPath));
filePath = options.entryPoints[basename];
} else {
filePath = path.join(options.cwd, entry.entryPoint);
}
const name = entryToName.get(filePath)!;
entryToChunk.set(name, entryPath);
}
}
}
if (!options.dev) {
esbuild = null;
}
return {
files,
entryToChunk,
dependencies,
};
}
let initialized = false;
export async function startEsbuild() {
esbuild = Deno.env.get("FRESH_ESBUILD_LOADER") === "portable"
? await import("esbuild-wasm")
: await import("esbuild");
if (!initialized) {
await esbuild.initialize({});
initialized = true;
}
}
function buildIdPlugin(buildId: string): EsbuildPlugin {
return {
name: "fresh-build-id",
setup(build) {
build.onResolve({ filter: /runtime[/\\]+build_id\.ts$/ }, (args) => {
return {
path: args.path,
namespace: "fresh-internal",
};
});
build.onLoad({
filter: /runtime[/\\]build_id\.ts$/,
namespace: "fresh-internal",
}, () => {
return {
contents: `export const BUILD_ID = "${buildId}";`,
};
});
},
};
}
function toPreactModPath(mod: string): string {
if (mod === "preact/debug") {
return path.join("debug", "dist", "debug.module.js");
} else if (mod === "preact/hooks") {
return path.join("hooks", "dist", "hooks.module.js");
} else if (mod === "preact/devtools") {
return path.join("devtools", "dist", "devtools.module.js");
} else if (mod === "preact/compat") {
return path.join("compat", "dist", "compat.module.js");
} else if (mod === "preact/jsx-runtime" || mod === "preact/jsx-dev-runtime") {
return path.join("jsx-runtime", "dist", "jsxRuntime.module.js");
} else {
return path.join("dist", "preact.module.js");
}
}
function preactDebugger(preactPath: string | undefined): EsbuildPlugin {
return {
name: "fresh-preact-debugger",
setup(build) {
if (preactPath === undefined) return;
build.onResolve({ filter: /^preact/ }, (args) => {
const resolved = path.resolve(preactPath, toPreactModPath(args.path));
return {
path: resolved,
};
});
},
};
}
function windowsPathFixer(): EsbuildPlugin {
return {
name: "fresh-fix-windows",
setup(build) {
if (Deno.build.os === "windows") {
build.onResolve({ filter: /\.*/ }, (args) => {
if (args.path.startsWith("\\")) {
const normalized = path.resolve(args.path);
return {
path: normalized,
};
}
});
}
},
};
}