@vuebro/loader-sfc 1.5.0 → 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.
@@ -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,10 @@
21
21
  "url": "git+https://github.com/vuebro/loader-sfc.git"
22
22
  },
23
23
  "license": "AGPL-3.0-only",
24
- "version": "1.5.0",
24
+ "version": "1.5.2",
25
25
  "type": "module",
26
- "main": "./dist/loader-sfc.esm-browser.prod.js",
26
+ "main": "./dist/loader-sfc.js",
27
+ "types": "./dist/loader-sfc.d.ts",
27
28
  "scripts": {
28
29
  "lint": "eslint .",
29
30
  "lint:fix": "eslint . --fix",
@@ -31,6 +32,7 @@
31
32
  },
32
33
  "devDependencies": {
33
34
  "@eslint/js": "^9.33.0",
35
+ "@rollup/plugin-terser": "^0.4.4",
34
36
  "@types/hash-sum": "^1.0.2",
35
37
  "@types/node": "^24.3.0",
36
38
  "eslint-config-prettier": "^10.1.8",
@@ -44,7 +46,7 @@
44
46
  "vite": "^7.1.3"
45
47
  },
46
48
  "dependencies": {
47
- "@vue/compiler-sfc": "^3.5.18",
49
+ "@vue/compiler-sfc": "^3.5.19",
48
50
  "@vueuse/core": "^13.7.0",
49
51
  "hash-sum": "^2.0.0",
50
52
  "sucrase": "^3.35.0"
@@ -1,4 +0,0 @@
1
- declare module "index" {
2
- const loadModule: (filename: string) => Promise<Record<string, string | object>>;
3
- export default loadModule;
4
- }