Fix Svelte build output (#201)

* fix(#200): svelte build output

* chore: add changeset
This commit is contained in:
Nate Moore 2021-05-13 09:29:23 -05:00 committed by Nate Moore
parent 3ef1b01e14
commit 000464bf35
4 changed files with 14 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix bug when building Svelte components

View file

@ -2,6 +2,6 @@
const { __astro_component: Component, __astro_children, ...props } = $$props; const { __astro_component: Component, __astro_children, ...props } = $$props;
</script> </script>
<Component {...props}> <svelte:component this={Component} {...props}>
{@html __astro_children} {@html __astro_children}
</Component> </svelte:component>

View file

@ -14,7 +14,6 @@ const defaultConfig = {
target: 'node14', target: 'node14',
sourcemap: 'inline', sourcemap: 'inline',
sourcesContent: false, sourcesContent: false,
plugins: [svelte()],
}; };
export default async function build(...args) { export default async function build(...args) {
@ -38,6 +37,7 @@ export default async function build(...args) {
outdir, outdir,
external, external,
format, format,
plugins: [svelte({ isDev })]
}); });
return; return;
} }
@ -61,6 +61,7 @@ export default async function build(...args) {
outdir, outdir,
external, external,
format, format,
plugins: [svelte({ isDev })]
}); });
process.on('beforeExit', () => { process.on('beforeExit', () => {

View file

@ -15,13 +15,13 @@ const convertMessage = ({ message, start, end, filename, frame }) => ({
}, },
}); });
const handleLoad = async (args, generate) => { const handleLoad = async (args, generate, { isDev }) => {
const { path } = args; const { path } = args;
const source = await fs.readFile(path, 'utf8'); const source = await fs.readFile(path, 'utf8');
const filename = relative(process.cwd(), path); const filename = relative(process.cwd(), path);
try { try {
let compileOptions = { css: false, generate, hydratable: true }; let compileOptions = { dev: isDev, css: false, generate, hydratable: true };
let { js, warnings } = compile(source, { ...compileOptions, filename }); let { js, warnings } = compile(source, { ...compileOptions, filename });
let contents = js.code + `\n//# sourceMappingURL=` + js.map.toUrl(); let contents = js.code + `\n//# sourceMappingURL=` + js.map.toUrl();
@ -32,7 +32,7 @@ const handleLoad = async (args, generate) => {
} }
}; };
export default function sveltePlugin() { export default function sveltePlugin({ isDev = false }) {
return { return {
name: 'svelte-esbuild', name: 'svelte-esbuild',
setup(build) { setup(build) {
@ -54,8 +54,8 @@ export default function sveltePlugin() {
}; };
} }
}); });
build.onLoad({ filter: /.*/, namespace: 'svelte:client' }, (args) => handleLoad(args, 'dom')); build.onLoad({ filter: /.*/, namespace: 'svelte:client' }, (args) => handleLoad(args, 'dom', { isDev }));
build.onLoad({ filter: /.*/, namespace: 'svelte:server' }, (args) => handleLoad(args, 'ssr')); build.onLoad({ filter: /.*/, namespace: 'svelte:server' }, (args) => handleLoad(args, 'ssr', { isDev }));
}, },
}; };
} }