Skip to content

Commit 6b99851

Browse files
authored
chore: remove build step for adapter node (#10041)
1 parent 33d8f7b commit 6b99851

File tree

12 files changed

+117
-114
lines changed

12 files changed

+117
-114
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': minor
3+
---
4+
5+
chore: remove build step for adapter node

.changeset/three-hotels-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': minor
3+
---
4+
5+
feat: load sourcemaps so that they can be merged to point to original sourcefiles

packages/adapter-node/ambient.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
declare module 'ENV' {
2-
export function env(key: string, fallback?: any): string;
3-
}
4-
51
declare module 'HANDLER' {
62
export const handler: import('polka').Middleware;
73
}
@@ -17,6 +13,11 @@ declare module 'SERVER' {
1713
export { Server } from '@sveltejs/kit';
1814
}
1915

16+
interface ImportMeta {
17+
SERVER_DIR: string;
18+
ENV_PREFIX?: string;
19+
}
20+
2021
declare namespace App {
2122
export interface Platform {
2223
/**

packages/adapter-node/index.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Adapter } from '@sveltejs/kit';
22
import './ambient.js';
33

4-
declare global {
5-
const ENV_PREFIX: string;
6-
}
7-
84
interface AdapterOptions {
95
out?: string;
106
precompress?: boolean;

packages/adapter-node/index.js

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { readFileSync, writeFileSync } from 'node:fs';
2-
import { fileURLToPath } from 'node:url';
3-
import { rollup } from 'rollup';
4-
import { nodeResolve } from '@rollup/plugin-node-resolve';
51
import commonjs from '@rollup/plugin-commonjs';
62
import json from '@rollup/plugin-json';
3+
import { nodeResolve } from '@rollup/plugin-node-resolve';
4+
import { createFilter, normalizePath } from '@rollup/pluginutils';
5+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
6+
import { readFile } from 'node:fs/promises';
7+
import path from 'node:path';
8+
import { fileURLToPath } from 'node:url';
9+
import { rollup } from 'rollup';
710

8-
const files = fileURLToPath(new URL('./files', import.meta.url).href);
11+
/** @param {string} path */
12+
const resolve = (path) => fileURLToPath(new URL(path, import.meta.url));
913

1014
/** @type {import('.').default} */
1115
export default function (opts = {}) {
@@ -15,7 +19,8 @@ export default function (opts = {}) {
1519
name: '@sveltejs/adapter-node',
1620

1721
async adapt(builder) {
18-
const tmp = builder.getBuildDirectory('adapter-node');
22+
// use an adjacent temporary directory so that any relative paths in eg. sourcemaps don't break
23+
const tmp = path.join(path.dirname(builder.getServerDirectory()), 'adapter-node');
1924

2025
builder.rimraf(out);
2126
builder.rimraf(tmp);
@@ -50,45 +55,85 @@ export default function (opts = {}) {
5055
// will get included in the bundled code
5156
const bundle = await rollup({
5257
input: {
53-
index: `${tmp}/index.js`,
54-
manifest: `${tmp}/manifest.js`
58+
handler: resolve('./src/handler.js'),
59+
index: resolve('./src/index.js')
5560
},
5661
external: [
5762
// dependencies could have deep exports, so we need a regex
5863
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
5964
],
6065
plugins: [
66+
{
67+
name: 'adapter-node-resolve',
68+
resolveId(id) {
69+
switch (id) {
70+
case 'MANIFEST':
71+
return `${tmp}/manifest.js`;
72+
case 'SERVER':
73+
return `${tmp}/index.js`;
74+
case 'SHIMS':
75+
return '\0virtual:SHIMS';
76+
}
77+
},
78+
load(id) {
79+
if (id === '\0virtual:SHIMS') {
80+
return polyfill
81+
? "import { installPolyfills } from '@sveltejs/kit/node/polyfills'; installPolyfills();"
82+
: '';
83+
}
84+
},
85+
resolveImportMeta(property, { chunkId, moduleId }) {
86+
if (property === 'SERVER_DIR' && moduleId === resolve('./src/handler.js')) {
87+
const segments = chunkId.split('/').length - 1;
88+
89+
return `new URL("${'../'.repeat(segments) || '.'}", import.meta.url)`;
90+
} else if (property === 'ENV_PREFIX' && moduleId === resolve('./src/env.js')) {
91+
return JSON.stringify(envPrefix);
92+
}
93+
}
94+
},
6195
nodeResolve({
6296
preferBuiltins: true,
6397
exportConditions: ['node']
6498
}),
6599
commonjs({ strictRequires: true }),
66-
json()
100+
json(),
101+
merge_sourcemap_plugin(tmp)
67102
]
68103
});
69104

70105
await bundle.write({
71-
dir: `${out}/server`,
106+
dir: out,
72107
format: 'esm',
73108
sourcemap: true,
74-
chunkFileNames: 'chunks/[name]-[hash].js'
109+
chunkFileNames: 'server/chunks/[name]-[hash].js',
110+
// without this rollup will insert some imports to try speed up
111+
// module loading but it doesn't really affect anything on the server side
112+
hoistTransitiveImports: false
75113
});
114+
}
115+
};
116+
}
76117

77-
builder.copy(files, out, {
78-
replace: {
79-
ENV: './env.js',
80-
HANDLER: './handler.js',
81-
MANIFEST: './server/manifest.js',
82-
SERVER: './server/index.js',
83-
SHIMS: './shims.js',
84-
ENV_PREFIX: JSON.stringify(envPrefix)
85-
}
86-
});
118+
/**
119+
* Load sourcemaps for files in the tmp directory so that the final ones
120+
* point to the original source files, instead of the generated files in outDir.
121+
* @param {string} tmp
122+
* @returns {import('rollup').Plugin}
123+
*/
124+
function merge_sourcemap_plugin(tmp) {
125+
const should_process_sourcemaps = createFilter(`${normalizePath(tmp)}/**/*.js`);
87126

88-
// If polyfills aren't wanted then clear the file
89-
if (!polyfill) {
90-
writeFileSync(`${out}/shims.js`, '', 'utf-8');
91-
}
127+
return {
128+
name: 'adapter-node-sourcemap-loader',
129+
async load(id) {
130+
if (!should_process_sourcemaps(id)) return;
131+
if (!existsSync(`${id}.map`)) return;
132+
const [code, map] = await Promise.all([
133+
readFile(id, 'utf-8'),
134+
readFile(`${id}.map`, 'utf-8')
135+
]);
136+
return { code, map };
92137
}
93138
};
94139
}

packages/adapter-node/package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,32 @@
1919
},
2020
"types": "index.d.ts",
2121
"files": [
22-
"files",
22+
"src",
2323
"index.js",
2424
"index.d.ts"
2525
],
2626
"scripts": {
27-
"dev": "rimraf files && rollup -cw",
28-
"build": "rimraf files && rollup -c",
2927
"test": "echo \"tests temporarily disabled\" # c8 vitest run",
3028
"check": "tsc",
3129
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
32-
"format": "pnpm lint --write",
33-
"prepublishOnly": "pnpm build"
30+
"format": "pnpm lint --write"
3431
},
3532
"devDependencies": {
36-
"@polka/url": "^1.0.0-next.21",
3733
"@sveltejs/kit": "workspace:^",
3834
"@types/node": "^16.18.6",
3935
"c8": "^8.0.0",
40-
"polka": "^1.0.0-next.22",
41-
"rimraf": "^5.0.0",
42-
"sirv": "^2.0.3",
4336
"typescript": "^4.9.4",
4437
"vitest": "^0.32.2"
4538
},
4639
"dependencies": {
40+
"@polka/url": "^1.0.0-next.21",
4741
"@rollup/plugin-commonjs": "^25.0.0",
4842
"@rollup/plugin-json": "^6.0.0",
4943
"@rollup/plugin-node-resolve": "^15.0.1",
50-
"rollup": "^3.7.0"
44+
"@rollup/pluginutils": "^5.0.2",
45+
"polka": "^1.0.0-next.22",
46+
"rollup": "^3.7.0",
47+
"sirv": "^2.0.2"
5148
},
5249
"peerDependencies": {
5350
"@sveltejs/kit": "^1.0.0"

packages/adapter-node/rollup.config.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/adapter-node/src/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global ENV_PREFIX */
1+
export const ENV_PREFIX = import.meta.ENV_PREFIX;
22

33
const expected = new Set([
44
'SOCKET_PATH',

packages/adapter-node/src/handler.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import 'SHIMS';
2+
23
import fs from 'node:fs';
34
import path from 'node:path';
4-
import sirv from 'sirv';
55
import { fileURLToPath } from 'node:url';
6+
67
import { parse as polka_url_parser } from '@polka/url';
78
import { getRequest, setResponse } from '@sveltejs/kit/node';
8-
import { Server } from 'SERVER';
9-
import { manifest, prerendered } from 'MANIFEST';
10-
import { env } from 'ENV';
9+
import sirv from 'sirv';
1110

12-
/* global ENV_PREFIX */
11+
import { env, ENV_PREFIX } from './env.js';
12+
import { manifest, prerendered } from 'MANIFEST';
13+
import { Server } from 'SERVER';
1314

1415
const server = new Server(manifest);
1516
await server.init({ env: process.env });
@@ -20,7 +21,7 @@ const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
2021
const host_header = env('HOST_HEADER', 'host').toLowerCase();
2122
const body_size_limit = parseInt(env('BODY_SIZE_LIMIT', '524288'));
2223

23-
const dir = path.dirname(fileURLToPath(import.meta.url));
24+
const dir = fileURLToPath(import.meta.SERVER_DIR);
2425

2526
/**
2627
* @param {string} path

packages/adapter-node/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { handler } from 'HANDLER';
2-
import { env } from 'ENV';
1+
import { handler } from './handler.js';
2+
import { env } from './env.js';
33
import polka from 'polka';
44

55
export const path = env('SOCKET_PATH', false);

0 commit comments

Comments
 (0)