diff options
author | 2021-05-03 13:18:08 -0500 | |
---|---|---|
committer | 2021-05-03 13:18:08 -0500 | |
commit | c93201a909105bb0ef75278d9635de4b5b8734e7 (patch) | |
tree | 5a9c091850a15f5562844a75b6b3d270f6d176c6 /scripts/utils/svelte-plugin.js | |
parent | 4ff3add50f170b50f7d629a7d16e03f196e19edc (diff) | |
download | astro-c93201a909105bb0ef75278d9635de4b5b8734e7.tar.gz astro-c93201a909105bb0ef75278d9635de4b5b8734e7.tar.zst astro-c93201a909105bb0ef75278d9635de4b5b8734e7.zip |
Refactor `astro` export map, add `source-map-support` (#161)
* fix: add svelte plugin for esbuild, remove precompiled svelte components
* refactor: public export map, public types
* feat: add source-map-support to common code paths
* chore: move new "exports" to "imports" map, add internal types
* Include outPath in error logging for bad load status, and drop error stack (#163)
* Include outPath in the error logging for bad load status
* Discard error stack since it seems not useful
* feat: improve build error logging
* refactor: use object param for writeResult
Co-authored-by: Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>
Diffstat (limited to 'scripts/utils/svelte-plugin.js')
-rw-r--r-- | scripts/utils/svelte-plugin.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/scripts/utils/svelte-plugin.js b/scripts/utils/svelte-plugin.js new file mode 100644 index 000000000..84839ffd7 --- /dev/null +++ b/scripts/utils/svelte-plugin.js @@ -0,0 +1,60 @@ +// @ts-nocheck +import { compile } from 'svelte/compiler'; +import { relative, isAbsolute, join, dirname } from 'path'; +import { promises as fs } from 'fs'; + +const convertMessage = ({ message, start, end, filename, frame }) => ({ + text: message, + location: start && end && { + file: filename, + line: start.line, + column: start.column, + length: start.line === end.line ? end.column - start.column : 0, + lineText: frame, + }, +}) + +const handleLoad = async (args, generate) => { + const { path } = args; + const source = await fs.readFile(path, 'utf8'); + const filename = relative(process.cwd(), path) + + try { + let compileOptions = { css: false, generate, hydratable: true }; + + let { js, warnings } = compile(source, { ...compileOptions, filename }) + let contents = js.code + `\n//# sourceMappingURL=` + js.map.toUrl() + + return { loader: 'js', contents, resolveDir: dirname(path), warnings: warnings.map(w => convertMessage(w)) }; + } catch (e) { + return { errors: [convertMessage(e)] } + } +} + +export default function sveltePlugin() { + return { + name: 'svelte-esbuild', + setup(build) { + build.onResolve({ filter: /\.svelte$/ }, args => { + let path = args.path.replace(/\.(?:client|server)/, ''); + path = isAbsolute(path) ? path : join(args.resolveDir, path) + + if (/\.client\.svelte$/.test(args.path)) { + return { + path, + namespace: 'svelte:client', + } + } + + if (/\.server\.svelte$/.test(args.path)) { + return { + path, + namespace: 'svelte:server', + } + } + }); + build.onLoad({ filter: /.*/, namespace: 'svelte:client' }, (args) => handleLoad(args, 'dom')) + build.onLoad({ filter: /.*/, namespace: 'svelte:server' }, (args) => handleLoad(args, 'ssr')) + }, + } +} |