Skip to content

Commit

Permalink
Shebang and relative imports (HoudiniGraphql#630)
Browse files Browse the repository at this point in the history
* add shebang to cmd

* simplify resolveId logic to do the equivalent of resolve_relative

* only perform resolve_relative on non-entries - leave old logic behind for entries

* changeset

* add comment

* more comments
  • Loading branch information
AlecAivazis authored and endigma committed Nov 10, 2024
1 parent 0813bab commit c0cb7cc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-walls-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

Fix relative imports from root layout
5 changes: 5 additions & 0 deletions .changeset/young-bears-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini': patch
---

Add shebang to executable
6 changes: 5 additions & 1 deletion e2e/sveltekit/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import cache from '$houdini/runtime/cache';
import { routes } from '$lib/utils/routes.js';
// leave this in to make sure we don't break relative imports from
// the root layout. see: https://github.com/HoudiniGraphql/houdini/issues/629
import Test from './Test.svelte';
if (browser) {
// @ts-ignore
window.cache = cache;
Expand All @@ -22,7 +26,7 @@

<slot />

<hr />
<Test />

<nav>
{#each routesKvp as { key, value }}
Expand Down
1 change: 1 addition & 0 deletions e2e/sveltekit/src/routes/Test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<hr />
10 changes: 5 additions & 5 deletions packages/_scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default async function ({ plugin }) {
// cmd needs to be bundled and set as the project's bin
else if (dirname === 'cmd') {
package_json.bin = './build/cmd-esm/index.js'
await build({ package_json, source: dir, plugin })
await build({ package_json, source: dir, plugin, cmd: true })
}

// its not a special directory, treat it as a sub module
Expand All @@ -82,7 +82,7 @@ export default async function ({ plugin }) {
}

// create esm and cjs builds of the source
async function build({ package_json, source, bundle = true, plugin }) {
async function build({ package_json, source, bundle = true, plugin, cmd }) {
// if we aren't bundling, look up the entrypoints once
const children = bundle
? []
Expand All @@ -96,12 +96,12 @@ async function build({ package_json, source, bundle = true, plugin }) {
// where we will put everything
const target_dir = path.join(build_dir, `${path.basename(source)}-${which}`)

let header = ''
let header = cmd ? '#!/usr/bin/env node\n' : ''
if (bundle) {
if (plugin) {
header = `const require = conflict_free(import.meta.url);`
header += `const require = conflict_free(import.meta.url);`
} else {
header = `import { createRequire as conflict_free } from 'module'; const require = conflict_free(import.meta.url);`
header += `import { createRequire as conflict_free } from 'module'; const require = conflict_free(import.meta.url);`
}
}

Expand Down
20 changes: 13 additions & 7 deletions packages/houdini-svelte/src/plugin/fsPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ import {
// this plugin is responsible for faking `+page.js` existence in the eyes of sveltekit
export default (getFramwork: () => Framework) =>
({
resolveId(filepath, _, { config }) {
async resolveId(filepath, _, { config, isEntry }) {
// without this check, the block underneath breaks relative imports from root layout
if (!isEntry) {
// make sure there is no
const match = filepath.match('^((../)+)src/routes')
if (match) {
return path.join(config.projectRoot, filepath.substring(match[1].length))
}
// if there is no deep relative import, do the default thing
return
}

// everything internal to houdini should assume posix paths
filepath = path.posixify(filepath.toString())

Expand All @@ -23,11 +34,9 @@ export default (getFramwork: () => Framework) =>
is_root_layout_server(config, filepath)
) {
return {
id: filepath,
id: resolve_relative(config, filepath),
}
}

return null
},

load: async (filepath, { config }) => {
Expand Down Expand Up @@ -57,9 +66,6 @@ export default (getFramwork: () => Framework) =>
empty_layout,
}
}

// do the normal thing
return null
},
} as Plugin['vite'])

Expand Down

0 comments on commit c0cb7cc

Please sign in to comment.