Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/major-lizards-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

fix: bundle entrypoints alongside app code
5 changes: 0 additions & 5 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

declare global {
const ENV_PREFIX: string;
const PRECOMPRESS: boolean;
}

interface AdapterOptions {
out?: string;
precompress?: boolean;
Expand Down
68 changes: 37 additions & 31 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';

/**
* @template T
Expand Down Expand Up @@ -46,27 +47,23 @@ export default function (opts = {}) {

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')
);
// Copy the entrypoints into `.svelte-kit/adapter-node/entries`,
// so that node modules are correctly resolved
builder.copy(files, `${tmp}/entries`);

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
const server = builder.getServerDirectory();

/** @type {Record<string, string>} */
const input = {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
index: `${tmp}/entries/index.js`,
env: `${tmp}/entries/env.js`,
handler: `${tmp}/entries/handler.js`,
shims: `${tmp}/entries/shims.js`
};

if (builder.hasServerInstrumentationFile?.()) {
input['instrumentation.server'] = `${tmp}/instrumentation.server.js`;
input['instrumentation.server'] = `${server}/instrumentation.server.js`;
}

// we bundle the Vite output so that deployments only need
Expand All @@ -79,40 +76,49 @@ export default function (opts = {}) {
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
],
plugins: [
{
name: 'adapter-node:alias',
resolveId(id) {
if (id === 'SERVER') return `${server}/index.js`;
if (id === 'MANIFEST') return `${server}/manifest.js`;
}
},
nodeResolve({
preferBuiltins: true,
exportConditions: ['node']
}),
// @ts-ignore https://github.com/rollup/plugins/issues/1329
Comment thread
vercel[bot] marked this conversation as resolved.
// @ts-expect-error https://github.com/rollup/plugins/issues/1329
replace({
// only replace tokens in the adapter's own entrypoints, so that
// identifiers like `BASE` in the user's app code or bundled
// dependencies aren't accidentally replaced
include: [`${tmp}/entries/**`],
values: {
BASE: JSON.stringify(builder.config.kit.paths.base),
ENV_PREFIX: JSON.stringify(envPrefix),
PRECOMPRESS: JSON.stringify(precompress),
PRERENDERED: `new Set(${JSON.stringify(builder.prerendered.paths)})`
},
preventAssignment: true
}),
// @ts-expect-error https://github.com/rollup/plugins/issues/1329
commonjs({ strictRequires: true }),
// @ts-ignore https://github.com/rollup/plugins/issues/1329
// @ts-expect-error https://github.com/rollup/plugins/issues/1329
json()
]
});

await bundle.write({
dir: `${out}/server`,
dir: out,
format: 'esm',
sourcemap: true,
chunkFileNames: 'chunks/[name]-[hash].js'
});

builder.copy(files, out, {
replace: {
ENV: './env.js',
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: './server/index.js',
SHIMS: './shims.js',
ENV_PREFIX: JSON.stringify(envPrefix),
PRECOMPRESS: JSON.stringify(precompress)
}
chunkFileNames: 'server/chunks/[name]-[hash].js'
});

if (builder.hasServerInstrumentationFile?.()) {
builder.instrument?.({
entrypoint: `${out}/index.js`,
instrumentation: `${out}/server/instrumentation.server.js`,
instrumentation: `${out}/instrumentation.server.js`,
module: {
exports: ['path', 'host', 'port', 'server']
}
Expand Down
17 changes: 4 additions & 13 deletions packages/adapter-node/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
declare module 'ENV' {
export function env(key: string, fallback?: any): string;
export function timeout_env(key: string, fallback?: any): number | undefined;
}

declare module 'HANDLER' {
export const handler: import('polka').Middleware;
}

declare module 'MANIFEST' {
import { SSRManifest } from '@sveltejs/kit';

export const base: string;
export const manifest: SSRManifest;
export const prerendered: Set<string>;
}

declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'SHIMS' {}
declare const BASE: string;
declare const ENV_PREFIX: string;
declare const PRECOMPRESS: boolean;
declare const PRERENDERED: Set<string>;
1 change: 1 addition & 0 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@rollup/plugin-commonjs": "^29.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-replace": "^6.0.3",
"rollup": "^4.59.0"
},
"peerDependencies": {
Expand Down
74 changes: 20 additions & 54 deletions packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,59 +35,25 @@ function prefixBuiltinModules() {
};
}

export default [
{
input: 'src/index.js',
output: {
file: 'files/index.js',
format: 'esm'
},
plugins: [
clearOutput('files/index.js'),
nodeResolve({ preferBuiltins: true }),
commonjs(),
json(),
prefixBuiltinModules()
],
external: ['ENV', 'HANDLER']
export default {
input: {
index: 'src/index.js',
env: 'src/env.js',
handler: 'src/handler.js',
shims: 'src/shims.js'
},
{
input: 'src/env.js',
output: {
file: 'files/env.js',
format: 'esm'
},
plugins: [
clearOutput('files/env.js'),
nodeResolve(),
commonjs(),
json(),
prefixBuiltinModules()
],
external: ['HANDLER']
output: {
dir: 'files',
format: 'esm',
hoistTransitiveImports: false,
chunkFileNames: 'chunks/[hash].js'
},
{
input: 'src/handler.js',
output: {
file: 'files/handler.js',
format: 'esm',
inlineDynamicImports: true
},
plugins: [
clearOutput('files/handler.js'),
nodeResolve(),
commonjs(),
json(),
prefixBuiltinModules()
],
external: ['ENV', 'MANIFEST', 'SERVER', 'SHIMS']
},
{
input: 'src/shims.js',
output: {
file: 'files/shims.js',
format: 'esm'
},
plugins: [clearOutput('files/shims.js'), nodeResolve(), commonjs(), prefixBuiltinModules()]
}
];
plugins: [
clearOutput('files'),
nodeResolve({ preferBuiltins: true }),
commonjs(),
json(),
prefixBuiltinModules()
],
external: ['MANIFEST', 'SERVER', '@sveltejs/kit/node', '@sveltejs/kit/node/polyfills']
};
2 changes: 1 addition & 1 deletion packages/adapter-node/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (ENV_PREFIX) {

/**
* @param {string} name
* @param {any} fallback
* @param {any} [fallback]
*/
export function env(name, fallback) {
const prefix = expected_unprefixed.has(name) ? '' : ENV_PREFIX;
Expand Down
12 changes: 8 additions & 4 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'SHIMS';
import './shims.js';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
Expand All @@ -7,12 +7,16 @@ import { fileURLToPath } from 'node:url';
import { parse as polka_url_parser } from '@polka/url';
import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest, prerendered, base } from 'MANIFEST';
import { env } from 'ENV';
import { manifest } from 'MANIFEST';
import { env } from './env.js';
import { parse_as_bytes, parse_origin } from '../utils.js';

/* global BASE */
/* global ENV_PREFIX */
/* global PRECOMPRESS */
/* global PRERENDERED */

const prerendered = PRERENDERED;

const server = new Server(manifest);

Expand All @@ -35,7 +39,7 @@ if (isNaN(body_size_limit)) {

const dir = path.dirname(fileURLToPath(import.meta.url));

const asset_dir = `${dir}/client${base}`;
const asset_dir = `${dir}/client${BASE}`;

await server.init({
env: /** @type {Record<string, string>} */ (process.env),
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'node:http';
import process from 'node:process';
import { handler } from 'HANDLER';
import { env, timeout_env } from 'ENV';
import { handler } from './handler.js';
import { env, timeout_env } from './env.js';
import polka from 'polka';
import { format_listening_address } from '../utils.js';

Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading