Skip to content

Commit

Permalink
Sourcemaps (#895)
Browse files Browse the repository at this point in the history
* first pass threading sourcemaps

* ensure +layout.svelte exists

* stablize hash

* better behavior for fspatch

* update snapshots

* changelog
  • Loading branch information
AlecAivazis authored Feb 3, 2023
1 parent e4374e1 commit d03ce3b
Show file tree
Hide file tree
Showing 15 changed files with 13,096 additions and 9,556 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-kangaroos-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

Fix sourcemaps
1 change: 1 addition & 0 deletions packages/houdini-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"graphql": "^15.8.0",
"houdini": "workspace:^",
"recast": "^0.23.1",
"rollup": "^3.7.4",
"svelte": "^3.55.1",
"vite": "^4.0.4"
},
Expand Down
26 changes: 26 additions & 0 deletions packages/houdini-svelte/src/plugin/fsPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PluginHooks } from 'houdini'
import { fs, path } from 'houdini'
import type { PathLike } from 'node:fs'
import filesystem, { Dirent } from 'node:fs'
import filesystemPromises from 'node:fs/promises'

import { _config } from '.'
import type { Framework } from './kit'
Expand Down Expand Up @@ -217,6 +218,31 @@ Object.defineProperty(globalThis, 'fs', {
value: filesystem,
})

// patch the promise util to detect a mocked layout
const _readFile = filesystemPromises.readFile

filesystemPromises.readFile = async (path, options): Promise<any> => {
// this is +layout.svelte because source map validations are by file name
// make sure there is always a +layout.svelte
if (path.toString().endsWith('+layout.svelte')) {
try {
return await _readFile(path, options)
} catch {
return typeof options === 'string' || options?.encoding
? empty_layout
: Buffer.from(empty_layout)
}
}

return _readFile(path, options)
}

Object.defineProperty(globalThis, 'fs/promises', {
configurable: true,
enumerable: true,
value: filesystemPromises,
})

function virtual_file(name: string, options: Parameters<typeof filesystem.readdirSync>[1]) {
return !options?.withFileTypes
? name
Expand Down
15 changes: 10 additions & 5 deletions packages/houdini-svelte/src/plugin/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseJS, runPipeline, formatErrors } from 'houdini'
import type { TransformPage } from 'houdini/vite'
import * as recast from 'recast'
import type { SourceMapInput } from 'rollup'

import type { ParsedFile } from '../extract'
import { parseSvelte } from '../extract'
Expand All @@ -17,7 +18,7 @@ const pipeline = [kit, query, tags]
export default async function apply_transforms(
framework: Framework,
page: TransformPage
): Promise<{ code: string }> {
): Promise<{ code: string; map?: SourceMapInput }> {
// a single transform might need to do different things to the module and
// instance scripts so we're going to pull them out, push them through separately,
// and then join them back together
Expand All @@ -30,7 +31,7 @@ export default async function apply_transforms(
script = await parseJS(page.content)
}
} catch (e) {
return { code: page.content }
return { code: page.content, map: page.map }
}

// if the route script is nill we can just use an empty program
Expand Down Expand Up @@ -63,13 +64,17 @@ export default async function apply_transforms(
}

// print the result
const printedScript = recast.print(result.script).code
const { code, map } = recast.print(result.script, {
// @ts-ignore
inputSourceMap: page.map,
})

return {
// if we're transforming a svelte file, we need to replace the script's inner contents
code: !page.filepath.endsWith('.svelte')
? printedScript
: replace_tag_content(page.content, script.start, script.end, printedScript),
? code
: replace_tag_content(page.content, script.start, script.end, code),
map,
}
}

Expand Down
Loading

0 comments on commit d03ce3b

Please sign in to comment.