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

create commonjs bundle with adapter-node, since esm output has issues #6855

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-crews-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

Create CommonJS bundle with adapter-node
8 changes: 4 additions & 4 deletions packages/adapter-node/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
declare module 'ENV' {
declare module '0ENV' {
export function env(key: string, fallback?: any): string;
}

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

declare module 'MANIFEST' {
declare module '0MANIFEST' {
import { SSRManifest } from '@sveltejs/kit';
export const manifest: SSRManifest;
}

declare module 'SERVER' {
declare module '0SERVER' {
export { Server } from '@sveltejs/kit';
}
12 changes: 6 additions & 6 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ export default function (opts = {}) {
target: 'es2022',
entryPoints: [`${tmp}/index.js`, `${tmp}/manifest.js`],
outdir: `${out}/server`,
splitting: true,
format: 'esm',
bundle: true,
external: [...Object.keys(pkg.dependencies || {})]
});

builder.copy(files, out, {
replace: {
ENV: './env.js',
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: `./server/index.js`,
'0ENV': './env.js',
'0HANDLER': './handler.js',
'0MANIFEST': './server/manifest.js',
'0SERVER': `./server/index.js`,
ENV_PREFIX: JSON.stringify(envPrefix)
}
});

writeFileSync(`${out}/package.json`, JSON.stringify({ type: 'commonjs' }));
}
};
}
6 changes: 3 additions & 3 deletions packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default [
input: 'src/index.js',
output: {
file: 'files/index.js',
format: 'esm'
format: 'cjs'
},
plugins: [nodeResolve(), commonjs(), json()],
external: ['ENV', 'HANDLER', ...builtinModules]
Expand All @@ -17,7 +17,7 @@ export default [
input: 'src/env.js',
output: {
file: 'files/env.js',
format: 'esm'
format: 'cjs'
},
plugins: [nodeResolve(), commonjs(), json()],
external: ['HANDLER', ...builtinModules]
Expand All @@ -26,7 +26,7 @@ export default [
input: 'src/handler.js',
output: {
file: 'files/handler.js',
format: 'esm',
format: 'cjs',
inlineDynamicImports: true
},
plugins: [nodeResolve(), commonjs(), json()],
Expand Down
11 changes: 7 additions & 4 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import path from 'path';
import sirv from 'sirv';
import { fileURLToPath } from 'url';
import { getRequest, setResponse } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest } from 'MANIFEST';
import { env } from 'ENV';
import { Server } from '0SERVER';
import { manifest } from '0MANIFEST';
import { env } from '0ENV';

/* global ENV_PREFIX */

const server = new Server(manifest);
await server.init({ env: process.env });
const ready = server.init({ env: process.env });

const origin = env('ORIGIN', undefined);
const xff_depth = parseInt(env('XFF_DEPTH', '1'));
const address_header = env('ADDRESS_HEADER', '').toLowerCase();
Expand Down Expand Up @@ -48,6 +49,8 @@ function serve(path, client = false) {
const ssr = async (req, res) => {
let request;

await ready;

try {
request = await getRequest({
base: origin || get_origin(req.headers),
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,5 +1,5 @@
import { handler } from 'HANDLER';
import { env } from 'ENV';
import { handler } from '0HANDLER';
import { env } from '0ENV';
import polka from 'polka';

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