@vuebro/loader-sfc 2.0.4 → 2.1.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.
@@ -11,29 +11,33 @@ const fetchText = async (url) => {
11
11
  return new Response("");
12
12
  }
13
13
  }, inject = async (code) => {
14
- const objectURL = URL.createObjectURL(new Blob([code], { type: "application/javascript" })), value = (await import(objectURL));
15
- URL.revokeObjectURL(objectURL);
16
- return value;
17
- }, log = (msgs) => {
18
- msgs.forEach((msg) => {
19
- console.log(msg);
20
- });
21
- }, options = {
22
- jsxRuntime: "preserve",
23
- transforms: ["jsx", "typescript"],
14
+ const objectURL = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
15
+ try {
16
+ return (await import(objectURL));
17
+ }
18
+ finally {
19
+ URL.revokeObjectURL(objectURL);
20
+ }
24
21
  };
25
22
  /* -------------------------------------------------------------------------- */
26
23
  export default async (filename) => {
27
- const id = `data-v-${hash(filename)}`, { descriptor, errors } = parse((await (await fetchText(filename)).text()) || "<template></template>"), { script, scriptSetup, slotted, styles, template } = descriptor;
28
- log(errors);
24
+ const id = `data-v-${hash(filename)}`, langs = new Set(), { descriptor, errors: alerts } = parse((await (await fetchText(filename)).text()) || "<template></template>"), { script, scriptSetup, slotted, styles, template } = descriptor;
25
+ [script, scriptSetup].forEach((scriptBlock) => {
26
+ const { lang = "js" } = scriptBlock ?? {};
27
+ if (/[jt]sx$/.test(lang))
28
+ langs.add("jsx");
29
+ if (/tsx?$/.test(lang))
30
+ langs.add("typescript");
31
+ });
29
32
  const compilerOptions = {
30
- expressionPlugins: ["jsx", "typescript"],
33
+ expressionPlugins: [...langs],
31
34
  scopeId: id,
32
35
  slotted,
33
- }, scoped = styles.some(({ scoped }) => scoped), component = scoped ? { __scopeId: id } : {};
34
- if (!(document.getElementById(id) instanceof HTMLStyleElement)) {
35
- const warnings = new Set();
36
- const textContent = (await Promise.all(styles.map(async ({ content, module, scoped = false, src }) => {
36
+ }, scoped = styles.some(({ scoped }) => scoped), component = scoped ? { __scopeId: id } : {}, options = {
37
+ jsxRuntime: "preserve",
38
+ transforms: [...langs],
39
+ }, style = !(document.getElementById(id) instanceof HTMLStyleElement)
40
+ ? Promise.all(styles.map(async ({ content, module = false, scoped = false, src }) => {
37
41
  const { code, errors } = await compileStyleAsync({
38
42
  filename,
39
43
  id,
@@ -41,40 +45,43 @@ export default async (filename) => {
41
45
  source: src ? await (await fetchText(src)).text() : content,
42
46
  });
43
47
  if (module)
44
- warnings.add("<style module> is not supported in the playground.");
45
- log(errors);
48
+ errors.push(Error("<style module> is not supported in the playground."));
49
+ alerts.push(...errors);
46
50
  return code;
47
- })))
48
- .join("\n")
49
- .trim();
50
- if (textContent) {
51
- const el = document.createElement("style");
52
- el.id = id;
53
- el.textContent = textContent;
54
- document.head.appendChild(el);
55
- }
56
- log([...warnings]);
57
- }
58
- if (script || scriptSetup) {
59
- const { bindings, content, warnings = [], } = compileScript(descriptor, { id });
60
- if (template && bindings)
61
- compilerOptions.bindingMetadata = bindings;
62
- Object.assign(component, (await inject(transform(content, options).code)).default);
63
- log(warnings);
64
- }
65
- if (template) {
66
- const { ast, content: source } = template, { code, errors, tips } = compileTemplate({
67
- ...(ast && { ast }),
51
+ }))
52
+ : Promise.resolve([]), { ast, content: source } = template ?? {}, { bindings, content, warnings } = script || scriptSetup ? compileScript(descriptor, { id }) : {};
53
+ if (source && bindings)
54
+ compilerOptions.bindingMetadata = bindings;
55
+ const { code, errors, tips } = source !== undefined
56
+ ? compileTemplate({
57
+ ...(ast ?? {}),
68
58
  compilerOptions,
69
59
  filename,
70
60
  id,
71
61
  scoped,
72
62
  slotted,
73
63
  source,
74
- });
75
- Object.assign(component, await inject(transform(code, options).code));
76
- log(tips);
77
- log(errors);
64
+ })
65
+ : {};
66
+ const [styleResult, scriptResult, templateResult] = await Promise.all([
67
+ style,
68
+ content
69
+ ? inject(langs.size ? transform(content, options).code : content)
70
+ : Promise.resolve(undefined),
71
+ code
72
+ ? inject(langs.size ? transform(code, options).code : code)
73
+ : Promise.resolve(undefined),
74
+ ]), textContent = styleResult.join("\n").trim();
75
+ if (textContent) {
76
+ const el = document.createElement("style");
77
+ el.id = id;
78
+ el.textContent = textContent;
79
+ document.head.appendChild(el);
78
80
  }
81
+ Object.assign(component, scriptResult?.default);
82
+ Object.assign(component, templateResult);
83
+ [...alerts, ...(warnings ?? []), ...(tips ?? []), ...(errors ?? [])].forEach((msg) => {
84
+ console.log(msg);
85
+ });
79
86
  return component;
80
87
  };
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": "2.0.4",
24
+ "version": "2.1.0",
25
25
  "type": "module",
26
26
  "main": "./dist/loader-sfc.js",
27
27
  "types": "./dist/loader-sfc.d.ts",