@vuebro/loader-sfc 2.1.1 → 2.2.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.
@@ -20,56 +20,65 @@ const fetchText = async (url) => {
20
20
  }
21
21
  };
22
22
  /* -------------------------------------------------------------------------- */
23
- export default async (filename) => {
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) => {
23
+ export default async (filename, options) => {
24
+ const styleErrors = [], { descriptor, errors: parseErrors } = parse((await (await fetchText(filename)).text()) || "<template></template>"), { script, scriptSetup, slotted, styles, template } = descriptor;
25
+ let moduleWarning;
26
+ const id = `data-v-${hash(filename)}`, langs = new Set([script, scriptSetup].flatMap((scriptBlock) => {
26
27
  const { lang = "js" } = scriptBlock ?? {};
27
- if (/[jt]sx$/.test(lang))
28
- langs.add("jsx");
29
- if (/tsx?$/.test(lang))
30
- langs.add("typescript");
31
- });
32
- const compilerOptions = {
28
+ return [
29
+ ...(/[jt]sx$/.test(lang) ? ["jsx"] : []),
30
+ ...(/tsx?$/.test(lang) ? ["typescript"] : []),
31
+ ];
32
+ })), compilerOptions = {
33
33
  expressionPlugins: [...langs],
34
34
  scopeId: id,
35
35
  slotted,
36
- }, scoped = styles.some(({ scoped }) => scoped), component = scoped ? { __scopeId: id } : {}, options = {
37
- jsxRuntime: "preserve",
38
- transforms: [...langs],
39
- }, style = !(document.getElementById(id) instanceof HTMLStyleElement)
36
+ ...options?.compiler,
37
+ }, scoped = styles.some(({ scoped }) => scoped), component = scoped ? { __scopeId: id } : {}, templateOptions = {
38
+ compilerOptions,
39
+ scoped,
40
+ slotted,
41
+ }, scriptOptions = {
42
+ id,
43
+ templateOptions,
44
+ ...options?.script,
45
+ }, isCompileTemplate = template && (!scriptSetup || !scriptOptions.inlineTemplate), style = !(document.getElementById(id) instanceof HTMLStyleElement)
40
46
  ? Promise.all(styles.map(async ({ content, module = false, scoped = false, src }) => {
41
47
  const { code, errors } = await compileStyleAsync({
42
48
  filename,
43
49
  id,
44
50
  scoped,
45
51
  source: src ? await (await fetchText(src)).text() : content,
52
+ ...options?.style,
46
53
  });
47
- if (module)
48
- errors.push(Error("<style module> is not supported in the playground."));
49
- alerts.push(...errors);
54
+ if (module && !moduleWarning)
55
+ moduleWarning = Error("<style module> is not supported in the playground.");
56
+ styleErrors.push(...errors);
50
57
  return code;
51
58
  }))
52
- : Promise.resolve([]), { ast, content: source } = template ?? {}, { bindings, content, warnings } = script || scriptSetup ? compileScript(descriptor, { id }) : {};
53
- if (source && bindings)
59
+ : Promise.resolve([]), sucraseOptions = {
60
+ jsxRuntime: "preserve",
61
+ transforms: [...langs],
62
+ }, { ast, content: source = "" } = template ?? {}, { bindings, content, warnings: scriptWarnings, } = script || scriptSetup ? compileScript(descriptor, scriptOptions) : {};
63
+ if (bindings && isCompileTemplate)
54
64
  compilerOptions.bindingMetadata = bindings;
55
- const { code, errors, tips } = source !== undefined
65
+ const { code, errors: templateErrors, tips: templateTips, } = isCompileTemplate
56
66
  ? compileTemplate({
57
- ...(ast ?? {}),
58
- compilerOptions,
67
+ ...ast,
59
68
  filename,
60
69
  id,
61
- scoped,
62
- slotted,
63
70
  source,
71
+ ...templateOptions,
72
+ ...options?.template,
64
73
  })
65
74
  : {};
66
75
  const [styleResult, scriptResult, templateResult] = await Promise.all([
67
76
  style,
68
77
  content
69
- ? inject(langs.size ? transform(content, options).code : content)
78
+ ? inject(langs.size ? transform(content, sucraseOptions).code : content)
70
79
  : Promise.resolve(undefined),
71
80
  code
72
- ? inject(langs.size ? transform(code, options).code : code)
81
+ ? inject(langs.size ? transform(code, sucraseOptions).code : code)
73
82
  : Promise.resolve(undefined),
74
83
  ]), textContent = styleResult.join("\n").trim();
75
84
  if (textContent) {
@@ -80,7 +89,14 @@ export default async (filename) => {
80
89
  }
81
90
  Object.assign(component, scriptResult?.default);
82
91
  Object.assign(component, templateResult);
83
- [...alerts, ...(warnings ?? []), ...(tips ?? []), ...(errors ?? [])].forEach((msg) => {
92
+ [
93
+ ...parseErrors,
94
+ ...(scriptWarnings ?? []),
95
+ ...(templateTips ?? []),
96
+ ...(templateErrors ?? []),
97
+ ...styleErrors,
98
+ ...(moduleWarning ? [moduleWarning] : []),
99
+ ].forEach((msg) => {
84
100
  console.log(msg);
85
101
  });
86
102
  return component;
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.1.1",
24
+ "version": "2.2.1",
25
25
  "type": "module",
26
26
  "main": "./dist/loader-sfc.js",
27
27
  "types": "./dist/loader-sfc.d.ts",