From 738e2d4f40a149670ba035402611c24413a9ca03 Mon Sep 17 00:00:00 2001 From: Jelmer Bot Date: Wed, 8 May 2024 15:49:45 +0200 Subject: [PATCH 1/4] detect build-emitted assets in prerenderer --- .../kit/src/core/generate_manifest/index.js | 9 ++++ packages/kit/src/core/postbuild/prerender.js | 5 +++ packages/kit/src/exports/vite/index.js | 1 + .../paths-base/custom_asset_plugin.js | 41 +++++++++++++++++++ .../src/routes/assets/emitted/+page.js | 7 ++++ .../src/routes/assets/emitted/+page.svelte | 4 ++ .../src/routes/assets/emitted/message.csv | 1 + .../paths-base/test/tests.spec.js | 5 +++ .../prerendering/paths-base/vite.config.js | 3 +- 9 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 packages/kit/test/prerendering/paths-base/custom_asset_plugin.js create mode 100644 packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js create mode 100644 packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte create mode 100644 packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/message.csv diff --git a/packages/kit/src/core/generate_manifest/index.js b/packages/kit/src/core/generate_manifest/index.js index a2ef4cd4c619..2e63bc5c47a3 100644 --- a/packages/kit/src/core/generate_manifest/index.js +++ b/packages/kit/src/core/generate_manifest/index.js @@ -80,6 +80,15 @@ export function generate_manifest({ build_data, relative_path, routes }) { mime_types[ext] ??= mime.lookup(ext) || ''; } + const emitted_asset_dir = path.resolve(build_data.out_dir, 'server', build_data.asset_prefix); + if (fs.existsSync(emitted_asset_dir)) { + for (const file of fs.readdirSync(emitted_asset_dir)) { + assets.push(path.join(build_data.asset_prefix, file).replaceAll(path.sep, '/')); + const ext = path.extname(file); + mime_types[ext] ??= mime.lookup(ext) || ''; + } + } + // prettier-ignore // String representation of /** @template {import('@sveltejs/kit').SSRManifest} T */ diff --git a/packages/kit/src/core/postbuild/prerender.js b/packages/kit/src/core/postbuild/prerender.js index 05c995ab9771..76394585a941 100644 --- a/packages/kit/src/core/postbuild/prerender.js +++ b/packages/kit/src/core/postbuild/prerender.js @@ -211,6 +211,11 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) { const filepath = saved.get(file); if (filepath) return readFileSync(filepath); + // Static assets emitted during build + if (file.startsWith(config.appDir)) { + return readFileSync(join(config.outDir, 'output', 'server', file)); + } + // stuff in `static` return readFileSync(join(config.files.assets, file)); }, diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index c91055ba9aa7..4b19eda1492d 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -754,6 +754,7 @@ async function kit({ svelte_config }) { app_path: `${kit.paths.base.slice(1)}${kit.paths.base ? '/' : ''}${kit.appDir}`, manifest_data, out_dir: out, + asset_prefix: vite_config.build.rollupOptions.output.assetFileNames.split('/[')[0], service_worker: service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable? client: null, server_manifest diff --git a/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js b/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js new file mode 100644 index 000000000000..e39b7d094d3c --- /dev/null +++ b/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js @@ -0,0 +1,41 @@ +import fs from 'node:fs'; +import path from 'node:path'; + +export default function () { + let config; + const ext = '.csv'; + return { + name: 'vite-plugin-sveltekit-custom-asset', + resolveId(id) { + if (id.endsWith(ext)) { + return id; + } + }, + configResolved(resolvedConfig) { + config = resolvedConfig; + }, + load(id, options) { + if (!id.endsWith(ext)) return; + if (config.command === 'serve') + return `export default "${path.relative(process.cwd(), id).replaceAll(path.sep, '/')}";`; + + const outName = config.build.rollupOptions.output.assetFileNames + .replace('[name]', path.basename(id, ext)) + .replace('[hash]', '000000') + .replace('[extname]', ext); + + if (config.build?.ssr) { + const ref = this.emitFile({ + type: 'asset', + fileName: outName + }); + fs.readFile(id, (err, data) => { + if (err) this.error(err); + this.setAssetSource(ref, data); + }); + } + + return `export default "${config.base}${outName}";`; + } + }; +} diff --git a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js new file mode 100644 index 000000000000..ad8947e2176d --- /dev/null +++ b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js @@ -0,0 +1,7 @@ +import url from "./message.csv" + +export async function load({fetch}) { + const response = await fetch(url); + const asset = await response.text(); + return { asset } +} \ No newline at end of file diff --git a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte new file mode 100644 index 000000000000..ea472f490d29 --- /dev/null +++ b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte @@ -0,0 +1,4 @@ + +

