From 7bbb9d2b24ef742a593353a39009fb6038f5652b Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Tue, 25 Apr 2023 12:00:32 +0545 Subject: [PATCH] remove build step for adapter node --- packages/adapter-node/ambient.d.ts | 6 +++ packages/adapter-node/index.d.ts | 4 -- packages/adapter-node/index.js | 60 +++++++++++++++--------- packages/adapter-node/package.json | 15 +++--- packages/adapter-node/rollup.config.js | 44 ----------------- packages/adapter-node/src/env.js | 4 +- packages/adapter-node/src/handler.js | 13 ++--- packages/adapter-node/src/index.js | 4 +- packages/adapter-node/src/shims_empty.js | 0 pnpm-lock.yaml | 6 +-- 10 files changed, 65 insertions(+), 91 deletions(-) delete mode 100644 packages/adapter-node/rollup.config.js create mode 100644 packages/adapter-node/src/shims_empty.js diff --git a/packages/adapter-node/ambient.d.ts b/packages/adapter-node/ambient.d.ts index 7d45ea6dc64a..15b57f6ebdd8 100644 --- a/packages/adapter-node/ambient.d.ts +++ b/packages/adapter-node/ambient.d.ts @@ -1,5 +1,6 @@ declare module 'ENV' { export function env(key: string, fallback?: any): string; + export const ENV_PREFIX: string; } declare module 'HANDLER' { @@ -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 { /** diff --git a/packages/adapter-node/index.d.ts b/packages/adapter-node/index.d.ts index 12ea6273dd66..c3b224198ddb 100644 --- a/packages/adapter-node/index.d.ts +++ b/packages/adapter-node/index.d.ts @@ -1,10 +1,6 @@ import { Adapter } from '@sveltejs/kit'; import './ambient.js'; -declare global { - const ENV_PREFIX: string; -} - interface AdapterOptions { out?: string; precompress?: boolean; diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index d5fa532dfe95..b7e20a216b0b 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -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 = {}) { @@ -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'] @@ -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'); - } } }; } diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index f17fd9e679ea..97a7eb906b03 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -19,13 +19,11 @@ }, "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", @@ -33,13 +31,9 @@ "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" }, @@ -47,7 +41,12 @@ "@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" diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js deleted file mode 100644 index 16c4a20c37a7..000000000000 --- a/packages/adapter-node/rollup.config.js +++ /dev/null @@ -1,44 +0,0 @@ -import { nodeResolve } from '@rollup/plugin-node-resolve'; -import commonjs from '@rollup/plugin-commonjs'; -import json from '@rollup/plugin-json'; -import { builtinModules } from 'node:module'; - -export default [ - { - input: 'src/index.js', - output: { - file: 'files/index.js', - format: 'esm' - }, - plugins: [nodeResolve({ preferBuiltins: true }), commonjs(), json()], - external: ['ENV', 'HANDLER', ...builtinModules] - }, - { - input: 'src/env.js', - output: { - file: 'files/env.js', - format: 'esm' - }, - plugins: [nodeResolve(), commonjs(), json()], - external: ['HANDLER', ...builtinModules] - }, - { - input: 'src/handler.js', - output: { - file: 'files/handler.js', - format: 'esm', - inlineDynamicImports: true - }, - plugins: [nodeResolve(), commonjs(), json()], - external: ['ENV', 'MANIFEST', 'SERVER', 'SHIMS', ...builtinModules] - }, - { - input: 'src/shims.js', - output: { - file: 'files/shims.js', - format: 'esm' - }, - plugins: [nodeResolve(), commonjs()], - external: builtinModules - } -]; diff --git a/packages/adapter-node/src/env.js b/packages/adapter-node/src/env.js index 841bddf2c237..330032cde945 100644 --- a/packages/adapter-node/src/env.js +++ b/packages/adapter-node/src/env.js @@ -1,4 +1,6 @@ -/* global ENV_PREFIX */ +/* global __ENV_PREFIX */ + +export const ENV_PREFIX = import.meta.ENV_PREFIX; const expected = new Set([ 'SOCKET_PATH', diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 8a1390a78f25..fefdc0f9882e 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -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 }); @@ -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 diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index ea3feee05860..5832b244a51e 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -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); diff --git a/packages/adapter-node/src/shims_empty.js b/packages/adapter-node/src/shims_empty.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5eaa50334283..8dd4c1402625 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -198,6 +198,9 @@ importers: typescript: specifier: ^4.9.4 version: 4.9.4 + undici: + specifier: ~5.22.0 + version: 5.22.0 uvu: specifier: ^0.5.6 version: 0.5.6 @@ -2438,7 +2441,6 @@ packages: engines: {node: '>=10.16.0'} dependencies: streamsearch: 1.1.0 - dev: false /c8@7.12.0: resolution: {integrity: sha512-CtgQrHOkyxr5koX1wEUmN/5cfDa2ckbHRA4Gy5LAL0zaCFtVWJS5++n+w4/sr2GWGerBxgTjpKeDclk/Qk6W/A==} @@ -5194,7 +5196,6 @@ packages: /streamsearch@1.1.0: resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==} engines: {node: '>=10.0.0'} - dev: false /string-width@1.0.2: resolution: {integrity: sha512-0XsVpQLnVCXHJfyEs8tC0zpTVIr5PKKsQtkT29IwupnPTjtPmQ3xT/4yCREF9hYkV/3M3kzcUTSAZT6a6h81tw==} @@ -5642,7 +5643,6 @@ packages: engines: {node: '>=14.0'} dependencies: busboy: 1.6.0 - dev: false /universalify@0.1.2: resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==}