@weapp-vite/volar 2.1.12 → 2.1.14

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.
package/dist/index.cjs CHANGED
@@ -688,6 +688,78 @@ function resolveScriptSetupLang(lang) {
688
688
  return lang === "js" ? "js" : "ts";
689
689
  }
690
690
  //#endregion
691
+ //#region src/templateRef.ts
692
+ const SERVICE_SCRIPT_RE = /^script_([jt]sx?)$/;
693
+ const WHITESPACE_RE = /\s/;
694
+ const LINE_TERMINATOR_RE = /[\n\r\u2028\u2029]/;
695
+ const BROWSER_ELEMENTS = "Omit<SVGElementTagNameMap, keyof HTMLElementTagNameMap> & HTMLElementTagNameMap";
696
+ const MINI_PROGRAM_ELEMENTS = "import('wevu').MiniProgramTemplateRefElements";
697
+ const LOCAL_ELEMENTS = `Omit<${BROWSER_ELEMENTS}, keyof ${MINI_PROGRAM_ELEMENTS}> & ${MINI_PROGRAM_ELEMENTS}`;
698
+ /** 线性扫描首条上游语法指令,保留跨行分隔符及空白 key/value 的匹配边界。 */
699
+ function readCompilerDirective(comment) {
700
+ let cursor = 0;
701
+ while (cursor < comment.length) {
702
+ while (cursor < comment.length && WHITESPACE_RE.test(comment[cursor])) cursor += 1;
703
+ if (cursor === comment.length) return;
704
+ if (comment[cursor] !== "@") {
705
+ while (cursor < comment.length && !LINE_TERMINATOR_RE.test(comment[cursor])) cursor += 1;
706
+ continue;
707
+ }
708
+ const keyStart = cursor + 1;
709
+ if (keyStart === comment.length || LINE_TERMINATOR_RE.test(comment[keyStart])) {
710
+ cursor = keyStart;
711
+ continue;
712
+ }
713
+ let separator = keyStart + 1;
714
+ while (separator < comment.length && !WHITESPACE_RE.test(comment[separator])) separator += 1;
715
+ if (separator === comment.length) return;
716
+ let valueStart = separator;
717
+ while (valueStart < comment.length && WHITESPACE_RE.test(comment[valueStart])) valueStart += 1;
718
+ const key = comment.slice(keyStart, separator);
719
+ if (valueStart === comment.length) {
720
+ valueStart -= 1;
721
+ while (valueStart > separator && LINE_TERMINATOR_RE.test(comment[valueStart])) valueStart -= 1;
722
+ return valueStart > separator ? {
723
+ key,
724
+ value: comment[valueStart]
725
+ } : void 0;
726
+ }
727
+ let valueEnd = valueStart;
728
+ while (valueEnd < comment.length && !LINE_TERMINATOR_RE.test(comment[valueEnd])) valueEnd += 1;
729
+ while (valueEnd > valueStart && WHITESPACE_RE.test(comment[valueEnd - 1])) valueEnd -= 1;
730
+ return {
731
+ key,
732
+ value: comment.slice(valueStart, valueEnd)
733
+ };
734
+ }
735
+ }
736
+ /** 仅解析文件级 lib 覆盖,保持上游每条注释首个配置与最后有效配置的语义。 */
737
+ function resolveTemplateRefLib(baseLib, comments) {
738
+ let lib = baseLib;
739
+ for (const comment of comments) {
740
+ const directive = readCompilerDirective(comment);
741
+ if (directive?.key !== "lib") continue;
742
+ try {
743
+ lib = JSON.parse(directive.value);
744
+ } catch {}
745
+ }
746
+ return lib;
747
+ }
748
+ /** 在上游服务脚本生成后,用模块局部类型覆盖小程序原生 ref,避免修改浏览器全局声明。 */
749
+ function createTemplateRefPlugin(baseLib) {
750
+ return {
751
+ name: "@weapp-vite/volar:template-ref",
752
+ version: PLUGIN_VERSION,
753
+ order: 1,
754
+ resolveEmbeddedCode(_fileName, sfc, embeddedCode) {
755
+ const match = SERVICE_SCRIPT_RE.exec(embeddedCode.id);
756
+ if (!match || resolveTemplateRefLib(baseLib, sfc.comments) !== "wevu") return;
757
+ const declaration = match[1].startsWith("j") ? `/** @typedef {${LOCAL_ELEMENTS}} __VLS_Elements */` : `type __VLS_Elements = ${LOCAL_ELEMENTS};`;
758
+ embeddedCode.content.push(`\n${declaration}\n`);
759
+ }
760
+ };
761
+ }
762
+ //#endregion
691
763
  //#region src/index.ts
