@vuebro/loader-sfc 1.6.0 → 2.0.1

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,69 @@
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
25
  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;
26
+ expressionPlugins: ["jsx", "typescript"],
27
+ }, id = hash(filename), module = {}, { descriptor, errors } = parse((await (await fetchText(filename)).text()) || "<template></template>"), { script, scriptSetup, slotted, styles, template } = descriptor;
53
28
  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];
29
+ let el = document.getElementById(id);
30
+ if (!(el instanceof HTMLStyleElement)) {
31
+ el = document.createElement("style");
32
+ el.id = id;
33
+ document.head.appendChild(el);
34
+ }
35
+ el.textContent = (await Promise.all(styles.map(async ({ content, module, scoped = false, src }) => {
36
+ const { code, errors } = await compileStyleAsync({
37
+ filename,
38
+ id,
39
+ modules: !!module,
40
+ scoped,
41
+ source: src ? await (await fetchText(src)).text() : content,
61
42
  });
43
+ log(errors);
44
+ return code;
45
+ }))).join("\n");
46
+ if (script || scriptSetup) {
62
47
  const { bindings, content, warnings = [], } = compileScript(descriptor, { id });
63
48
  log(warnings);
64
49
  if (bindings)
65
50
  compilerOptions.bindingMetadata = bindings;
66
- Object.assign(module, (await inject(transforms.length
67
- ? transform(content, { jsxRuntime, transforms }).code
68
- : content)).default);
51
+ Object.assign(module, (await inject(transform(content, options).code)).default);
69
52
  }
70
- if (descriptor.template) {
53
+ if (template) {
54
+ const { content: source } = template;
71
55
  const { code, errors, tips } = compileTemplate({
72
- ast: descriptor.template.ast,
73
56
  compilerOptions,
74
- filename: descriptor.filename,
57
+ filename,
75
58
  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,
59
+ scoped: styles.some(({ scoped }) => scoped),
60
+ slotted,
61
+ source,
84
62
  });
85
63
  log(errors);
86
64
  log(tips);
87
- Object.assign(module, await inject(transforms.length
88
- ? transform(code, { jsxRuntime, transforms }).code
89
- : code));
65
+ Object.assign(module, await inject(transform(code, options).code));
90
66
  }
91
- await Promise.all(descriptor.styles.map((style) => addStyle(id, descriptor, style)));
92
67
  return module;
93
68
  };
94
69
  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.1",
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,11 +44,10 @@
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
52
  "sucrase": "^3.35.0"
54
53
  }