Skip to content

Commit

Permalink
add-entry-file-support
Browse files Browse the repository at this point in the history
  • Loading branch information
bertybot2 committed Jan 29, 2025
1 parent b3e5f96 commit 3d7c8f2
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 246 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-rocks-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"svelte-adapter-fastly": minor
---

- Added Build step for custom entry files. entry files can now be written in Typescript and handle relative imports.
- Added `experimentalTopLevelAwait` flag
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface AdapterOptions {
entry?: string;
staticPublishConfig?: string;
silent?: boolean;
experimentalTopLevelAwait?: boolean;
}

export type FastlyConfig = {
Expand Down
95 changes: 56 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { posix, resolve } from "node:path";
import { execSync } from "node:child_process";
import { build } from "esbuild";
import toml from "@iarna/toml";
import readline from "node:readline/promises";
import { fileURLToPath } from "node:url";
Expand Down Expand Up @@ -49,32 +50,6 @@ export default function (opts = {}) {

const workerSource = `${tmp}/src`;
const relativePath = posix.relative(tmp, builder.getServerDirectory());
const workerRelativePath = posix.relative(
workerSource,
builder.getServerDirectory()
);

builder.copy(
`${files}/handleSvelteKitRequest.js`,
`${workerSource}/handleSvelteKitRequest.js`,
{
replace: {
SERVER: `${workerRelativePath}/index.js`,
MANIFEST: `../manifest.js`,
STATICS: `../static-publisher/statics.js`,
},
}
);

builder.copy(
entry ? entry : `${files}/entry.js`,
`${workerSource}/entry.js`,
{
replace: {
"svelte-adapter-fastly": `./handleSvelteKitRequest.js`,
},
}
);

let prerendered_entries = Array.from(builder.prerendered.pages.entries());

Expand All @@ -84,25 +59,67 @@ export default function (opts = {}) {
{ file: `${builder.config.kit.paths.base}/${file}` },
]);
}
await build({
conditions: ["fastly"],
entryPoints: [entry ? entry : `${files}/entry.js`],
bundle: true,
outdir: workerSource,
write: true,
external: ["fastly:*"],
allowOverwrite: true,
format: "esm",
tsconfig: undefined,
plugins: [
{
name: "svelte-adapter-fastly",
setup(build) {
build.onResolve({ filter: /^svelte-adapter-fastly/ }, () => {
return { path: `${files}/handleSvelteKitRequest.js` };
});
build.onResolve({ filter: /^MANIFEST/ }, (args) => ({
path: args.path,
namespace: "svelte-adapter-fastly",
}));

writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath,
})};\n\n` +
`export const prerendered = new Map(${JSON.stringify(
prerendered_entries
)});\n\n` +
`export const base_path = ${JSON.stringify(
builder.config.kit.paths.base
)};\n`
);
build.onResolve({ filter: /^STATICS/ }, () => {
return { path: `${tmp}/static-publisher/statics.js` };
});
build.onResolve({ filter: /^SERVER/ }, () => {
return { path: `${builder.getServerDirectory()}/index.js` };
});
build.onLoad(
{ filter: /.*/, namespace: "svelte-adapter-fastly" },
async () => {
return {
loader: "js",
resolveDir: tmp,
contents:
`export const manifest = ${builder.generateManifest({
relativePath,
})};\n\n` +
`export const prerendered = new Map(${JSON.stringify(
prerendered_entries
)});\n\n` +
`export const base_path = ${JSON.stringify(
builder.config.kit.paths.base
)};\n`,
};
}
);
},
},
],
});

builder.log("Building worker...");

console.log("creating wasm file");
execSync(
`npx --package @fastly/js-compute js-compute-runtime src/entry.js ./bin/main.wasm`,
`npx --package @fastly/js-compute js-compute-runtime ${
opts.experimentalTopLevelAwait
? `--enable-experimental-top-level-await`
: ""
} ${workerSource}/entry.js ./bin/main.wasm`,
{
cwd: `${tmp}`,
stdio: "inherit",
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
"release": "changeset publish"
},
"dependencies": {
"@fastly/compute-js-static-publish": "^6.1.0",
"@fastly/compute-js-static-publish": "^6.2.0",
"@iarna/toml": "^2.2.5",
"@fastly/js-compute": "^3.7.0"
"@fastly/js-compute": "^3.29.2",
"esbuild": "^0.24.2"
},
"devDependencies": {
"@changesets/cli": "^2.27.8",
Expand All @@ -54,8 +55,5 @@
"peerDependencies": {
"@sveltejs/kit": "^2.0.0"
},
"packageManager": "[email protected]",
"engines": {
"pnpm": "^9.0.0"
}
"packageManager": "[email protected]"
}
Loading

0 comments on commit 3d7c8f2

Please sign in to comment.