Skip to content

Commit

Permalink
remove client.init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Sep 1, 2022
1 parent e8c958d commit aac42b2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/vite/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function ensure_imports({
importKind,
}: {
page: TransformPage
import: string
import?: string
as?: never
sourceModule: string
importKind?: 'value' | 'type'
Expand All @@ -26,7 +26,7 @@ export function ensure_imports({
importKind,
}: {
page: TransformPage
import: string[]
import?: string[]
as?: string[]
sourceModule: string
importKind?: 'value' | 'type'
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/vite/transforms/kit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)])
}
22 changes: 22 additions & 0 deletions src/vite/transforms/kit/init.test.ts
Original file line number Diff line number Diff line change
@@ -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',
`
<script>
export let data
</script>
`
)

expect(result).toMatchInlineSnapshot(`
import "$houdini/runtime/adapter";
import __houdini_client__ from "../../../my/client/path";
export let data;
__houdini_client__.receiveServerSession(data);
`)
})
15 changes: 15 additions & 0 deletions src/vite/transforms/kit/init.ts
Original file line number Diff line number Diff line change
@@ -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',
})
}
2 changes: 2 additions & 0 deletions src/vite/transforms/kit/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/vite/transforms/kit/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit aac42b2

Please sign in to comment.