0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/scripts/utils/svelte-plugin.js
Erika f14e48098a
nit: use node: prefix everywhere (#7692)
* nit: use `node:` prefix everywhere

* nit: fs/promises too

* test: workaround issue in node builtin detection
2023-07-18 02:17:59 +02:00

70 lines
1.8 KiB
JavaScript

// @ts-nocheck
import { promises as fs } from 'node:fs';
import { dirname, isAbsolute, join, relative } from 'node:path';
import { compile } from 'svelte/compiler';
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, { isDev }) => {
const { path } = args;
const source = await fs.readFile(path, 'utf8');
const filename = relative(process.cwd(), path);
try {
let compileOptions = { dev: isDev, 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({ isDev = false }) {
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', { isDev })
);
build.onLoad({ filter: /.*/, namespace: 'svelte:server' }, (args) =>
handleLoad(args, 'ssr', { isDev })
);
},
};
}