diff --git a/src/vite/imports.ts b/src/vite/imports.ts index c093126bda..de62d30948 100644 --- a/src/vite/imports.ts +++ b/src/vite/imports.ts @@ -14,7 +14,7 @@ export function ensure_imports({ importKind, }: { page: TransformPage - import: string + import?: string as?: never sourceModule: string importKind?: 'value' | 'type' @@ -26,7 +26,7 @@ export function ensure_imports({ importKind, }: { page: TransformPage - import: string[] + import?: string[] as?: string[] sourceModule: string importKind?: 'value' | 'type' @@ -39,11 +39,29 @@ export function ensure_imports({ as, }: { page: TransformPage - import: string[] | string + import?: string[] | string as?: string[] sourceModule: string importKind?: 'value' | 'type' }): { ids: Identifier[] | Identifier; added: number } { + // if there is no import, we can simplify the logic, just look for something with a matching source + if (!importID) { + // look for an import from the source module + const has_import = page.script.body.find( + (statement) => + statement.type === 'ImportDeclaration' && statement.source.value === sourceModule + ) + if (!has_import) { + page.script.body.unshift({ + type: 'ImportDeclaration', + source: AST.stringLiteral(sourceModule), + importKind, + }) + } + + return { ids: [], added: has_import ? 0 : 1 } + } + const idList = (Array.isArray(importID) ? importID : [importID]).map((id) => AST.identifier(id)) // figure out the list of things to import diff --git a/src/vite/transforms/kit/index.ts b/src/vite/transforms/kit/index.ts index 61a8461061..9cc72ae0c2 100644 --- a/src/vite/transforms/kit/index.ts +++ b/src/vite/transforms/kit/index.ts @@ -6,6 +6,7 @@ import * as recast from 'recast' import { Config } from '../../../common' import { TransformPage } from '../../plugin' +import init from './init' import load from './load' import session from './session' @@ -15,5 +16,5 @@ export default async function SvelteKitProcessor(config: Config, page: Transform return } - await Promise.all([load(page), session(page)]) + await Promise.all([load(page), session(page), init(page)]) } diff --git a/src/vite/transforms/kit/init.test.ts b/src/vite/transforms/kit/init.test.ts new file mode 100644 index 0000000000..771cc7d0ab --- /dev/null +++ b/src/vite/transforms/kit/init.test.ts @@ -0,0 +1,22 @@ +import { test, expect } from 'vitest' + +import { transform_svelte_test } from '../../tests' + +test('modifies root +layout.svelte to import adapter', async function () { + // run the test + const result = await transform_svelte_test( + 'src/routes/+layout.svelte', + ` + + ` + ) + + expect(result).toMatchInlineSnapshot(` + import "$houdini/runtime/adapter"; + import __houdini_client__ from "../../../my/client/path"; + export let data; + __houdini_client__.receiveServerSession(data); + `) +}) diff --git a/src/vite/transforms/kit/init.ts b/src/vite/transforms/kit/init.ts new file mode 100644 index 0000000000..2246006e0c --- /dev/null +++ b/src/vite/transforms/kit/init.ts @@ -0,0 +1,15 @@ +import { ensure_imports } from '../../imports' +import { TransformPage } from '../../plugin' + +export default async function kit_init(page: TransformPage) { + // we only care about the root layout file + if (!page.config.isRootLayout(page.filepath)) { + return + } + + // all we need to do is make sure that something imports the adapter + ensure_imports({ + page, + sourceModule: '$houdini/runtime/adapter', + }) +} diff --git a/src/vite/transforms/kit/load.test.ts b/src/vite/transforms/kit/load.test.ts index 27bbd3439a..abc7e44ded 100644 --- a/src/vite/transforms/kit/load.test.ts +++ b/src/vite/transforms/kit/load.test.ts @@ -1012,6 +1012,7 @@ test('layout loads', async function () { `) expect(route.layout).toMatchInlineSnapshot(` + import "$houdini/runtime/adapter"; import __houdini_client__ from "../../../my/client/path"; export let data; __houdini_client__.receiveServerSession(data); @@ -1034,6 +1035,7 @@ test('layout inline query', async function () { }) expect(route.layout).toMatchInlineSnapshot(` + import "$houdini/runtime/adapter"; import __houdini_client__ from "../../../my/client/path"; export let data; diff --git a/src/vite/transforms/kit/session.test.ts b/src/vite/transforms/kit/session.test.ts index 23758e1249..4dcf499117 100644 --- a/src/vite/transforms/kit/session.test.ts +++ b/src/vite/transforms/kit/session.test.ts @@ -14,6 +14,7 @@ test('modifies root +layout.svelte with data prop', async function () { ) expect(result).toMatchInlineSnapshot(` + import "$houdini/runtime/adapter"; import __houdini_client__ from "../../../my/client/path"; export let data; __houdini_client__.receiveServerSession(data); @@ -29,6 +30,7 @@ test('modifies root +layout.svelte without data prop', async function () { ) expect(result).toMatchInlineSnapshot(` + import "$houdini/runtime/adapter"; import __houdini_client__ from "../../../my/client/path"; export let data; __houdini_client__.receiveServerSession(data);