@vuebro/loader-sfc 1.5.22 → 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,83 +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
- });
20
+ }, inject = async (code) => {
21
+ const objectURL = URL.createObjectURL(new Blob([code], { type: "application/javascript" })), value = (await import(objectURL));
22
+ URL.revokeObjectURL(objectURL);
23
+ return value;
24
+ }, loadModule = async (filename) => {
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
+ };
26
33
  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];
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 : {}),
48
55
  });
49
- const { bindings, content, warnings = [], } = compileScript(descriptor, { id });
50
56
  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);
57
+ return inject(transform(content, options).code);
56
58
  }
57
- if (descriptor.template) {
59
+ else if (template) {
60
+ const { content: source } = template;
58
61
  const { code, errors, tips } = compileTemplate({
59
- ast: descriptor.template.ast,
60
- compilerOptions,
61
- filename: descriptor.filename,
62
+ ...templateOptions,
63
+ filename,
62
64
  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,
65
+ source,
71
66
  });
72
67
  log(errors);
73
68
  log(tips);
74
- Object.assign(module, await inject(transforms.length
75
- ? transform(code, { jsxRuntime, transforms }).code
76
- : code));
69
+ return inject(transform(code, options).code);
77
70
  }
78
- descriptor.styles.forEach((style) => {
79
- void addStyle(id, descriptor, style);
80
- });
81
- return module;
71
+ else
72
+ return {};
82
73
  };
83
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.5.22",
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,22 +34,22 @@
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.0",
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",
41
41
  "eslint-plugin-import-x": "^4.16.1",
42
42
  "eslint-plugin-perfectionist": "^4.15.0",
43
43
  "eslint-plugin-prettier": "^5.5.4",
44
- "jiti": "^2.6.0",
45
- "typescript": "^5.9.2",
44
+ "jiti": "^2.6.1",
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
  }