@wsxjs/wsx-vite-plugin 0.0.7 → 0.0.8

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,166 +0,0 @@
1
- /* eslint-disable no-console */
2
- /**
3
- * Vite Plugin for WSX (Web Component JSX)
4
- *
5
- * 专门处理.wsx文件:
6
- * - 自动添加JSX pragma
7
- * - 支持TypeScript编译
8
- * - 完全隔离,不影响主项目React配置
9
- */
10
-
11
- import type { Plugin } from "vite";
12
- import { transform } from "esbuild";
13
-
14
- export interface WSXPluginOptions {
15
- /**
16
- * JSX工厂函数名
17
- * @default 'h'
18
- */
19
- jsxFactory?: string;
20
-
21
- /**
22
- * JSX Fragment函数名
23
- * @default 'Fragment'
24
- */
25
- jsxFragment?: string;
26
-
27
- /**
28
- * 是否启用调试日志
29
- * @default false
30
- */
31
- debug?: boolean;
32
-
33
- /**
34
- * 文件扩展名
35
- * @default ['.wsx']
36
- */
37
- extensions?: string[];
38
- }
39
-
40
- /**
41
- * 获取 JSX 工厂函数的导入路径
42
- */
43
- function getJSXFactoryImportPath(_options: WSXPluginOptions): string {
44
- // 使用 @wsxjs/wsx-core 包中的 JSX 工厂
45
- return "@wsxjs/wsx-core";
46
- }
47
-
48
- /**
49
- * WSX Vite插件
50
- */
51
- export function vitePluginWSX(options: WSXPluginOptions = {}): Plugin {
52
- const {
53
- jsxFactory = "h",
54
- jsxFragment = "Fragment",
55
- debug = false,
56
- extensions = [".wsx"],
57
- } = options;
58
-
59
- return {
60
- name: "vite-plugin-wsx",
61
- enforce: "pre", // 确保在 React 插件之前执行
62
-
63
- // 处理 .wsx 文件加载
64
- load(id: string) {
65
- const isWSXFile = extensions.some((ext) => id.endsWith(ext));
66
-
67
- if (!isWSXFile) {
68
- return null;
69
- }
70
-
71
- if (debug) {
72
- console.log(`[WSX Plugin] Loading: ${id}`);
73
- }
74
-
75
- // 返回 null 让 Vite 继续处理文件
76
- return null;
77
- },
78
-
79
- // 在transform阶段处理文件
80
- async transform(code: string, id: string) {
81
- // 检查是否是WSX文件
82
- const isWSXFile = extensions.some((ext) => id.endsWith(ext));
83
-
84
- if (!isWSXFile) {
85
- return null;
86
- }
87
-
88
- if (debug) {
89
- console.log(`[WSX Plugin] Processing: ${id}`);
90
- }
91
-
92
- let transformedCode = code;
93
-
94
- // 1. 检查是否已经有JSX工厂导入
95
- const hasWSXCoreImport = code.includes('from "@wsxjs/wsx-core"');
96
- // 更精确的检测:使用正则表达式检查 JSX 工厂函数是否在导入中
97
- const hasJSXInImport =
98
- hasWSXCoreImport &&
99
- (new RegExp(`[{,]\\s*${jsxFactory}\\s*[},]`).test(code) ||
100
- new RegExp(`[{,]\\s*${jsxFragment}\\s*[},]`).test(code));
101
-
102
- // 调试信息
103
- if (debug) {
104
- console.log(`[WSX Plugin] Checking JSX imports for: ${id}`);
105
- console.log(` - hasWSXCoreImport: ${hasWSXCoreImport}`);
106
- console.log(` - hasJSXInImport: ${hasJSXInImport}`);
107
- console.log(` - has < character: ${code.includes("<")}`);
108
- console.log(` - has Fragment: ${code.includes("Fragment")}`);
109
- }
110
-
111
- // 如果有JSX语法但没有JSX工厂导入,则需要注入
112
- if ((code.includes("<") || code.includes("Fragment")) && !hasJSXInImport) {
113
- // 使用标准的包导入
114
- const importPath = getJSXFactoryImportPath(options);
115
- const importStatement = `import { ${jsxFactory}, ${jsxFragment} } from "${importPath}";\n`;
116
- transformedCode = importStatement + transformedCode;
117
-
118
- if (debug) {
119
- console.log(`[WSX Plugin] Added JSX factory import to: ${id}`);
120
- }
121
- }
122
-
123
- // 2. 添加JSX pragma - 不添加,让esbuild使用jsxFactory config
124
- // JSX transformation will be handled by esbuild with our custom config
125
-
126
- // 3. 使用 esbuild 进行 JSX 转换
127
-
128
- try {
129
- const result = await transform(transformedCode, {
130
- loader: "tsx",
131
- jsx: "transform",
132
- jsxFactory: jsxFactory,
133
- jsxFragment: jsxFragment,
134
- target: "es2020",
135
- format: "esm",
136
- // Esbuild supports decorators natively with tsx loader
137
- });
138
-
139
- if (debug) {
140
- console.log(`[WSX Plugin] JSX transformed: ${id}`);
141
- }
142
-
143
- return {
144
- code: result.code,
145
- map: null,
146
- };
147
- } catch (error) {
148
- console.error(`[WSX Plugin] Transform error for ${id}:`, error);
149
- throw error;
150
- }
151
- },
152
-
153
- // We handle JSX transformation directly in the transform hook
154
- // No need to modify global esbuild config
155
-
156
- // 构建开始时的日志
157
- buildStart() {
158
- if (debug) {
159
- console.log(`[WSX Plugin] Build started with extensions: ${extensions.join(", ")}`);
160
- console.log(`[WSX Plugin] JSX Factory: ${jsxFactory}, Fragment: ${jsxFragment}`);
161
- }
162
- },
163
- };
164
- }
165
-
166
- export default vitePluginWSX;