@vuebro/loader-sfc 1.5.1 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/loader-sfc.d.ts +2 -4
- package/dist/loader-sfc.esm-browser.prod.js +18070 -41042
- package/dist/loader-sfc.js +83 -0
- package/package.json +4 -3
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { compileScript, compileStyleAsync, compileTemplate, parse, } from "@vue/compiler-sfc";
|
|
2
|
+
import { useStyleTag } from "@vueuse/core";
|
|
3
|
+
import hash from "hash-sum";
|
|
4
|
+
import { transform } from "sucrase";
|
|
5
|
+
const body = "<template></template>", fetchText = async (url) => {
|
|
6
|
+
try {
|
|
7
|
+
const response = await fetch(url);
|
|
8
|
+
return response.ok ? response : new Response(body);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return new Response(body);
|
|
12
|
+
}
|
|
13
|
+
}, log = (msgs) => {
|
|
14
|
+
msgs.forEach((msg) => {
|
|
15
|
+
console.log(msg);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
const addStyle = async (id, { filename }, { content, module, scoped = false, src }) => {
|
|
19
|
+
const { code, errors } = await compileStyleAsync({
|
|
20
|
+
filename,
|
|
21
|
+
id,
|
|
22
|
+
modules: !!module,
|
|
23
|
+
scoped,
|
|
24
|
+
source: src ? await (await fetchText(src)).text() : content,
|
|
25
|
+
});
|
|
26
|
+
log(errors);
|
|
27
|
+
useStyleTag(code, scoped ? { id } : undefined);
|
|
28
|
+
}, inject = (code) => import(
|
|
29
|
+
/* @vite-ignore */
|
|
30
|
+
`data:text/javascript;base64,${btoa(Array.from(new TextEncoder().encode(code), (byte) => String.fromCodePoint(byte)).join(""))}`), loadModule = async (filename) => {
|
|
31
|
+
const { descriptor, errors } = parse((await (await fetchText(filename)).text()) || body, { filename });
|
|
32
|
+
const compilerOptions = { expressionPlugins: [] }, scriptBlocks = ["script", "scriptSetup"], contents = await Promise.all(scriptBlocks.map(async (key) => {
|
|
33
|
+
const { lang = "js", src } = (descriptor[key] ?? {});
|
|
34
|
+
if (/[jt]sx$/.test(lang))
|
|
35
|
+
compilerOptions.expressionPlugins?.push("jsx");
|
|
36
|
+
if (/tsx?$/.test(lang))
|
|
37
|
+
compilerOptions.expressionPlugins?.push("typescript");
|
|
38
|
+
return src && (await (await fetchText(src)).text());
|
|
39
|
+
})), id = `data-v-${hash(filename)}`, jsxRuntime = "preserve", module = {}, scoped = descriptor.styles.some(({ scoped }) => scoped), { expressionPlugins: transforms } = compilerOptions;
|
|
40
|
+
log(errors);
|
|
41
|
+
if (scoped)
|
|
42
|
+
module.__scopeId = id;
|
|
43
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
44
|
+
scriptBlocks.forEach((key, i) => {
|
|
45
|
+
const scriptBlock = descriptor[key];
|
|
46
|
+
if (scriptBlock && contents[i] !== undefined)
|
|
47
|
+
scriptBlock.content = contents[i];
|
|
48
|
+
});
|
|
49
|
+
const { bindings, content, warnings = [], } = compileScript(descriptor, { id });
|
|
50
|
+
log(warnings);
|
|
51
|
+
if (bindings)
|
|
52
|
+
compilerOptions.bindingMetadata = bindings;
|
|
53
|
+
Object.assign(module, (await inject(transforms.length
|
|
54
|
+
? transform(content, { jsxRuntime, transforms }).code
|
|
55
|
+
: content)).default);
|
|
56
|
+
}
|
|
57
|
+
if (descriptor.template) {
|
|
58
|
+
const { code, errors, tips } = compileTemplate({
|
|
59
|
+
ast: descriptor.template.ast,
|
|
60
|
+
compilerOptions,
|
|
61
|
+
filename: descriptor.filename,
|
|
62
|
+
id,
|
|
63
|
+
scoped,
|
|
64
|
+
slotted: descriptor.slotted,
|
|
65
|
+
source: descriptor.template.src
|
|
66
|
+
? await (await fetchText(descriptor.template.src)).text()
|
|
67
|
+
: descriptor.template.content,
|
|
68
|
+
// @ts-expect-error TODO remove expect-error after 3.6
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
70
|
+
vapor: descriptor.vapor,
|
|
71
|
+
});
|
|
72
|
+
log(errors);
|
|
73
|
+
log(tips);
|
|
74
|
+
Object.assign(module, await inject(transforms.length
|
|
75
|
+
? transform(code, { jsxRuntime, transforms }).code
|
|
76
|
+
: code));
|
|
77
|
+
}
|
|
78
|
+
descriptor.styles.forEach((style) => {
|
|
79
|
+
void addStyle(id, descriptor, style);
|
|
80
|
+
});
|
|
81
|
+
return module;
|
|
82
|
+
};
|
|
83
|
+
export default loadModule;
|
package/package.json
CHANGED
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"url": "git+https://github.com/vuebro/loader-sfc.git"
|
|
22
22
|
},
|
|
23
23
|
"license": "AGPL-3.0-only",
|
|
24
|
-
"version": "1.5.
|
|
24
|
+
"version": "1.5.2",
|
|
25
25
|
"type": "module",
|
|
26
|
-
"main": "./dist/loader-sfc.
|
|
26
|
+
"main": "./dist/loader-sfc.js",
|
|
27
27
|
"types": "./dist/loader-sfc.d.ts",
|
|
28
28
|
"scripts": {
|
|
29
29
|
"lint": "eslint .",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@eslint/js": "^9.33.0",
|
|
35
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
35
36
|
"@types/hash-sum": "^1.0.2",
|
|
36
37
|
"@types/node": "^24.3.0",
|
|
37
38
|
"eslint-config-prettier": "^10.1.8",
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
"vite": "^7.1.3"
|
|
46
47
|
},
|
|
47
48
|
"dependencies": {
|
|
48
|
-
"@vue/compiler-sfc": "^3.5.
|
|
49
|
+
"@vue/compiler-sfc": "^3.5.19",
|
|
49
50
|
"@vueuse/core": "^13.7.0",
|
|
50
51
|
"hash-sum": "^2.0.0",
|
|
51
52
|
"sucrase": "^3.35.0"
|