uview-pro 0.6.12 → 0.6.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.
@@ -1,133 +1,137 @@
1
- import { readFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { normalizePath } from 'vite';
4
-
5
- /**
6
- * Parse Vue SFC file, return descriptor
7
- */
8
- export async function parseSFC(code: string) {
9
- try {
10
- const { parse } = await import('vue/compiler-sfc');
11
- return parse(code, { pad: 'space' }).descriptor;
12
- } catch {
13
- throw new Error('[vite-plugin-uni-root] Vue version must be 3.2.13 or higher.');
14
- }
15
- }
16
-
17
- /**
18
- * Strip JSON comments (single-line and multi-line)
19
- */
20
- export function stripJsonComments(str: string): string {
21
- return str.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
22
- }
23
-
24
- /**
25
- * Convert pages.json path to absolute file path
26
- */
27
- export function formatPagePath(root: string, path: string) {
28
- return normalizePath(`${join(root, path)}.vue`).replace(/^[A-Z]:/, match => match.toLowerCase());
29
- }
30
-
31
- /**
32
- * Load pages.json and resolve all page paths (main + subPackages)
33
- */
34
- export function loadPagesJson(path: string, rootPath: string): string[] {
35
- const raw = readFileSync(path, 'utf-8');
36
- const { pages = [], subPackages = [] } = JSON.parse(stripJsonComments(raw));
37
-
38
- return [
39
- ...pages.map((page: any) => formatPagePath(rootPath, page.path)),
40
- ...subPackages
41
- .map(({ pages: subPages = [], root = '' }: any) => {
42
- return subPages.map((page: any) => formatPagePath(join(rootPath, root), page.path));
43
- })
44
- .flat()
45
- ];
46
- }
47
-
48
- /**
49
- * camelCase to kebab-case: PageMeta -> page-meta
50
- */
51
- export function toKebabCase(str: string) {
52
- return str
53
- .replace(/([a-z])([A-Z])/g, '$1-$2')
54
- .replace(/[_\s]+/g, '-')
55
- .toLowerCase();
56
- }
57
-
58
- /**
59
- * kebab-case to PascalCase: page-meta -> PageMeta
60
- */
61
- export function toPascalCase(str: string) {
62
- return str.replace(/(^\w|-+\w)/g, match => match.toUpperCase().replace(/-/g, ''));
63
- }
64
-
65
- interface TagNode {
66
- loc: {
67
- source: string;
68
- start: { offset: number };
69
- end: { offset: number };
70
- };
71
- }
72
-
73
- /**
74
- * Find a tag node in SFC template AST by tag name
75
- * Supports both kebab-case and PascalCase
76
- */
77
- export function findNode(sfc: any, rawTagName: string): TagNode | undefined {
78
- const templateSource = sfc.template?.content;
79
- if (!templateSource) return;
80
-
81
- let tagName = '';
82
- if (templateSource.includes(`<${toKebabCase(rawTagName)}`)) {
83
- tagName = toKebabCase(rawTagName);
84
- } else if (templateSource.includes(`<${toPascalCase(rawTagName)}`)) {
85
- tagName = toPascalCase(rawTagName);
86
- }
87
-
88
- if (!tagName) return;
89
-
90
- const nodeAst = sfc.template?.ast;
91
- if (!nodeAst) return;
92
-
93
- // Recursively traverse AST to find the target node
94
- const traverse = (nodes: any): TagNode | undefined => {
95
- for (const node of nodes) {
96
- if (node.type === 1) {
97
- if (node.tag === tagName) return node;
98
- if (node.children?.length) {
99
- const found = traverse(node.children);
100
- if (found) return found;
101
- }
102
- }
103
- }
104
- return undefined;
105
- };
106
-
107
- return traverse(nodeAst.children);
108
- }
109
-
110
- /**
111
- * Normalize platform-specific page paths (.mp-weixin.vue -> .vue)
112
- */
113
- export function normalizePlatformPath(id: string) {
114
- const platform = process.env.UNI_PLATFORM;
115
- if (!platform) return id;
116
-
117
- const regex = new RegExp(`\\.${platform}\\.vue$`);
118
- if (regex.test(id)) {
119
- return id.replace(`.${platform}.`, '.');
120
- }
121
- return id;
122
- }
123
-
124
- /**
125
- * Debounce utility
126
- */
127
- export function debounce<T extends (...args: any[]) => void>(fn: T, delay: number) {
128
- let timer: ReturnType<typeof setTimeout> | null = null;
129
- return (...args: Parameters<T>) => {
130
- if (timer) clearTimeout(timer);
131
- timer = setTimeout(() => fn(...args), delay);
132
- };
133
- }
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { normalizePath } from 'vite';
4
+
5
+ /**
6
+ * Parse Vue SFC file, return descriptor
7
+ *
8
+ * When vue/compiler-sfc is not available (e.g. HBuilderX projects where
9
+ * vue is not in node_modules), returns null instead of throwing.
10
+ * Callers should fall back to regex-based matching.
11
+ */
12
+ export async function parseSFC(code: string) {
13
+ try {
14
+ const { parse } = await import('vue/compiler-sfc');
15
+ return parse(code, { pad: 'space' }).descriptor;
16
+ } catch {
17
+ return null;
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Strip JSON comments (single-line and multi-line)
23
+ */
24
+ export function stripJsonComments(str: string): string {
25
+ return str.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
26
+ }
27
+
28
+ /**
29
+ * Convert pages.json path to absolute file path
30
+ */
31
+ export function formatPagePath(root: string, path: string) {
32
+ return normalizePath(`${join(root, path)}.vue`).replace(/^[A-Z]:/, match => match.toLowerCase());
33
+ }
34
+
35
+ /**
36
+ * Load pages.json and resolve all page paths (main + subPackages)
37
+ */
38
+ export function loadPagesJson(path: string, rootPath: string): string[] {
39
+ const raw = readFileSync(path, 'utf-8');
40
+ const { pages = [], subPackages = [] } = JSON.parse(stripJsonComments(raw));
41
+
42
+ return [
43
+ ...pages.map((page: any) => formatPagePath(rootPath, page.path)),
44
+ ...subPackages
45
+ .map(({ pages: subPages = [], root = '' }: any) => {
46
+ return subPages.map((page: any) => formatPagePath(join(rootPath, root), page.path));
47
+ })
48
+ .flat()
49
+ ];
50
+ }
51
+
52
+ /**
53
+ * camelCase to kebab-case: PageMeta -> page-meta
54
+ */
55
+ export function toKebabCase(str: string) {
56
+ return str
57
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
58
+ .replace(/[_\s]+/g, '-')
59
+ .toLowerCase();
60
+ }
61
+
62
+ /**
63
+ * kebab-case to PascalCase: page-meta -> PageMeta
64
+ */
65
+ export function toPascalCase(str: string) {
66
+ return str.replace(/(^\w|-+\w)/g, match => match.toUpperCase().replace(/-/g, ''));
67
+ }
68
+
69
+ interface TagNode {
70
+ loc: {
71
+ source: string;
72
+ start: { offset: number };
73
+ end: { offset: number };
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Find a tag node in SFC template AST by tag name
79
+ * Supports both kebab-case and PascalCase
80
+ */
81
+ export function findNode(sfc: any, rawTagName: string): TagNode | undefined {
82
+ const templateSource = sfc.template?.content;
83
+ if (!templateSource) return;
84
+
85
+ let tagName = '';
86
+ if (templateSource.includes(`<${toKebabCase(rawTagName)}`)) {
87
+ tagName = toKebabCase(rawTagName);
88
+ } else if (templateSource.includes(`<${toPascalCase(rawTagName)}`)) {
89
+ tagName = toPascalCase(rawTagName);
90
+ }
91
+
92
+ if (!tagName) return;
93
+
94
+ const nodeAst = sfc.template?.ast;
95
+ if (!nodeAst) return;
96
+
97
+ // Recursively traverse AST to find the target node
98
+ const traverse = (nodes: any): TagNode | undefined => {
99
+ for (const node of nodes) {
100
+ if (node.type === 1) {
101
+ if (node.tag === tagName) return node;
102
+ if (node.children?.length) {
103
+ const found = traverse(node.children);
104
+ if (found) return found;
105
+ }
106
+ }
107
+ }
108
+ return undefined;
109
+ };
110
+
111
+ return traverse(nodeAst.children);
112
+ }
113
+
114
+ /**
115
+ * Normalize platform-specific page paths (.mp-weixin.vue -> .vue)
116
+ */
117
+ export function normalizePlatformPath(id: string) {
118
+ const platform = process.env.UNI_PLATFORM;
119
+ if (!platform) return id;
120
+
121
+ const regex = new RegExp(`\\.${platform}\\.vue$`);
122
+ if (regex.test(id)) {
123
+ return id.replace(`.${platform}.`, '.');
124
+ }
125
+ return id;
126
+ }
127
+
128
+ /**
129
+ * Debounce utility
130
+ */
131
+ export function debounce<T extends (...args: any[]) => void>(fn: T, delay: number) {
132
+ let timer: ReturnType<typeof setTimeout> | null = null;
133
+ return (...args: Parameters<T>) => {
134
+ if (timer) clearTimeout(timer);
135
+ timer = setTimeout(() => fn(...args), delay);
136
+ };
137
+ }
package/readme.md CHANGED
@@ -4,8 +4,8 @@
4
4
  <h3 align="center" style="margin: 30px 0 30px;font-weight: bold;font-size:40px;">uView Pro</h3>
5
5
  <h3 align="center">uni-app Vue3 多平台快速开发的 UI 框架</h3>
6
6
 
7
- [![star](https://gitee.com/anyup/uView-Pro/badge/star.svg)](https://gitee.com/anyup/uView-Pro)
8
- [![fork](https://gitee.com/anyup/uView-Pro/badge/fork.svg)](https://gitee.com/anyup/uView-Pro)
7
+ [![star](https://gitee.com/anyup/uView-Pro/badge/star.svg?theme=gvp)](https://gitee.com/anyup/uView-Pro)
8
+ [![fork](https://gitee.com/anyup/uView-Pro/badge/fork.svg?theme=gvp)](https://gitee.com/anyup/uView-Pro)
9
9
  [![stars](https://img.shields.io/github/stars/anyup/uView-Pro?style=flat-square&logo=GitHub)](https://github.com/anyup/uView-Pro)
10
10
  [![forks](https://img.shields.io/github/forks/anyup/uView-Pro?style=flat-square&logo=GitHub)](https://github.com/anyup/uView-Pro)
11
11
  [![issues](https://img.shields.io/github/issues/anyup/uView-Pro?style=flat-square&logo=GitHub)](https://github.com/anyup/uView-Pro/issues)
package/types/index.d.ts CHANGED
@@ -1,19 +1,14 @@
1
- /// <reference path="./components.d.ts" />
2
- /// <reference path="./uni-app.d.ts" />
3
-
4
- import { $u } from '../libs';
5
-
6
- // uview-pro 模块类型声明
7
- declare module 'uview-pro' {
8
- // 导出安装函数
9
- export function install(): void;
10
- }
11
-
12
- // 全局类型扩展
13
- declare global {
14
- interface Uni {
15
- $u: typeof $u;
16
- }
17
- }
18
-
19
- export {};
1
+ /// <reference path="./components.d.ts" />
2
+ /// <reference path="./uni-app.d.ts" />
3
+
4
+ export * from './global';
5
+
6
+ declare global {
7
+ interface Uni {
8
+ $u: typeof import('../libs').$u;
9
+ }
10
+ }
11
+
12
+ declare module 'uview-pro' {
13
+ export function install(): void;
14
+ }