@wsxjs/wsx-vite-plugin 0.0.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 WSX Framework Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**
4
+ * Vite Plugin for WSX (Web Component JSX)
5
+ *
6
+ * 专门处理.wsx文件:
7
+ * - 自动添加JSX pragma
8
+ * - 支持TypeScript编译
9
+ * - 完全隔离,不影响主项目React配置
10
+ */
11
+
12
+ interface WSXPluginOptions {
13
+ /**
14
+ * JSX工厂函数名
15
+ * @default 'h'
16
+ */
17
+ jsxFactory?: string;
18
+ /**
19
+ * JSX Fragment函数名
20
+ * @default 'Fragment'
21
+ */
22
+ jsxFragment?: string;
23
+ /**
24
+ * 是否启用调试日志
25
+ * @default false
26
+ */
27
+ debug?: boolean;
28
+ /**
29
+ * 文件扩展名
30
+ * @default ['.wsx']
31
+ */
32
+ extensions?: string[];
33
+ }
34
+ /**
35
+ * WSX Vite插件
36
+ */
37
+ declare function vitePluginWSX(options?: WSXPluginOptions): Plugin;
38
+
39
+ export { vitePluginWSX as wsx };
@@ -0,0 +1,39 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**
4
+ * Vite Plugin for WSX (Web Component JSX)
5
+ *
6
+ * 专门处理.wsx文件:
7
+ * - 自动添加JSX pragma
8
+ * - 支持TypeScript编译
9
+ * - 完全隔离,不影响主项目React配置
10
+ */
11
+
12
+ interface WSXPluginOptions {
13
+ /**
14
+ * JSX工厂函数名
15
+ * @default 'h'
16
+ */
17
+ jsxFactory?: string;
18
+ /**
19
+ * JSX Fragment函数名
20
+ * @default 'Fragment'
21
+ */
22
+ jsxFragment?: string;
23
+ /**
24
+ * 是否启用调试日志
25
+ * @default false
26
+ */
27
+ debug?: boolean;
28
+ /**
29
+ * 文件扩展名
30
+ * @default ['.wsx']
31
+ */
32
+ extensions?: string[];
33
+ }
34
+ /**
35
+ * WSX Vite插件
36
+ */
37
+ declare function vitePluginWSX(options?: WSXPluginOptions): Plugin;
38
+
39
+ export { vitePluginWSX as wsx };
package/dist/index.js ADDED
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ wsx: () => vite_plugin_wsx_default
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/vite-plugin-wsx.ts
28
+ var import_esbuild = require("esbuild");
29
+ function getJSXFactoryImportPath(_options) {
30
+ return "@wsxjs/wsx-core";
31
+ }
32
+ function vitePluginWSX(options = {}) {
33
+ const {
34
+ jsxFactory = "h",
35
+ jsxFragment = "Fragment",
36
+ debug = false,
37
+ extensions = [".wsx"]
38
+ } = options;
39
+ return {
40
+ name: "vite-plugin-wsx",
41
+ enforce: "pre",
42
+ // 确保在 React 插件之前执行
43
+ // 处理 .wsx 文件加载
44
+ load(id) {
45
+ const isWSXFile = extensions.some((ext) => id.endsWith(ext));
46
+ if (!isWSXFile) {
47
+ return null;
48
+ }
49
+ if (debug) {
50
+ console.log(`[WSX Plugin] Loading: ${id}`);
51
+ }
52
+ return null;
53
+ },
54
+ // 在transform阶段处理文件
55
+ async transform(code, id) {
56
+ const isWSXFile = extensions.some((ext) => id.endsWith(ext));
57
+ if (!isWSXFile) {
58
+ return null;
59
+ }
60
+ if (debug) {
61
+ console.log(`[WSX Plugin] Processing: ${id}`);
62
+ }
63
+ let transformedCode = code;
64
+ const hasWSXCoreImport = code.includes('from "@wsxjs/wsx-core"');
65
+ const hasJSXInImport = hasWSXCoreImport && (new RegExp(`[{,]\\s*${jsxFactory}\\s*[},]`).test(code) || new RegExp(`[{,]\\s*${jsxFragment}\\s*[},]`).test(code));
66
+ if (debug) {
67
+ console.log(`[WSX Plugin] Checking JSX imports for: ${id}`);
68
+ console.log(` - hasWSXCoreImport: ${hasWSXCoreImport}`);
69
+ console.log(` - hasJSXInImport: ${hasJSXInImport}`);
70
+ console.log(` - has < character: ${code.includes("<")}`);
71
+ console.log(` - has Fragment: ${code.includes("Fragment")}`);
72
+ }
73
+ if ((code.includes("<") || code.includes("Fragment")) && !hasJSXInImport) {
74
+ const importPath = getJSXFactoryImportPath(options);
75
+ const importStatement = `import { ${jsxFactory}, ${jsxFragment} } from "${importPath}";
76
+ `;
77
+ transformedCode = importStatement + transformedCode;
78
+ if (debug) {
79
+ console.log(`[WSX Plugin] Added JSX factory import to: ${id}`);
80
+ }
81
+ }
82
+ try {
83
+ const result = await (0, import_esbuild.transform)(transformedCode, {
84
+ loader: "tsx",
85
+ jsx: "transform",
86
+ jsxFactory,
87
+ jsxFragment,
88
+ target: "es2020",
89
+ format: "esm"
90
+ // Esbuild supports decorators natively with tsx loader
91
+ });
92
+ if (debug) {
93
+ console.log(`[WSX Plugin] JSX transformed: ${id}`);
94
+ }
95
+ return {
96
+ code: result.code,
97
+ map: null
98
+ };
99
+ } catch (error) {
100
+ console.error(`[WSX Plugin] Transform error for ${id}:`, error);
101
+ throw error;
102
+ }
103
+ },
104
+ // We handle JSX transformation directly in the transform hook
105
+ // No need to modify global esbuild config
106
+ // 构建开始时的日志
107
+ buildStart() {
108
+ if (debug) {
109
+ console.log(`[WSX Plugin] Build started with extensions: ${extensions.join(", ")}`);
110
+ console.log(`[WSX Plugin] JSX Factory: ${jsxFactory}, Fragment: ${jsxFragment}`);
111
+ }
112
+ }
113
+ };
114
+ }
115
+ var vite_plugin_wsx_default = vitePluginWSX;
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
118
+ wsx
119
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,92 @@
1
+ // src/vite-plugin-wsx.ts
2
+ import { transform } from "esbuild";
3
+ function getJSXFactoryImportPath(_options) {
4
+ return "@wsxjs/wsx-core";
5
+ }
6
+ function vitePluginWSX(options = {}) {
7
+ const {
8
+ jsxFactory = "h",
9
+ jsxFragment = "Fragment",
10
+ debug = false,
11
+ extensions = [".wsx"]
12
+ } = options;
13
+ return {
14
+ name: "vite-plugin-wsx",
15
+ enforce: "pre",
16
+ // 确保在 React 插件之前执行
17
+ // 处理 .wsx 文件加载
18
+ load(id) {
19
+ const isWSXFile = extensions.some((ext) => id.endsWith(ext));
20
+ if (!isWSXFile) {
21
+ return null;
22
+ }
23
+ if (debug) {
24
+ console.log(`[WSX Plugin] Loading: ${id}`);
25
+ }
26
+ return null;
27
+ },
28
+ // 在transform阶段处理文件
29
+ async transform(code, id) {
30
+ const isWSXFile = extensions.some((ext) => id.endsWith(ext));
31
+ if (!isWSXFile) {
32
+ return null;
33
+ }
34
+ if (debug) {
35
+ console.log(`[WSX Plugin] Processing: ${id}`);
36
+ }
37
+ let transformedCode = code;
38
+ const hasWSXCoreImport = code.includes('from "@wsxjs/wsx-core"');
39
+ const hasJSXInImport = hasWSXCoreImport && (new RegExp(`[{,]\\s*${jsxFactory}\\s*[},]`).test(code) || new RegExp(`[{,]\\s*${jsxFragment}\\s*[},]`).test(code));
40
+ if (debug) {
41
+ console.log(`[WSX Plugin] Checking JSX imports for: ${id}`);
42
+ console.log(` - hasWSXCoreImport: ${hasWSXCoreImport}`);
43
+ console.log(` - hasJSXInImport: ${hasJSXInImport}`);
44
+ console.log(` - has < character: ${code.includes("<")}`);
45
+ console.log(` - has Fragment: ${code.includes("Fragment")}`);
46
+ }
47
+ if ((code.includes("<") || code.includes("Fragment")) && !hasJSXInImport) {
48
+ const importPath = getJSXFactoryImportPath(options);
49
+ const importStatement = `import { ${jsxFactory}, ${jsxFragment} } from "${importPath}";
50
+ `;
51
+ transformedCode = importStatement + transformedCode;
52
+ if (debug) {
53
+ console.log(`[WSX Plugin] Added JSX factory import to: ${id}`);
54
+ }
55
+ }
56
+ try {
57
+ const result = await transform(transformedCode, {
58
+ loader: "tsx",
59
+ jsx: "transform",
60
+ jsxFactory,
61
+ jsxFragment,
62
+ target: "es2020",
63
+ format: "esm"
64
+ // Esbuild supports decorators natively with tsx loader
65
+ });
66
+ if (debug) {
67
+ console.log(`[WSX Plugin] JSX transformed: ${id}`);
68
+ }
69
+ return {
70
+ code: result.code,
71
+ map: null
72
+ };
73
+ } catch (error) {
74
+ console.error(`[WSX Plugin] Transform error for ${id}:`, error);
75
+ throw error;
76
+ }
77
+ },
78
+ // We handle JSX transformation directly in the transform hook
79
+ // No need to modify global esbuild config
80
+ // 构建开始时的日志
81
+ buildStart() {
82
+ if (debug) {
83
+ console.log(`[WSX Plugin] Build started with extensions: ${extensions.join(", ")}`);
84
+ console.log(`[WSX Plugin] JSX Factory: ${jsxFactory}, Fragment: ${jsxFragment}`);
85
+ }
86
+ }
87
+ };
88
+ }
89
+ var vite_plugin_wsx_default = vitePluginWSX;
90
+ export {
91
+ vite_plugin_wsx_default as wsx
92
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@wsxjs/wsx-vite-plugin",
3
+ "version": "0.0.5",
4
+ "description": "Vite plugin for WSX Framework",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "!**/__tests__",
19
+ "!**/test"
20
+ ],
21
+ "keywords": [
22
+ "vite",
23
+ "plugin",
24
+ "wsx",
25
+ "web-components"
26
+ ],
27
+ "dependencies": {
28
+ "@wsxjs/wsx-core": "0.0.5"
29
+ },
30
+ "devDependencies": {
31
+ "@types/jest": "^29.0.0",
32
+ "@types/node": "^20.0.0",
33
+ "jest": "^29.0.0",
34
+ "ts-jest": "^29.0.0",
35
+ "tsup": "^8.0.0",
36
+ "typescript": "^5.0.0",
37
+ "vite": "^5.0.0"
38
+ },
39
+ "peerDependencies": {
40
+ "vite": ">=4.0.0",
41
+ "esbuild": ">=0.25.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup src/index.ts --format cjs,esm --dts --external esbuild",
45
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch --external esbuild",
46
+ "test": "jest",
47
+ "test:watch": "jest --watch",
48
+ "test:coverage": "jest --coverage",
49
+ "typecheck": "tsc --noEmit",
50
+ "clean": "rm -rf dist coverage"
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as wsx } from "./vite-plugin-wsx";
@@ -0,0 +1,166 @@
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;