Skip to content

Commit

Permalink
Re-bundle server app in adapter-node (#1648)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Conduitry authored Jun 10, 2021
1 parent b1aa656 commit 0b780a6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
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.

0 comments on commit 0b780a6

Please sign in to comment.