From 45f75531be95755ea39c917651f0f82a5b58a089 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 24 Oct 2021 21:31:05 -0700 Subject: [PATCH] cleaner way of handling entry point --- packages/adapter-netlify/index.js | 1 + packages/adapter-netlify/package.json | 4 +++- packages/adapter-node/index.js | 5 +---- packages/adapter-node/package.json | 13 ++++++++++++- packages/kit/src/core/build/index.js | 13 ++----------- packages/kit/types/config.d.ts | 1 + 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index cd630b777732..4da1007d9847 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -7,6 +7,7 @@ import toml from '@iarna/toml'; export default function () { return { name: '@sveltejs/adapter-netlify', + serverEntryPoint: '@sveltejs/adapter-netlify/entry', async adapt({ utils }) { // "build" is the default publish directory when Netlify detects SvelteKit diff --git a/packages/adapter-netlify/package.json b/packages/adapter-netlify/package.json index b2ee1c0d4be0..d6278e2316ab 100644 --- a/packages/adapter-netlify/package.json +++ b/packages/adapter-netlify/package.json @@ -12,10 +12,12 @@ ".": { "import": "./index.js" }, + "./entry": { + "import": "./files/entry.js" + }, "./package.json": "./package.json" }, "main": "index.js", - "adapter": "files/entry.js", "types": "index.d.ts", "files": [ "files", diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 5c109e63decc..60385341b942 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -8,10 +8,6 @@ import zlib from 'zlib'; const pipe = promisify(pipeline); -/** - * @typedef {import('esbuild').BuildOptions} BuildOptions - */ - /** @type {import('.')} */ export default function ({ // TODO: add ability to customize entry point to build/index.js @@ -22,6 +18,7 @@ export default function ({ } = {}) { return { name: '@sveltejs/adapter-node', + serverEntryPoint: '@sveltejs/adapter-node/entry', async adapt({ utils }) { utils.rimraf(out); diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index 976fc2963ab4..d621b4fc7c8e 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -10,7 +10,18 @@ "type": "module", "main": "index.js", "types": "index.d.ts", - "adapter": "files/index.js", + "exports": { + ".": { + "import": "./index.js" + }, + "./entry": { + "import": "./files/index.js" + }, + "./middlewares": { + "import": "./files/middlewares.js" + }, + "./package.json": "./package.json" + }, "scripts": { "dev": "rollup -cw", "build": "rollup -c", diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 1d94a1d35383..f914d8f5fcc5 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -1,6 +1,5 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; import fs from 'fs'; -import { createRequire } from 'module'; import path from 'path'; import glob from 'tiny-glob/sync.js'; import vite from 'vite'; @@ -463,7 +462,7 @@ async function build_server( // don't warn on overriding defaults const [modified_vite_config] = deep_merge(default_config, vite_config); - const adapter = config.kit?.adapter?.name; + const adapter = config.kit?.adapter?.serverEntryPoint; /** * @type {{ app:string, index?:string }} */ @@ -471,15 +470,7 @@ async function build_server( app: app_file }; if (adapter) { - const require = createRequire(import.meta.url); - let pkg_path; - try { - pkg_path = require.resolve(`${adapter}/package.json`); - } catch (err) { - throw new Error(`Could not resolve ${adapter}/package.json while building ${cwd}`); - } - const pkg = JSON.parse(fs.readFileSync(pkg_path, 'utf8')); - input.index = path.resolve(pkg_path.substring(0, pkg_path.lastIndexOf('/')), pkg.adapter); + input.index = adapter; } /** @type {[any, string[]]} */ diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 3e509fa0981d..9919392d4788 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -15,6 +15,7 @@ export interface AdapterUtils { export interface Adapter { name: string; + serverEntryPoint?: string; adapt(context: { utils: AdapterUtils; config: ValidatedConfig }): Promise; }