692
764
  const require$1 = (0, node_module.createRequire)(typeof module !== "undefined" && module.filename ? module.filename : node_path.default.join(node_process.default.cwd(), "weapp-vite-volar.cjs"));
693
765
  let hasSchematicsTypes = false;
@@ -748,7 +820,7 @@ const plugin = (ctx) => {
748
820
  syncScriptBlockSource(scriptSetup);
749
821
  } else descriptor.scriptSetup = createSyntheticScriptSetup(wxsModuleNames);
750
822
  }
751
- return {
823
+ const parserPlugin = {
752
824
  name,
753
825
  version: PLUGIN_VERSION,
754
826
  order: -1,
@@ -772,6 +844,7 @@ const plugin = (ctx) => {
772
844
  resolveEmbeddedJsonBlock(fileName, sfc, embeddedCode, tsModule, hasSchematicsTypes);
773
845
  }
774
846
  };
847
+ return ctx?.vueCompilerOptions ? [parserPlugin, createTemplateRefPlugin(ctx.vueCompilerOptions.lib)] : parserPlugin;
775
848
  };
776
849
  //#endregion
777
850
  module.exports = plugin;
package/dist/index.mjs CHANGED
@@ -664,6 +664,78 @@ function resolveScriptSetupLang(lang) {
664
664
  return lang === "js" ? "js" : "ts";
665
665
  }
666
666
  //#endregion
667
+ //#region src/templateRef.ts
668
+ const SERVICE_SCRIPT_RE = /^script_([jt]sx?)$/;
669
+ const WHITESPACE_RE = /\s/;
670
+ const LINE_TERMINATOR_RE = /[\n\r\u2028\u2029]/;
671
+ const BROWSER_ELEMENTS = "Omit<SVGElementTagNameMap, keyof HTMLElementTagNameMap> & HTMLElementTagNameMap";
672
+ const MINI_PROGRAM_ELEMENTS = "import('wevu').MiniProgramTemplateRefElements";
673
+ const LOCAL_ELEMENTS = `Omit<${BROWSER_ELEMENTS}, keyof ${MINI_PROGRAM_ELEMENTS}> & ${MINI_PROGRAM_ELEMENTS}`;
674
+ /** 线性扫描首条上游语法指令,保留跨行分隔符及空白 key/value 的匹配边界。 */
675
+ function readCompilerDirective(comment) {
676
+ let cursor = 0;
677
+ while (cursor < comment.length) {
678
+ while (cursor < comment.length && WHITESPACE_RE.test(comment[cursor])) cursor += 1;
679
+ if (cursor === comment.length) return;
680
+ if (comment[cursor] !== "@") {
681
+ while (cursor < comment.length && !LINE_TERMINATOR_RE.test(comment[cursor])) cursor += 1;
682
+ continue;
683
+ }
684
+ const keyStart = cursor + 1;
685
+ if (keyStart === comment.length || LINE_TERMINATOR_RE.test(comment[keyStart])) {
686
+ cursor = keyStart;
687
+ continue;
688
+ }
689
+ let separator = keyStart + 1;
690
+ while (separator < comment.length && !WHITESPACE_RE.test(comment[separator])) separator += 1;
691
+ if (separator === comment.length) return;
692
+ let valueStart = separator;
693
+ while (valueStart < comment.length && WHITESPACE_RE.test(comment[valueStart])) valueStart += 1;
694
+ const key = comment.slice(keyStart, separator);
695
+ if (valueStart === comment.length) {
696
+ valueStart -= 1;
697
+ while (valueStart > separator && LINE_TERMINATOR_RE.test(comment[valueStart])) valueStart -= 1;
698
+ return valueStart > separator ? {
699
+ key,
700
+ value: comment[valueStart]
701
+ } : void 0;
702
+ }
703
+ let valueEnd = valueStart;
704
+ while (valueEnd < comment.length && !LINE_TERMINATOR_RE.test(comment[valueEnd])) valueEnd += 1;
705
+ while (valueEnd > valueStart && WHITESPACE_RE.test(comment[valueEnd - 1])) valueEnd -= 1;
706
+ return {
707
+ key,
708
+ value: comment.slice(valueStart, valueEnd)
709
+ };
710
+ }
711
+ }
712
+ /** 仅解析文件级 lib 覆盖,保持上游每条注释首个配置与最后有效配置的语义。 */
713
+ function resolveTemplateRefLib(baseLib, comments) {
714
+ let lib = baseLib;
715
+ for (const comment of comments) {
716
+ const directive = readCompilerDirective(comment);
717
+ if (directive?.key !== "lib") continue;
718
+ try {
719
+ lib = JSON.parse(directive.value);
720
+ } catch {}
721
+ }
722
+ return lib;
723
+ }
724
+ /** 在上游服务脚本生成后,用模块局部类型覆盖小程序原生 ref,避免修改浏览器全局声明。 */
725
+ function createTemplateRefPlugin(baseLib) {
726
+ return {
727
+ name: "@weapp-vite/volar:template-ref",
728
+ version: PLUGIN_VERSION,
729
+ order: 1,
730
+ resolveEmbeddedCode(_fileName, sfc, embeddedCode) {
731
+ const match = SERVICE_SCRIPT_RE.exec(embeddedCode.id);
732
+ if (!match || resolveTemplateRefLib(baseLib, sfc.comments) !== "wevu") return;
733
+ const declaration = match[1].startsWith("j") ? `/** @typedef {${LOCAL_ELEMENTS}} __VLS_Elements */` : `type __VLS_Elements = ${LOCAL_ELEMENTS};`;
734
+ embeddedCode.content.push(`\n${declaration}\n`);
735
+ }
736
+ };
737
+ }
738
+ //#endregion
667
739
  //#region src/index.ts