{data.asset}

\ No newline at end of file diff --git a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/message.csv b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/message.csv new file mode 100644 index 000000000000..07beee08078d --- /dev/null +++ b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/message.csv @@ -0,0 +1 @@ +A custom asset emitted by a vite plugin during build. \ No newline at end of file diff --git a/packages/kit/test/prerendering/paths-base/test/tests.spec.js b/packages/kit/test/prerendering/paths-base/test/tests.spec.js index 77674df89c93..7ef974373ae1 100644 --- a/packages/kit/test/prerendering/paths-base/test/tests.spec.js +++ b/packages/kit/test/prerendering/paths-base/test/tests.spec.js @@ -29,3 +29,8 @@ test('prerenders /path-base/assets', () => { const content = read('assets.html'); assert.match(content, /]+src="\/path-base\//u); }); + +test('prerenders /path-base/assets/emitted', () => { + const content = read('assets/emitted.html'); + assert.ok(content.includes('

A custom asset emitted by a vite plugin during build.

')); +}); \ No newline at end of file diff --git a/packages/kit/test/prerendering/paths-base/vite.config.js b/packages/kit/test/prerendering/paths-base/vite.config.js index 2857b2592d11..cd49ec622d63 100644 --- a/packages/kit/test/prerendering/paths-base/vite.config.js +++ b/packages/kit/test/prerendering/paths-base/vite.config.js @@ -1,5 +1,6 @@ import * as path from 'node:path'; import { sveltekit } from '@sveltejs/kit/vite'; +import csvasset from './custom_asset_plugin.js'; /** @type {import('vite').UserConfig} */ const config = { @@ -13,7 +14,7 @@ const config = { logLevel: 'silent', - plugins: [sveltekit()], + plugins: [csvasset(), sveltekit()], server: { fs: { From a216d8c826d90bce18a0544efacbc9f3937d1756 Mon Sep 17 00:00:00 2001 From: Jelmer Bot Date: Wed, 8 May 2024 16:24:48 +0200 Subject: [PATCH 2/4] lint and check fixes --- packages/kit/src/exports/vite/index.js | 2 +- packages/kit/src/types/internal.d.ts | 1 + .../prerendering/paths-base/custom_asset_plugin.js | 2 +- .../paths-base/src/routes/assets/emitted/+page.js | 13 +++++++------ .../src/routes/assets/emitted/+page.svelte | 5 +++-- .../test/prerendering/paths-base/test/tests.spec.js | 2 +- packages/kit/types/index.d.ts | 1 + 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 4b19eda1492d..393171283d0b 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -754,7 +754,7 @@ async function kit({ svelte_config }) { app_path: `${kit.paths.base.slice(1)}${kit.paths.base ? '/' : ''}${kit.appDir}`, manifest_data, out_dir: out, - asset_prefix: vite_config.build.rollupOptions.output.assetFileNames.split('/[')[0], + asset_prefix: `${kit.appDir}/immutable/assets`, service_worker: service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable? client: null, server_manifest diff --git a/packages/kit/src/types/internal.d.ts b/packages/kit/src/types/internal.d.ts index af50cbe74c7d..38c6350c12c3 100644 --- a/packages/kit/src/types/internal.d.ts +++ b/packages/kit/src/types/internal.d.ts @@ -63,6 +63,7 @@ export interface BuildData { app_path: string; manifest_data: ManifestData; out_dir: string; + asset_prefix: string; service_worker: string | null; client: { start: string; diff --git a/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js b/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js index e39b7d094d3c..3a28d6c3cabe 100644 --- a/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js +++ b/packages/kit/test/prerendering/paths-base/custom_asset_plugin.js @@ -14,7 +14,7 @@ export default function () { configResolved(resolvedConfig) { config = resolvedConfig; }, - load(id, options) { + load(id) { if (!id.endsWith(ext)) return; if (config.command === 'serve') return `export default "${path.relative(process.cwd(), id).replaceAll(path.sep, '/')}";`; diff --git a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js index ad8947e2176d..ecd0785e6b06 100644 --- a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js +++ b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.js @@ -1,7 +1,8 @@ -import url from "./message.csv" +// @ts-ignore +import url from './message.csv'; -export async function load({fetch}) { - const response = await fetch(url); - const asset = await response.text(); - return { asset } -} \ No newline at end of file +export async function load({ fetch }) { + const response = await fetch(url); + const asset = await response.text(); + return { asset }; +} diff --git a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte index ea472f490d29..33b39b7e03cd 100644 --- a/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte +++ b/packages/kit/test/prerendering/paths-base/src/routes/assets/emitted/+page.svelte @@ -1,4 +1,5 @@ -

{data.asset}

\ No newline at end of file + +

{data.asset}

diff --git a/packages/kit/test/prerendering/paths-base/test/tests.spec.js b/packages/kit/test/prerendering/paths-base/test/tests.spec.js index 7ef974373ae1..1ea39a737327 100644 --- a/packages/kit/test/prerendering/paths-base/test/tests.spec.js +++ b/packages/kit/test/prerendering/paths-base/test/tests.spec.js @@ -33,4 +33,4 @@ test('prerenders /path-base/assets', () => { test('prerenders /path-base/assets/emitted', () => { const content = read('assets/emitted.html'); assert.ok(content.includes('

A custom asset emitted by a vite plugin during build.

')); -}); \ No newline at end of file +}); diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 1219176d5665..bb2a23166a95 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1587,6 +1587,7 @@ declare module '@sveltejs/kit' { app_path: string; manifest_data: ManifestData; out_dir: string; + asset_prefix: string; service_worker: string | null; client: { start: string; From 0fd65d309f029b36de41e28a3e8946d893e9385f Mon Sep 17 00:00:00 2001 From: Jelmer Bot Date: Wed, 8 May 2024 16:31:41 +0200 Subject: [PATCH 3/4] add changeset --- .changeset/chilled-humans-roll.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chilled-humans-roll.md diff --git a/.changeset/chilled-humans-roll.md b/.changeset/chilled-humans-roll.md new file mode 100644 index 000000000000..f9b97341116a --- /dev/null +++ b/.changeset/chilled-humans-roll.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +Detect assets emitted by vite plugins in prerenderer `fetch`. From 59e62e6c3bb58c2e35c5df3416205246af18b3d4 Mon Sep 17 00:00:00 2001 From: Jelmer Bot Date: Mon, 12 Aug 2024 12:45:30 +0200 Subject: [PATCH 4/4] Update .changeset/chilled-humans-roll.md Co-authored-by: Tee Ming --- .changeset/chilled-humans-roll.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/chilled-humans-roll.md b/.changeset/chilled-humans-roll.md index f9b97341116a..14e779a03a54 100644 --- a/.changeset/chilled-humans-roll.md +++ b/.changeset/chilled-humans-roll.md @@ -2,4 +2,4 @@ "@sveltejs/kit": patch --- -Detect assets emitted by vite plugins in prerenderer `fetch`. +fix: detect assets emitted by vite plugins in prerenderer `fetch`.