build: add Vue and Pinia to the browser build and unit tests

This commit is contained in:
Sameh Khamis committed 2026-10-30 16:07:44 -07:00
1 parent 6d6df9d83b
commit c13c487dcf
8 files changed
+658 -20

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
import { register } from 'node:module';
// Lets unit tests import single-file components (`node --import`).
register('./vueLoader.mjs', import.meta.url);
+39
View File
@@ -0,0 +1,39 @@
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { compileScript, compileTemplate, parse } from 'vue/compiler-sfc';
/**
* Compile a single-file component to an ES module for Node's test runner.
* Styles are dropped: tests read the global stylesheet separately.
*/
export function compileVueModule(source, filename) {
const { descriptor, errors } = parse(source, { filename });
if (errors.length) throw errors[0];
const id = filename;
if (descriptor.scriptSetup) {
return compileScript(descriptor, { id, inlineTemplate: true }).content;
}
const script = descriptor.script
? compileScript(descriptor, { id }).content.replace(
'export default',
'const __sfc__ =',
)
: 'const __sfc__ = {};';
const template = descriptor.template
? compileTemplate({ source: descriptor.template.content, filename, id })
.code
: '';
return `${script}\n${template}\n__sfc__.render = typeof render === 'function' ? render : __sfc__.render;\nexport default __sfc__;\n`;
}
/** Node module hook: load `.vue` files as compiled modules. */
export async function load(url, context, nextLoad) {
if (!url.endsWith('.vue')) return nextLoad(url, context);
const filename = fileURLToPath(url);
const source = await readFile(filename, 'utf8');
return {
format: 'module',
source: compileVueModule(source, filename),
shortCircuit: true,
};
}