@vuebro/loader-sfc 1.6.0 → 2.0.0

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.
@@ -1,94 +1,74 @@
1
1
  import { compileScript, compileStyleAsync, compileTemplate, parse, } from "@vue/compiler-sfc";
2
- import { useStyleTag } from "@vueuse/core";
3
2
  import hash from "hash-sum";
4
3
  import { transform } from "sucrase";
5
- const body = "<template></template>", fetchText = async (url) => {
4
+ const log = (msgs) => {
5
+ msgs.forEach((msg) => {
6
+ console.log(msg);
7
+ });
8
+ }, options = {
9
+ jsxRuntime: "preserve",
10
+ transforms: ["jsx", "typescript"],
11
+ };
12
+ const fetchText = async (url) => {
6
13
  try {
7
14
  const response = await fetch(url);
8
- return response.ok ? response : new Response(body);
15
+ return response.ok ? response : new Response("");
9
16
  }
10
17
  catch {
11
- return new Response(body);
18
+ return new Response("");
12
19
  }
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
20
  }, inject = async (code) => {
29
- const objectURL = URL.createObjectURL(new Blob([code], { type: "application/javascript" })),
30
- /* @vite-ignore */
31
- value = (await import(objectURL));
21
+ const objectURL = URL.createObjectURL(new Blob([code], { type: "application/javascript" })), value = (await import(objectURL));
32
22
  URL.revokeObjectURL(objectURL);
33
23
  return value;
34
24
  }, loadModule = async (filename) => {
35
- const id = `data-v-${hash(filename)}`, { descriptor, errors } = parse((await (await fetchText(filename)).text()) || body, { filename });
36
- const compilerOptions = {
37
- cacheHandlers: true,
38
- comments: false,
39
- expressionPlugins: [],
40
- hoistStatic: true,
41
- optimizeImports: true,
42
- scopeId: id,
43
- sourceMap: false,
44
- ssr: false,
45
- }, scriptBlocks = ["script", "scriptSetup"], contents = await Promise.all(scriptBlocks.map(async (key) => {
46
- const { lang = "js", src } = (descriptor[key] ?? {});
47
- if (/[jt]sx$/.test(lang))
48
- compilerOptions.expressionPlugins?.push("jsx");
49
- if (/tsx?$/.test(lang))
50
- compilerOptions.expressionPlugins?.push("typescript");
51
- return src && (await (await fetchText(src)).text());
52
- })), jsxRuntime = "preserve", module = {}, scoped = descriptor.styles.some(({ scoped }) => scoped), { expressionPlugins: transforms } = compilerOptions;
25
+ const id = hash(filename), { descriptor, errors } = parse((await (await fetchText(filename)).text()) || "<template></template>"), { script, scriptSetup, slotted, styles, template } = descriptor;
26
+ const templateOptions = {
27
+ compilerOptions: {
28
+ expressionPlugins: ["jsx", "typescript"],
29
+ },
30
+ scoped: styles.some(({ scoped }) => scoped),
31
+ slotted,
32
+ };
53
33
  log(errors);
54
- if (scoped)
55
- module.__scopeId = id;
56
- if (descriptor.script || descriptor.scriptSetup) {
57
- scriptBlocks.forEach((key, i) => {
58
- const scriptBlock = descriptor[key];
59
- if (scriptBlock && contents[i] !== undefined)
60
- scriptBlock.content = contents[i];
34
+ let el = document.getElementById(id);
35
+ if (!(el instanceof HTMLStyleElement)) {
36
+ el = document.createElement("style");
37
+ el.id = id;
38
+ document.head.appendChild(el);
39
+ }
40
+ el.textContent = (await Promise.all(styles.map(async ({ content, module, scoped = false, src }) => {
41
+ const { code, errors } = await compileStyleAsync({
42
+ filename,
43
+ id,
44
+ modules: !!module,
45
+ scoped,
46
+ source: src ? await (await fetchText(src)).text() : content,
47
+ });
48
+ log(errors);
49
+ return code;
50
+ }))).join("\n");
51
+ if (script || scriptSetup) {
52
+ const { content, warnings = [] } = compileScript(descriptor, {
53
+ id,
54
+ ...(template ? templateOptions : {}),
61
55
  });
62
- const { bindings, content, warnings = [], } = compileScript(descriptor, { id });
63
56
  log(warnings);
64
- if (bindings)
65
- compilerOptions.bindingMetadata = bindings;
66
- Object.assign(module, (await inject(transforms.length
67
- ? transform(content, { jsxRuntime, transforms }).code
68
- : content)).default);
57
+ return inject(transform(content, options).code);
69
58
  }
70
- if (descriptor.template) {
59
+ else if (template) {
60
+ const { content: source } = template;
71
61
  const { code, errors, tips } = compileTemplate({
72
- ast: descriptor.template.ast,
73
- compilerOptions,
74
- filename: descriptor.filename,
62
+ ...templateOptions,
63
+ filename,
75
64
  id,
76
- scoped,
77
- slotted: descriptor.slotted,
78
- source: descriptor.template.src
79
- ? await (await fetchText(descriptor.template.src)).text()
80
- : descriptor.template.content,
81
- // @ts-expect-error TODO remove expect-error after 3.6
82
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
83
- vapor: descriptor.vapor,
65
+ source,
84
66
  });
85
67
  log(errors);
86
68
  log(tips);
87
- Object.assign(module, await inject(transforms.length
88
- ? transform(code, { jsxRuntime, transforms }).code
89
- : code));
69
+ return inject(transform(code, options).code);
90
70
  }
91
- await Promise.all(descriptor.styles.map((style) => addStyle(id, descriptor, style)));
92
- return module;
71
+ else
72
+ return {};
93
73
  };
94
74
  export default loadModule;
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "url": "git+https://github.com/vuebro/loader-sfc.git"
22
22
  },
23
23
  "license": "AGPL-3.0-only",
24
- "version": "1.6.0",
24
+ "version": "2.0.0",
25
25
  "type": "module",
26
26
  "main": "./dist/loader-sfc.js",
27
27
  "types": "./dist/loader-sfc.d.ts",
@@ -34,7 +34,7 @@
34
34
  "@eslint/js": "^9.36.0",
35
35
  "@rollup/plugin-terser": "^0.4.4",
36
36
  "@types/hash-sum": "^1.0.2",
37
- "@types/node": "^24.6.1",
37
+ "@types/node": "^24.6.2",
38
38
  "eslint": "^9.36.0",
39
39
  "eslint-config-prettier": "^10.1.8",
40
40
  "eslint-import-resolver-typescript": "^4.4.4",
@@ -44,12 +44,12 @@
44
44
  "jiti": "^2.6.1",
45
45
  "typescript": "^5.9.3",
46
46
  "typescript-eslint": "^8.45.0",
47
- "vite": "^7.1.7"
47
+ "vite": "^7.1.8"
48
48
  },
49
49
  "dependencies": {
50
50
  "@vue/compiler-sfc": "^3.5.22",
51
- "@vueuse/core": "^13.9.0",
52
51
  "hash-sum": "^2.0.0",
53
- "sucrase": "^3.35.0"
52
+ "sucrase": "^3.35.0",
53
+ "vue": "^3.5.22"
54
54
  }
55
55
  }