Skip to content

Commit 0b780a6

Browse files
authored
Re-bundle server app in adapter-node (#1648)
* prepare adapter-node runtime for bundling * add esbuild as dependency of adapter-node * bundle app as part of adapter-node's adapt phase * add changeset * add require() shim for requires not transformed by esbuild esbuild doesn't seem to transform require()s of Node builtins into imports, so define a global.require for them.
1 parent b1aa656 commit 0b780a6

File tree

7 files changed

+31
-10
lines changed

7 files changed

+31
-10
lines changed

.changeset/tiny-socks-sparkle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': patch
3+
---
4+
5+
Bundle server-side app during adapt phase

packages/adapter-node/index.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { copyFileSync } from 'fs';
1+
import { readFileSync } from 'fs';
22
import { join } from 'path';
33
import { fileURLToPath } from 'url';
4+
import esbuild from 'esbuild';
45

56
/**
67
* @param {{
@@ -18,11 +19,17 @@ export default function ({ out = 'build' } = {}) {
1819
utils.copy_client_files(static_directory);
1920
utils.copy_static_files(static_directory);
2021

21-
utils.log.minor('Copying server');
22-
utils.copy_server_files(out);
23-
22+
utils.log.minor('Building server');
2423
const files = fileURLToPath(new URL('./files', import.meta.url));
25-
copyFileSync(`${files}/server.js`, `${out}/index.js`);
24+
utils.copy(files, '.svelte-kit/node');
25+
await esbuild.build({
26+
entryPoints: ['.svelte-kit/node/index.js'],
27+
outfile: join(out, 'index.js'),
28+
bundle: true,
29+
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
30+
format: 'esm',
31+
platform: 'node'
32+
});
2633

2734
utils.log.minor('Prerendering static pages');
2835
await utils.prerender({

packages/adapter-node/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
2121
"prepublishOnly": "npm run build"
2222
},
23+
"dependencies": {
24+
"esbuild": "^0.12.5"
25+
},
2326
"devDependencies": {
2427
"@rollup/plugin-json": "^4.1.0",
2528
"@sveltejs/kit": "workspace:*",

packages/adapter-node/rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import json from '@rollup/plugin-json';
55
export default {
66
input: 'src/index.js',
77
output: {
8-
file: 'files/server.js',
8+
file: 'files/index.js',
99
format: 'esm',
1010
sourcemap: true
1111
},
1212
plugins: [nodeResolve(), commonjs(), json()],
13-
external: ['./app.js', ...require('module').builtinModules]
13+
external: ['../output/server/app.js', ...require('module').builtinModules]
1414
};

packages/adapter-node/src/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import './require_shim';
12
import { createServer } from './server';
2-
/*eslint import/no-unresolved: [2, { ignore: ['\.\/app\.js$'] }]*/
3-
import * as app from './app.js';
3+
// TODO hardcoding the relative location makes this brittle
4+
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
45

56
const { HOST = '0.0.0.0', PORT = 3000 } = process.env;
67

7-
const instance = createServer({ render: app.render }).listen(PORT, HOST, () => {
8+
const instance = createServer({ render }).listen(PORT, HOST, () => {
89
console.log(`Listening on port ${PORT}`);
910
});
1011

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createRequire } from 'module';
2+
global.require = createRequire(import.meta.url);

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)