mirror of
https://github.com/withastro/astro.git
synced 2025-01-23 11:01:54 -05:00
c93201a909
* 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>
60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
// @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'))
|
|
},
|
|
}
|
|
}
|