Skip to content

Commit

Permalink
remove build step for adapter node
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan committed Apr 25, 2023
1 parent f940f1c commit 7bbb9d2
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 91 deletions.
6 changes: 6 additions & 0 deletions packages/adapter-node/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module 'ENV' {
export function env(key: string, fallback?: any): string;
export const ENV_PREFIX: string;
}

declare module 'HANDLER' {
Expand All @@ -17,6 +18,11 @@ declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

interface ImportMeta {
SERVER_DIR: string;
ENV_PREFIX: string;
}

declare namespace App {
export interface Platform {
/**
Expand Down
4 changes: 0 additions & 4 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

declare global {
const ENV_PREFIX: string;
}

interface AdapterOptions {
out?: string;
precompress?: boolean;
Expand Down
60 changes: 37 additions & 23 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { readFileSync, writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

const files = fileURLToPath(new URL('./files', import.meta.url).href);
/**
* @param {string} path
* @returns
*/
const resolve = (path) => fileURLToPath(new URL(path, import.meta.url));

/** @type {import('.').default} */
export default function (opts = {}) {
Expand Down Expand Up @@ -50,14 +54,40 @@ export default function (opts = {}) {
// will get included in the bundled code
const bundle = await rollup({
input: {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
handler: resolve('./src/handler.js'),
index: resolve('./src/index.js')
},
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
],
plugins: [
{
name: 'adapter-node-resolve',
resolveId(id) {
switch (id) {
case 'ENV':
return resolve('./src/env.js');
case 'HANDLER':
return resolve('./src/handler.js');
case 'MANIFEST':
return `${tmp}/manifest.js`;
case 'SERVER':
return `${tmp}/index.js`;
case 'SHIMS':
return resolve(polyfill ? './src/shims.js' : './src/shims_empty.js');
}
},
resolveImportMeta(property, { chunkId, moduleId }) {
if (property === 'SERVER_DIR' && moduleId === resolve('./src/handler.js')) {
const segments = chunkId.split('/').length - 1;

return `new URL("${'../'.repeat(segments) || '.'}", import.meta.url)`;
} else if (property === 'ENV_PREFIX' && moduleId === resolve('./src/env.js')) {
return JSON.stringify(envPrefix);
}
}
},
nodeResolve({
preferBuiltins: true,
exportConditions: ['node']
Expand All @@ -68,27 +98,11 @@ export default function (opts = {}) {
});

await bundle.write({
dir: `${out}/server`,
dir: `${out}`,
format: 'esm',
sourcemap: true,
chunkFileNames: `chunks/[name]-[hash].js`
});

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

// If polyfills aren't wanted then clear the file
if (!polyfill) {
writeFileSync(`${out}/shims.js`, '', 'utf-8');
}
}
};
}
15 changes: 7 additions & 8 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,34 @@
},
"types": "index.d.ts",
"files": [
"files",
"src",
"index.js",
"index.d.ts"
],
"scripts": {
"dev": "rimraf files && rollup -cw",
"build": "rimraf files && rollup -c",
"test": "echo \"tests temporarily disabled\" # c8 uvu tests",
"check": "tsc",
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"format": "pnpm lint --write",
"prepublishOnly": "pnpm build"
},
"devDependencies": {
"@polka/url": "^1.0.0-next.21",
"@sveltejs/kit": "workspace:^",
"@types/node": "^16.18.6",
"c8": "^7.12.0",
"polka": "^1.0.0-next.22",
"rimraf": "^5.0.0",
"sirv": "^2.0.2",
"typescript": "^4.9.4",
"uvu": "^0.5.6"
},
"dependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"rollup": "^3.7.0"
"@rollup/plugin-replace": "^5.0.2",
"rollup": "^3.7.0",
"@polka/url": "^1.0.0-next.21",
"undici": "~5.22.0",
"polka": "^1.0.0-next.22",
"sirv": "^2.0.2"
},
"peerDependencies": {
"@sveltejs/kit": "^1.0.0"
Expand Down
44 changes: 0 additions & 44 deletions packages/adapter-node/rollup.config.js

This file was deleted.

4 changes: 3 additions & 1 deletion packages/adapter-node/src/env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global ENV_PREFIX */
/* global __ENV_PREFIX */

export const ENV_PREFIX = import.meta.ENV_PREFIX;

const expected = new Set([
'SOCKET_PATH',
Expand Down
13 changes: 7 additions & 6 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'SHIMS';

import fs from 'node:fs';
import path from 'node:path';
import sirv from 'sirv';
import { fileURLToPath } from 'node:url';

import { parse as polka_url_parser } from '@polka/url';
import { getRequest, setResponse } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { env } from 'ENV';
import sirv from 'sirv';

/* global ENV_PREFIX */
import { env, ENV_PREFIX } from './env.js';
import { manifest, prerendered } from 'MANIFEST';
import { Server } from 'SERVER';

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

const dir = path.dirname(fileURLToPath(import.meta.url));
const dir = fileURLToPath(import.meta.SERVER_DIR);

/**
* @param {string} path
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 './handler.js';
import { env } from './env.js';
import polka from 'polka';

export const path = env('SOCKET_PATH', false);
Expand Down
Empty file.
6 changes: 3 additions & 3 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 7bbb9d2

Please sign in to comment.