Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement serverAssets #11647

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const server_template = ({
}) => `
import root from '../root.${isSvelte5Plus() ? 'js' : 'svelte'}';
import { set_building, set_prerendering } from '__sveltekit/environment';
import { set_assets } from '__sveltekit/paths';
import { set_assets, set_server_assets } from '__sveltekit/paths';
import { set_private_env, set_public_env, set_safe_public_env } from '${runtime_directory}/shared-server.js';

export const options = {
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function get_hooks() {
};
}

export { set_assets, set_building, set_prerendering, set_private_env, set_public_env, set_safe_public_env };
export { set_assets, set_server_assets, set_building, set_prerendering, set_private_env, set_public_env, set_safe_public_env };
`;

// TODO need to re-run this whenever src/app.html or src/error.html are
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ export async function dev(vite, vite_config, svelte_config) {
);
set_fix_stack_trace(fix_stack_trace);

const { set_assets } = await vite.ssrLoadModule('__sveltekit/paths');
const { set_assets, set_server_assets } = await vite.ssrLoadModule('__sveltekit/paths');
set_assets(assets);
set_server_assets(process.cwd());

const server = new Server(manifest);

Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,14 @@ async function kit({ svelte_config }) {
return dedent`
export const base = ${global}?.base ?? ${s(base)};
export const assets = ${global}?.assets ?? ${assets ? s(assets) : 'base'};
export let serverAssets = '';
`;
}

return dedent`
export let base = ${s(base)};
export let assets = ${assets ? s(assets) : 'base'};
export let serverAssets = '';

export const relative = ${svelte_config.kit.paths.relative};

Expand All @@ -472,6 +474,11 @@ async function kit({ svelte_config }) {
export function set_assets(path) {
assets = initial.assets = path;
}

/** @param {string} path */
export function set_server_assets(path) {
serverAssets = path;
}
`;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ export async function preview(vite, vite_config, svelte_config) {
}

/** @type {import('types').ServerInternalModule} */
const { set_assets } = await import(pathToFileURL(join(dir, 'internal.js')).href);
const { set_assets, set_server_assets } = await import(
pathToFileURL(join(dir, 'internal.js')).href
);

/** @type {import('types').ServerModule} */
const { Server } = await import(pathToFileURL(join(dir, 'index.js')).href);

const { manifest } = await import(pathToFileURL(join(dir, 'manifest.js')).href);

set_assets(assets);
set_server_assets(dir);

const server = new Server(manifest);
await server.init({
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/app/paths/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { base, assets } from '__sveltekit/paths';
export { base, assets, serverAssets } from '__sveltekit/paths';
import { base } from '__sveltekit/paths';
import { resolve_route } from '../../../utils/routing.js';

Expand Down
14 changes: 14 additions & 0 deletions packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ export let base: '' | `/${string}`;
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

/**
* The directory containing assets imported by files on the server. On Node-based platforms, this allows you to read assets from the filesystem.
*
* @example
* ```js
* import fs from 'node:fs';
* import somefile from './somefile.txt';
* import { serverAssets } from '$app/paths';
*
* const data = fs.readFileSync(`${serverAssets}/${somefile}`, 'utf-8');
* ```
*/
export let serverAssets: string;

/**
* Populate a route ID with params to resolve a pathname.
* @example
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/types/ambient-private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ declare module '__sveltekit/environment' {
declare module '__sveltekit/paths' {
export let base: '' | `/${string}`;
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let serverAssets: string;
export let relative: boolean;
export function reset(): void;
export function override(paths: { base: string; assets: string }): void;
export function set_assets(path: string): void;
export function set_server_assets(path: string): void;
}
1 change: 1 addition & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ServerInternalModule {
set_private_env(environment: Record<string, string>): void;
set_public_env(environment: Record<string, string>): void;
set_safe_public_env(environment: Record<string, string>): void;
set_server_assets(path: string): void;
set_version(version: string): void;
set_fix_stack_trace(fix_stack_trace: (error: unknown) => string): void;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,20 @@ declare module '$app/paths' {
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

/**
* The directory containing assets imported by files on the server. On Node-based platforms, this allows you to read assets from the filesystem.
*
* @example
* ```js
* import fs from 'node:fs';
* import somefile from './somefile.txt';
* import { serverAssets } from '$app/paths';
*
* const data = fs.readFileSync(`${serverAssets}/${somefile}`, 'utf-8');
* ```
*/
export let serverAssets: string;

/**
* Populate a route ID with params to resolve a pathname.
* @example
Expand Down
Loading