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

Re-bundle server app in adapter-node #1648

Merged
merged 5 commits into from
Jun 10, 2021
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/tiny-socks-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

Bundle server-side app during adapt phase
17 changes: 12 additions & 5 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { copyFileSync } from 'fs';
import { readFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';

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

utils.log.minor('Copying server');
utils.copy_server_files(out);

utils.log.minor('Building server');
const files = fileURLToPath(new URL('./files', import.meta.url));
copyFileSync(`${files}/server.js`, `${out}/index.js`);
utils.copy(files, '.svelte-kit/node');
await esbuild.build({
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),
bundle: true,
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
format: 'esm',
platform: 'node'
});

utils.log.minor('Prerendering static pages');
await utils.prerender({
Expand Down
3 changes: 3 additions & 0 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build"
},
"dependencies": {
"esbuild": "^0.12.5"
},
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@sveltejs/kit": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import json from '@rollup/plugin-json';
export default {
input: 'src/index.js',
output: {
file: 'files/server.js',
file: 'files/index.js',
format: 'esm',
sourcemap: true
},
plugins: [nodeResolve(), commonjs(), json()],
external: ['./app.js', ...require('module').builtinModules]
external: ['../output/server/app.js', ...require('module').builtinModules]
};
7 changes: 4 additions & 3 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import './require_shim';
import { createServer } from './server';
/*eslint import/no-unresolved: [2, { ignore: ['\.\/app\.js$'] }]*/
import * as app from './app.js';
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved

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

const instance = createServer({ render: app.render }).listen(PORT, HOST, () => {
const instance = createServer({ render }).listen(PORT, HOST, () => {
console.log(`Listening on port ${PORT}`);
});

Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-node/src/require_shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createRequire } from 'module';
global.require = createRequire(import.meta.url);
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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