-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesb.ts
More file actions
149 lines (130 loc) · 3.96 KB
/
esb.ts
File metadata and controls
149 lines (130 loc) · 3.96 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
import type { Adapter, Builder } from "@sveltejs/kit";
import { build } from "esbuild";
import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
interface AdapterOptions {
/**
* @default "build"
*/
out?: string;
/**
* @default true
*/
precompress?: boolean;
/**
* esbuild can only build for "node"
*
* @default "node"
*/
runtime?: "node";
/**
* Options for esbuild build
*/
buildOptions?: {
/**
* @default true
*/
minify?: boolean;
/**
* @default "linked"
*/
sourcemap?: "linked" | "none" | "inline" | "external";
};
}
const files = fileURLToPath(new URL("./files", import.meta.url).href);
export default (options: AdapterOptions = {}): Adapter => {
const { out = "build", precompress = true } = options;
const sourcemap = options.buildOptions?.sourcemap ?? "linked";
return {
name: "sveltekit-on-lambda",
async adapt(builder: Builder) {
const tmp = builder.getBuildDirectory("adapter-esbuild-build-lambda");
builder.rimraf(out);
builder.rimraf(tmp);
builder.mkdirp(tmp);
builder.log.minor("Copying assets");
const clientFiles = builder.writeClient(
`${out}/client${builder.config.kit.paths.base}`,
);
const prerenderedFiles = builder.writePrerendered(
`${out}/prerendered${builder.config.kit.paths.base}`,
);
if (precompress) {
builder.log.minor("Compressing assets");
await Promise.all([
builder.compress(`${out}/client`),
builder.compress(`${out}/prerendered`),
]);
}
builder.log.minor("Building server");
builder.writeServer(tmp);
writeFileSync(
`${tmp}/manifest.js`,
[
`export const manifest = ${builder.generateManifest({ relativePath: "./" })};`,
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});`,
`export const base = ${JSON.stringify(builder.config.kit.paths.base)};`,
].join("\n\n"),
);
const pkg = JSON.parse(readFileSync("package.json", "utf8"));
const input: Record<string, string> = {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`,
};
if (builder.hasServerInstrumentationFile?.()) {
input["instrumentation.server"] = `${tmp}/instrumentation.server.js`;
}
await build({
entryPoints: Object.values(input),
format: "esm",
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) =>
new RegExp(`^${d}(\\/.*)?$`).toString(),
),
],
target: "es2022",
bundle: true,
platform: "node",
outdir: `${out}/server`,
minify: options.buildOptions?.minify ?? true,
splitting: true,
treeShaking: true,
sourcemap: sourcemap === "none" ? undefined : sourcemap,
});
builder.copy(`${files}/node`, `${out}/server`, {
replace: {
MANIFEST: "./manifest.js",
SERVER: "./index.js",
},
});
if (builder.hasServerInstrumentationFile?.()) {
builder.instrument?.({
entrypoint: `${out}/index.js`,
instrumentation: `${out}/server/instrumentation.server.js`,
module: {
exports: ["path", "host", "port", "server"],
},
});
}
const routes = [
...new Set(
[...clientFiles, ...prerenderedFiles]
.map((x) => {
const z = dirname(x);
if (z === ".") return x;
if (z.includes("/")) return undefined;
return `${z}/*`;
})
.filter(Boolean),
),
];
writeFileSync(join(out, "routes.json"), JSON.stringify(routes));
},
supports: {
read: () => true,
instrumentation: () => true,
},
};
};