668
740
  const require = createRequire(typeof module !== "undefined" && module.filename ? module.filename : path.join(process.cwd(), "weapp-vite-volar.cjs"));
669
741
  let hasSchematicsTypes = false;
@@ -724,7 +796,7 @@ const plugin = (ctx) => {
724
796
  syncScriptBlockSource(scriptSetup);
725
797
  } else descriptor.scriptSetup = createSyntheticScriptSetup(wxsModuleNames);
726
798
  }
727
- return {
799
+ const parserPlugin = {
728
800
  name,
729
801
  version: PLUGIN_VERSION,
730
802
  order: -1,
@@ -748,6 +820,7 @@ const plugin = (ctx) => {
748
820
  resolveEmbeddedJsonBlock(fileName, sfc, embeddedCode, tsModule, hasSchematicsTypes);
749
821
  }
750
822
  };
823
+ return ctx?.vueCompilerOptions ? [parserPlugin, createTemplateRefPlugin(ctx.vueCompilerOptions.lib)] : parserPlugin;
751
824
  };
752
825
  //#endregion
753
826
  export { plugin as default };
@@ -0,0 +1,5 @@
1
+ import type { VueLanguagePluginReturn } from '@vue/language-core';
2
+ /** 仅解析文件级 lib 覆盖,保持上游每条注释首个配置与最后有效配置的语义。 */
3
+ export declare function resolveTemplateRefLib(baseLib: string, comments: readonly string[]): unknown;
4
+ /** 在上游服务脚本生成后,用模块局部类型覆盖小程序原生 ref,避免修改浏览器全局声明。 */
5
+ export declare function createTemplateRefPlugin(baseLib: string): VueLanguagePluginReturn;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapp-vite/volar",
3
- "version": "2.1.12",
3
+ "version": "2.1.14",
4
4
  "description": "Volar plugin for weapp-vite - Provides IntelliSense and type checking for WeChat mini-program config blocks",
5
5
  "author": "ice breaker <1324318532@qq.com>",
6
6
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "registry": "https://registry.npmjs.org"
49
49
  },
50
50
  "dependencies": {
51
- "@weapp-core/schematics": "6.2.8"
51
+ "@weapp-core/schematics": "6.2.10"
52
52
  },
53
53
  "scripts": {
54
54
  "benchmark": "node --import tsx benchmark/sfc.ts",