@shadow-js/vite 0.1.0
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 +21 -0
- package/README.md +298 -0
- package/dist/index.cjs +59 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jehaad AL-Johani
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# ShadowJS Vite Plugin
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@shadow-js/vite)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Vite plugin for ShadowJS that provides seamless development experience with hot reload, JSX compilation, and optimized build process.
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- **⚡ Hot Module Replacement**: Instant updates during development
|
|
12
|
+
- **🎯 JSX Compilation**: Automatic ShadowJS JSX transformations
|
|
13
|
+
- **🔧 Zero Configuration**: Works out of the box with sensible defaults
|
|
14
|
+
- **🎨 TypeScript Support**: Full TypeScript and TSX compilation
|
|
15
|
+
- **📦 Optimized Builds**: Production-ready bundle optimization
|
|
16
|
+
- **🔍 Source Maps**: Accurate debugging with source map support
|
|
17
|
+
- **🎭 Development Server**: Fast development server with HMR
|
|
18
|
+
- **📱 Modern Standards**: ES2022+ with modern browser support
|
|
19
|
+
|
|
20
|
+
## 📦 Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @shadow-js/vite
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Install together with ShadowJS:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install shadow-js @shadow-js/vite
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 🚀 Quick Start
|
|
33
|
+
|
|
34
|
+
### Basic Setup
|
|
35
|
+
|
|
36
|
+
**vite.config.ts**
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { defineConfig } from "vite";
|
|
40
|
+
import shadow from "@shadow-js/vite";
|
|
41
|
+
|
|
42
|
+
export default defineConfig({
|
|
43
|
+
plugins: [shadow()],
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**src/App.tsx**
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
import { useStore, Show } from "shadow-js";
|
|
51
|
+
|
|
52
|
+
function App() {
|
|
53
|
+
const [count, setCount] = useStore(0);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div>
|
|
57
|
+
<h1>ShadowJS Counter</h1>
|
|
58
|
+
<button onClick={() => setCount((c) => c + 1)}>Count: {count()}</button>
|
|
59
|
+
<Show when={count() > 5}>
|
|
60
|
+
<p>🎉 You reached {count()}!</p>
|
|
61
|
+
</Show>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default App;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**src/main.ts**
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { render } from "shadow-js";
|
|
73
|
+
import App from "./App";
|
|
74
|
+
|
|
75
|
+
render(App, document.getElementById("app")!);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**index.html**
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<!DOCTYPE html>
|
|
82
|
+
<html lang="en">
|
|
83
|
+
<head>
|
|
84
|
+
<meta charset="UTF-8" />
|
|
85
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
86
|
+
<title>ShadowJS App</title>
|
|
87
|
+
</head>
|
|
88
|
+
<body>
|
|
89
|
+
<div id="app"></div>
|
|
90
|
+
<script type="module" src="/src/main.ts"></script>
|
|
91
|
+
</body>
|
|
92
|
+
</html>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Development Commands
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
# Start development server
|
|
99
|
+
npm run dev
|
|
100
|
+
|
|
101
|
+
# Build for production
|
|
102
|
+
npm run build
|
|
103
|
+
|
|
104
|
+
# Preview production build
|
|
105
|
+
npm run preview
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 🎯 How It Works
|
|
109
|
+
|
|
110
|
+
The plugin integrates ShadowJS with Vite's build pipeline:
|
|
111
|
+
|
|
112
|
+
### 1. Development Mode
|
|
113
|
+
|
|
114
|
+
- **Hot Module Replacement**: Instant updates without page refresh
|
|
115
|
+
- **Source Maps**: Accurate debugging information
|
|
116
|
+
- **Error Overlay**: Clear error messages in browser
|
|
117
|
+
|
|
118
|
+
### 2. Build Process
|
|
119
|
+
|
|
120
|
+
- **JSX Compilation**: Transforms TSX/JSX using ShadowJS compiler
|
|
121
|
+
- **TypeScript Compilation**: Converts TypeScript to JavaScript
|
|
122
|
+
- **JSX Runtime Injection**: Automatically imports ShadowJS runtime
|
|
123
|
+
- **Code Splitting**: Optimizes bundle size with code splitting
|
|
124
|
+
- **Minification**: Production-ready code minification
|
|
125
|
+
|
|
126
|
+
### 3. Plugin Architecture
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
Source Code (.tsx)
|
|
130
|
+
↓
|
|
131
|
+
ShadowJS Compiler (JSX → Reactive)
|
|
132
|
+
↓
|
|
133
|
+
SWC (TypeScript → JavaScript)
|
|
134
|
+
↓
|
|
135
|
+
Vite (Bundling & Optimization)
|
|
136
|
+
↓
|
|
137
|
+
Output Bundle
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## 🔧 Configuration
|
|
141
|
+
|
|
142
|
+
### Basic Configuration
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// vite.config.ts
|
|
146
|
+
import { defineConfig } from "vite";
|
|
147
|
+
import shadow from "@shadow-js/vite";
|
|
148
|
+
|
|
149
|
+
export default defineConfig({
|
|
150
|
+
plugins: [shadow()],
|
|
151
|
+
// Your other Vite configuration
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Advanced Configuration
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// vite.config.ts
|
|
159
|
+
import { defineConfig } from "vite";
|
|
160
|
+
import shadow from "@shadow-js/vite";
|
|
161
|
+
|
|
162
|
+
export default defineConfig({
|
|
163
|
+
plugins: [
|
|
164
|
+
shadow({
|
|
165
|
+
// Plugin-specific options (future)
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
|
|
169
|
+
// Vite configuration
|
|
170
|
+
server: {
|
|
171
|
+
port: 3000,
|
|
172
|
+
open: true,
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
build: {
|
|
176
|
+
target: "es2022",
|
|
177
|
+
minify: "esbuild",
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
// TypeScript configuration
|
|
181
|
+
esbuild: {
|
|
182
|
+
// Vite handles TypeScript compilation
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### With Other Vite Plugins
|
|
188
|
+
|
|
189
|
+
The ShadowJS plugin works well with other Vite plugins:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { defineConfig } from "vite";
|
|
193
|
+
import shadow from "@shadow-js/vite";
|
|
194
|
+
import { viteStaticCopy } from "vite-plugin-static-copy";
|
|
195
|
+
|
|
196
|
+
export default defineConfig({
|
|
197
|
+
plugins: [
|
|
198
|
+
shadow(),
|
|
199
|
+
viteStaticCopy({
|
|
200
|
+
targets: [
|
|
201
|
+
{
|
|
202
|
+
src: "static/*",
|
|
203
|
+
dest: "assets",
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
}),
|
|
207
|
+
],
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 🏗️ Plugin Architecture
|
|
212
|
+
|
|
213
|
+
### Core Components
|
|
214
|
+
|
|
215
|
+
| Component | Description |
|
|
216
|
+
| ---------------------- | --------------------------------------------- |
|
|
217
|
+
| **JSX Transform** | Converts ShadowJS JSX to reactive expressions |
|
|
218
|
+
| **TypeScript Support** | Handles TypeScript compilation via SWC |
|
|
219
|
+
| **HMR Integration** | Manages hot module replacement |
|
|
220
|
+
| **Source Maps** | Generates source maps for debugging |
|
|
221
|
+
| **Error Handling** | Provides clear error messages |
|
|
222
|
+
|
|
223
|
+
### Transformation Pipeline
|
|
224
|
+
|
|
225
|
+
1. **File Detection**: Identifies `.tsx` and `.jsx` files
|
|
226
|
+
2. **ShadowJS Compilation**: Applies JSX transformations
|
|
227
|
+
3. **SWC Processing**: Handles TypeScript/JSX to JavaScript conversion
|
|
228
|
+
4. **Runtime Injection**: Adds necessary ShadowJS imports
|
|
229
|
+
5. **Source Map Generation**: Creates debugging source maps
|
|
230
|
+
|
|
231
|
+
### Development Features
|
|
232
|
+
|
|
233
|
+
- **Fast Refresh**: Instant updates during development
|
|
234
|
+
- **Error Boundaries**: Development-friendly error handling
|
|
235
|
+
- **Console Integration**: Clear logging and debugging messages
|
|
236
|
+
- **Type Checking**: Real-time TypeScript error reporting
|
|
237
|
+
|
|
238
|
+
## 🎨 Development Experience
|
|
239
|
+
|
|
240
|
+
### Hot Module Replacement
|
|
241
|
+
|
|
242
|
+
The plugin provides seamless HMR for ShadowJS components:
|
|
243
|
+
|
|
244
|
+
```jsx
|
|
245
|
+
// This component will hot-reload automatically
|
|
246
|
+
function Counter() {
|
|
247
|
+
const [count, setCount] = useStore(0);
|
|
248
|
+
|
|
249
|
+
return <button onClick={() => setCount((c) => c + 1)}>{count()}</button>;
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### TypeScript Integration
|
|
254
|
+
|
|
255
|
+
Full TypeScript support with IntelliSense:
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
// TypeScript types work perfectly
|
|
259
|
+
function UserComponent({ user }: { user: User }) {
|
|
260
|
+
const [isEditing, setIsEditing] = useStore(false);
|
|
261
|
+
|
|
262
|
+
return (
|
|
263
|
+
<div>
|
|
264
|
+
<h2>{user().name}</h2>
|
|
265
|
+
<button onClick={() => setIsEditing((v) => !v)}>
|
|
266
|
+
{isEditing() ? "Cancel" : "Edit"}
|
|
267
|
+
</button>
|
|
268
|
+
</div>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## 🤝 Contributing
|
|
274
|
+
|
|
275
|
+
We welcome any contributions! See the [Contributing Guide](../../CONTRIBUTING.md) for details.
|
|
276
|
+
|
|
277
|
+
Areas for contribution:
|
|
278
|
+
|
|
279
|
+
- Performance optimizations
|
|
280
|
+
- New plugin features
|
|
281
|
+
- Better error messages
|
|
282
|
+
- Enhanced TypeScript support
|
|
283
|
+
- Additional build optimizations
|
|
284
|
+
|
|
285
|
+
## 📄 License
|
|
286
|
+
|
|
287
|
+
MIT License - see [LICENSE](../../LICENSE) for details.
|
|
288
|
+
|
|
289
|
+
## 📞 Support
|
|
290
|
+
|
|
291
|
+
- **Documentation**: [Vite Plugin Guide](../../packages/vite/README.md)
|
|
292
|
+
- **Examples**: [Integration Examples](../../documentation/examples.md)
|
|
293
|
+
- **Issues**: [GitHub Issues](https://github.com/shadow-js/shadow/issues)
|
|
294
|
+
- **Discussions**: [GitHub Discussions](https://github.com/shadow-js/shadow/discussions)
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
Built by Jehaad AL-Johani for the ShadowJS ecosystem.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var compiler = require('@shadow-js/compiler');
|
|
4
|
+
var core = require('@swc/core');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
function shadow() {
|
|
8
|
+
return {
|
|
9
|
+
name: "@shadow-js/vite",
|
|
10
|
+
enforce: "pre",
|
|
11
|
+
// Prevent esbuild from also transforming JSX to avoid double transforms.
|
|
12
|
+
config() {
|
|
13
|
+
return {
|
|
14
|
+
esbuild: {
|
|
15
|
+
jsx: "preserve"
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
async transform(code, id) {
|
|
20
|
+
if (!/\.(tsx|jsx)$/.test(id)) return null;
|
|
21
|
+
if (id.includes("node_modules")) return null;
|
|
22
|
+
let transformedCode;
|
|
23
|
+
try {
|
|
24
|
+
transformedCode = compiler.transformSource(code);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
27
|
+
throw new Error(`ShadowJS (vite): compiler failed for ${id}: ${msg}`);
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const result = await core.transform(transformedCode, {
|
|
31
|
+
filename: id,
|
|
32
|
+
jsc: {
|
|
33
|
+
parser: { syntax: "typescript", tsx: true },
|
|
34
|
+
transform: {
|
|
35
|
+
react: {
|
|
36
|
+
runtime: "automatic",
|
|
37
|
+
importSource: "shadow-js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
target: "es2022"
|
|
41
|
+
},
|
|
42
|
+
module: { type: "es6" },
|
|
43
|
+
sourceMaps: true
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
code: result.code,
|
|
47
|
+
map: result.map ?? null
|
|
48
|
+
};
|
|
49
|
+
} catch (e) {
|
|
50
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
51
|
+
throw new Error(`ShadowJS (vite): SWC failed for ${id}: ${msg}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = shadow;
|
|
58
|
+
//# sourceMappingURL=index.cjs.map
|
|
59
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["transformSource","swcTransform"],"mappings":";;;;;;AA8De,SAAR,MAAA,GAAwC;AAC7C,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,iBAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA;AAAA,IAGT,MAAA,GAAS;AACP,MAAA,OAAO;AAAA,QACL,OAAA,EAAS;AAAA,UACP,GAAA,EAAK;AAAA;AACP,OACF;AAAA,IACF,CAAA;AAAA,IAEA,MAAM,SAAA,CAAU,IAAA,EAAM,EAAA,EAAI;AAExB,MAAA,IAAI,CAAC,cAAA,CAAe,IAAA,CAAK,EAAE,GAAG,OAAO,IAAA;AACrC,MAAA,IAAI,EAAA,CAAG,QAAA,CAAS,cAAc,CAAA,EAAG,OAAO,IAAA;AAGxC,MAAA,IAAI,eAAA;AACJ,MAAA,IAAI;AACF,QAAA,eAAA,GAAkBA,yBAAgB,IAAI,CAAA;AAAA,MACxC,SAAS,CAAA,EAAG;AACV,QAAA,MAAM,MAAM,CAAA,YAAa,KAAA,GAAQ,CAAA,CAAE,OAAA,GAAU,OAAO,CAAC,CAAA;AACrD,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,qCAAA,EAAwC,EAAE,CAAA,EAAA,EAAK,GAAG,CAAA,CAAE,CAAA;AAAA,MACtE;AAGA,MAAA,IAAI;AACF,QAAA,MAAM,MAAA,GAAS,MAAMC,cAAA,CAAa,eAAA,EAAiB;AAAA,UACjD,QAAA,EAAU,EAAA;AAAA,UACV,GAAA,EAAK;AAAA,YACH,MAAA,EAAQ,EAAE,MAAA,EAAQ,YAAA,EAAc,KAAK,IAAA,EAAK;AAAA,YAC1C,SAAA,EAAW;AAAA,cACT,KAAA,EAAO;AAAA,gBACL,OAAA,EAAS,WAAA;AAAA,gBACT,YAAA,EAAc;AAAA;AAChB,aACF;AAAA,YACA,MAAA,EAAQ;AAAA,WACV;AAAA,UACA,MAAA,EAAQ,EAAE,IAAA,EAAM,KAAA,EAAM;AAAA,UACtB,UAAA,EAAY;AAAA,SACb,CAAA;AAED,QAAA,OAAO;AAAA,UACL,MAAM,MAAA,CAAO,IAAA;AAAA,UACb,GAAA,EAAK,OAAO,GAAA,IAAO;AAAA,SACrB;AAAA,MACF,SAAS,CAAA,EAAG;AACV,QAAA,MAAM,MAAM,CAAA,YAAa,KAAA,GAAQ,CAAA,CAAE,OAAA,GAAU,OAAO,CAAC,CAAA;AACrD,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,gCAAA,EAAmC,EAAE,CAAA,EAAA,EAAK,GAAG,CAAA,CAAE,CAAA;AAAA,MACjE;AAAA,IACF;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { transformSource } from \"@shadow-js/compiler\";\nimport { transform as swcTransform } from \"@swc/core\";\n\n/**\n * Shadow Vite Plugin\n *\n * This plugin integrates Shadow with Vite to provide seamless development experience.\n * It handles the complete transformation pipeline from TSX/JSX source code to\n * Shadow-compatible JavaScript with reactive expressions.\n *\n * Plugin Pipeline:\n * 1. **Shadow Compiler**: Applies JSX transformations to inject reactive functions\n * 2. **SWC Transformation**: Converts TypeScript/JSX to JavaScript with proper imports\n * 3. **JSX Runtime Injection**: Automatically imports Shadow's JSX runtime functions\n * 4. **Source Map Generation**: Preserves debugging capabilities\n *\n * Features:\n * - **Selective Processing**: Only transforms user code (excludes node_modules)\n * - **TypeScript Support**: Full TypeScript and TSX compilation\n * - **Hot Module Replacement**: Compatible with Vite's HMR system\n * - **Error Handling**: Provides clear error messages for compilation failures\n * - **Performance**: Uses fast SWC compiler for optimal build speeds\n *\n * The plugin automatically configures Vite to preserve JSX for SWC processing\n * instead of using esbuild's JSX transform, ensuring compatibility with Shadow's\n * reactive programming model.\n *\n * @returns Vite PluginOption instance for Shadow integration\n *\n * @example\n * ```typescript\n * // vite.config.ts\n * import { defineConfig } from \"vite\";\n * import shadow from \"@shadow-js/vite\";\n *\n * export default defineConfig({\n * plugins: [shadow()]\n * })\n * ```\n *\n * @example\n * ```typescript\n * // Your component file (App.tsx)\n * import { useStore, Show } from \"shadow-js\";\n *\n * function App() {\n * const [count, setCount] = useStore(0)\n *\n * return (\n * <div>\n * <button onClick={() => setCount((c) => c + 1)}>\n * Increment\n * </button>\n * <Show when={count() > 0}>\n * <div>Count: {count()}</div>\n * </Show>\n * </div>\n * )\n * }\n * ```\n */\nexport default function shadow(): PluginOption {\n return {\n name: \"@shadow-js/vite\",\n enforce: \"pre\",\n\n // Prevent esbuild from also transforming JSX to avoid double transforms.\n config() {\n return {\n esbuild: {\n jsx: \"preserve\",\n },\n };\n },\n\n async transform(code, id) {\n // Only TSX/JSX user files\n if (!/\\.(tsx|jsx)$/.test(id)) return null;\n if (id.includes(\"node_modules\")) return null;\n\n // 1) Apply Shadow compiler transforms on source text\n let transformedCode: string;\n try {\n transformedCode = transformSource(code);\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n throw new Error(`ShadowJS (vite): compiler failed for ${id}: ${msg}`);\n }\n\n // 2) Use SWC to handle TS/JSX -> JS and inject jsx runtime imports\n try {\n const result = await swcTransform(transformedCode, {\n filename: id,\n jsc: {\n parser: { syntax: \"typescript\", tsx: true },\n transform: {\n react: {\n runtime: \"automatic\",\n importSource: \"shadow-js\",\n },\n },\n target: \"es2022\",\n },\n module: { type: \"es6\" },\n sourceMaps: true,\n });\n\n return {\n code: result.code,\n map: result.map ?? null,\n };\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n throw new Error(`ShadowJS (vite): SWC failed for ${id}: ${msg}`);\n }\n },\n } satisfies PluginOption;\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PluginOption } from 'vite';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shadow Vite Plugin
|
|
5
|
+
*
|
|
6
|
+
* This plugin integrates Shadow with Vite to provide seamless development experience.
|
|
7
|
+
* It handles the complete transformation pipeline from TSX/JSX source code to
|
|
8
|
+
* Shadow-compatible JavaScript with reactive expressions.
|
|
9
|
+
*
|
|
10
|
+
* Plugin Pipeline:
|
|
11
|
+
* 1. **Shadow Compiler**: Applies JSX transformations to inject reactive functions
|
|
12
|
+
* 2. **SWC Transformation**: Converts TypeScript/JSX to JavaScript with proper imports
|
|
13
|
+
* 3. **JSX Runtime Injection**: Automatically imports Shadow's JSX runtime functions
|
|
14
|
+
* 4. **Source Map Generation**: Preserves debugging capabilities
|
|
15
|
+
*
|
|
16
|
+
* Features:
|
|
17
|
+
* - **Selective Processing**: Only transforms user code (excludes node_modules)
|
|
18
|
+
* - **TypeScript Support**: Full TypeScript and TSX compilation
|
|
19
|
+
* - **Hot Module Replacement**: Compatible with Vite's HMR system
|
|
20
|
+
* - **Error Handling**: Provides clear error messages for compilation failures
|
|
21
|
+
* - **Performance**: Uses fast SWC compiler for optimal build speeds
|
|
22
|
+
*
|
|
23
|
+
* The plugin automatically configures Vite to preserve JSX for SWC processing
|
|
24
|
+
* instead of using esbuild's JSX transform, ensuring compatibility with Shadow's
|
|
25
|
+
* reactive programming model.
|
|
26
|
+
*
|
|
27
|
+
* @returns Vite PluginOption instance for Shadow integration
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // vite.config.ts
|
|
32
|
+
* import { defineConfig } from "vite";
|
|
33
|
+
* import shadow from "@shadow-js/vite";
|
|
34
|
+
*
|
|
35
|
+
* export default defineConfig({
|
|
36
|
+
* plugins: [shadow()]
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // Your component file (App.tsx)
|
|
43
|
+
* import { useStore, Show } from "shadow-js";
|
|
44
|
+
*
|
|
45
|
+
* function App() {
|
|
46
|
+
* const [count, setCount] = useStore(0)
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* <button onClick={() => setCount((c) => c + 1)}>
|
|
51
|
+
* Increment
|
|
52
|
+
* </button>
|
|
53
|
+
* <Show when={count() > 0}>
|
|
54
|
+
* <div>Count: {count()}</div>
|
|
55
|
+
* </Show>
|
|
56
|
+
* </div>
|
|
57
|
+
* )
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function shadow(): PluginOption;
|
|
62
|
+
|
|
63
|
+
export { shadow as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PluginOption } from 'vite';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shadow Vite Plugin
|
|
5
|
+
*
|
|
6
|
+
* This plugin integrates Shadow with Vite to provide seamless development experience.
|
|
7
|
+
* It handles the complete transformation pipeline from TSX/JSX source code to
|
|
8
|
+
* Shadow-compatible JavaScript with reactive expressions.
|
|
9
|
+
*
|
|
10
|
+
* Plugin Pipeline:
|
|
11
|
+
* 1. **Shadow Compiler**: Applies JSX transformations to inject reactive functions
|
|
12
|
+
* 2. **SWC Transformation**: Converts TypeScript/JSX to JavaScript with proper imports
|
|
13
|
+
* 3. **JSX Runtime Injection**: Automatically imports Shadow's JSX runtime functions
|
|
14
|
+
* 4. **Source Map Generation**: Preserves debugging capabilities
|
|
15
|
+
*
|
|
16
|
+
* Features:
|
|
17
|
+
* - **Selective Processing**: Only transforms user code (excludes node_modules)
|
|
18
|
+
* - **TypeScript Support**: Full TypeScript and TSX compilation
|
|
19
|
+
* - **Hot Module Replacement**: Compatible with Vite's HMR system
|
|
20
|
+
* - **Error Handling**: Provides clear error messages for compilation failures
|
|
21
|
+
* - **Performance**: Uses fast SWC compiler for optimal build speeds
|
|
22
|
+
*
|
|
23
|
+
* The plugin automatically configures Vite to preserve JSX for SWC processing
|
|
24
|
+
* instead of using esbuild's JSX transform, ensuring compatibility with Shadow's
|
|
25
|
+
* reactive programming model.
|
|
26
|
+
*
|
|
27
|
+
* @returns Vite PluginOption instance for Shadow integration
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // vite.config.ts
|
|
32
|
+
* import { defineConfig } from "vite";
|
|
33
|
+
* import shadow from "@shadow-js/vite";
|
|
34
|
+
*
|
|
35
|
+
* export default defineConfig({
|
|
36
|
+
* plugins: [shadow()]
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // Your component file (App.tsx)
|
|
43
|
+
* import { useStore, Show } from "shadow-js";
|
|
44
|
+
*
|
|
45
|
+
* function App() {
|
|
46
|
+
* const [count, setCount] = useStore(0)
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* <button onClick={() => setCount((c) => c + 1)}>
|
|
51
|
+
* Increment
|
|
52
|
+
* </button>
|
|
53
|
+
* <Show when={count() > 0}>
|
|
54
|
+
* <div>Count: {count()}</div>
|
|
55
|
+
* </Show>
|
|
56
|
+
* </div>
|
|
57
|
+
* )
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function shadow(): PluginOption;
|
|
62
|
+
|
|
63
|
+
export { shadow as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { transformSource } from '@shadow-js/compiler';
|
|
2
|
+
import { transform } from '@swc/core';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
function shadow() {
|
|
6
|
+
return {
|
|
7
|
+
name: "@shadow-js/vite",
|
|
8
|
+
enforce: "pre",
|
|
9
|
+
// Prevent esbuild from also transforming JSX to avoid double transforms.
|
|
10
|
+
config() {
|
|
11
|
+
return {
|
|
12
|
+
esbuild: {
|
|
13
|
+
jsx: "preserve"
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
async transform(code, id) {
|
|
18
|
+
if (!/\.(tsx|jsx)$/.test(id)) return null;
|
|
19
|
+
if (id.includes("node_modules")) return null;
|
|
20
|
+
let transformedCode;
|
|
21
|
+
try {
|
|
22
|
+
transformedCode = transformSource(code);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
25
|
+
throw new Error(`ShadowJS (vite): compiler failed for ${id}: ${msg}`);
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const result = await transform(transformedCode, {
|
|
29
|
+
filename: id,
|
|
30
|
+
jsc: {
|
|
31
|
+
parser: { syntax: "typescript", tsx: true },
|
|
32
|
+
transform: {
|
|
33
|
+
react: {
|
|
34
|
+
runtime: "automatic",
|
|
35
|
+
importSource: "shadow-js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
target: "es2022"
|
|
39
|
+
},
|
|
40
|
+
module: { type: "es6" },
|
|
41
|
+
sourceMaps: true
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
code: result.code,
|
|
45
|
+
map: result.map ?? null
|
|
46
|
+
};
|
|
47
|
+
} catch (e) {
|
|
48
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
49
|
+
throw new Error(`ShadowJS (vite): SWC failed for ${id}: ${msg}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { shadow as default };
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["swcTransform"],"mappings":";;;;AA8De,SAAR,MAAA,GAAwC;AAC7C,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,iBAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA;AAAA,IAGT,MAAA,GAAS;AACP,MAAA,OAAO;AAAA,QACL,OAAA,EAAS;AAAA,UACP,GAAA,EAAK;AAAA;AACP,OACF;AAAA,IACF,CAAA;AAAA,IAEA,MAAM,SAAA,CAAU,IAAA,EAAM,EAAA,EAAI;AAExB,MAAA,IAAI,CAAC,cAAA,CAAe,IAAA,CAAK,EAAE,GAAG,OAAO,IAAA;AACrC,MAAA,IAAI,EAAA,CAAG,QAAA,CAAS,cAAc,CAAA,EAAG,OAAO,IAAA;AAGxC,MAAA,IAAI,eAAA;AACJ,MAAA,IAAI;AACF,QAAA,eAAA,GAAkB,gBAAgB,IAAI,CAAA;AAAA,MACxC,SAAS,CAAA,EAAG;AACV,QAAA,MAAM,MAAM,CAAA,YAAa,KAAA,GAAQ,CAAA,CAAE,OAAA,GAAU,OAAO,CAAC,CAAA;AACrD,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,qCAAA,EAAwC,EAAE,CAAA,EAAA,EAAK,GAAG,CAAA,CAAE,CAAA;AAAA,MACtE;AAGA,MAAA,IAAI;AACF,QAAA,MAAM,MAAA,GAAS,MAAMA,SAAA,CAAa,eAAA,EAAiB;AAAA,UACjD,QAAA,EAAU,EAAA;AAAA,UACV,GAAA,EAAK;AAAA,YACH,MAAA,EAAQ,EAAE,MAAA,EAAQ,YAAA,EAAc,KAAK,IAAA,EAAK;AAAA,YAC1C,SAAA,EAAW;AAAA,cACT,KAAA,EAAO;AAAA,gBACL,OAAA,EAAS,WAAA;AAAA,gBACT,YAAA,EAAc;AAAA;AAChB,aACF;AAAA,YACA,MAAA,EAAQ;AAAA,WACV;AAAA,UACA,MAAA,EAAQ,EAAE,IAAA,EAAM,KAAA,EAAM;AAAA,UACtB,UAAA,EAAY;AAAA,SACb,CAAA;AAED,QAAA,OAAO;AAAA,UACL,MAAM,MAAA,CAAO,IAAA;AAAA,UACb,GAAA,EAAK,OAAO,GAAA,IAAO;AAAA,SACrB;AAAA,MACF,SAAS,CAAA,EAAG;AACV,QAAA,MAAM,MAAM,CAAA,YAAa,KAAA,GAAQ,CAAA,CAAE,OAAA,GAAU,OAAO,CAAC,CAAA;AACrD,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,gCAAA,EAAmC,EAAE,CAAA,EAAA,EAAK,GAAG,CAAA,CAAE,CAAA;AAAA,MACjE;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { transformSource } from \"@shadow-js/compiler\";\nimport { transform as swcTransform } from \"@swc/core\";\n\n/**\n * Shadow Vite Plugin\n *\n * This plugin integrates Shadow with Vite to provide seamless development experience.\n * It handles the complete transformation pipeline from TSX/JSX source code to\n * Shadow-compatible JavaScript with reactive expressions.\n *\n * Plugin Pipeline:\n * 1. **Shadow Compiler**: Applies JSX transformations to inject reactive functions\n * 2. **SWC Transformation**: Converts TypeScript/JSX to JavaScript with proper imports\n * 3. **JSX Runtime Injection**: Automatically imports Shadow's JSX runtime functions\n * 4. **Source Map Generation**: Preserves debugging capabilities\n *\n * Features:\n * - **Selective Processing**: Only transforms user code (excludes node_modules)\n * - **TypeScript Support**: Full TypeScript and TSX compilation\n * - **Hot Module Replacement**: Compatible with Vite's HMR system\n * - **Error Handling**: Provides clear error messages for compilation failures\n * - **Performance**: Uses fast SWC compiler for optimal build speeds\n *\n * The plugin automatically configures Vite to preserve JSX for SWC processing\n * instead of using esbuild's JSX transform, ensuring compatibility with Shadow's\n * reactive programming model.\n *\n * @returns Vite PluginOption instance for Shadow integration\n *\n * @example\n * ```typescript\n * // vite.config.ts\n * import { defineConfig } from \"vite\";\n * import shadow from \"@shadow-js/vite\";\n *\n * export default defineConfig({\n * plugins: [shadow()]\n * })\n * ```\n *\n * @example\n * ```typescript\n * // Your component file (App.tsx)\n * import { useStore, Show } from \"shadow-js\";\n *\n * function App() {\n * const [count, setCount] = useStore(0)\n *\n * return (\n * <div>\n * <button onClick={() => setCount((c) => c + 1)}>\n * Increment\n * </button>\n * <Show when={count() > 0}>\n * <div>Count: {count()}</div>\n * </Show>\n * </div>\n * )\n * }\n * ```\n */\nexport default function shadow(): PluginOption {\n return {\n name: \"@shadow-js/vite\",\n enforce: \"pre\",\n\n // Prevent esbuild from also transforming JSX to avoid double transforms.\n config() {\n return {\n esbuild: {\n jsx: \"preserve\",\n },\n };\n },\n\n async transform(code, id) {\n // Only TSX/JSX user files\n if (!/\\.(tsx|jsx)$/.test(id)) return null;\n if (id.includes(\"node_modules\")) return null;\n\n // 1) Apply Shadow compiler transforms on source text\n let transformedCode: string;\n try {\n transformedCode = transformSource(code);\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n throw new Error(`ShadowJS (vite): compiler failed for ${id}: ${msg}`);\n }\n\n // 2) Use SWC to handle TS/JSX -> JS and inject jsx runtime imports\n try {\n const result = await swcTransform(transformedCode, {\n filename: id,\n jsc: {\n parser: { syntax: \"typescript\", tsx: true },\n transform: {\n react: {\n runtime: \"automatic\",\n importSource: \"shadow-js\",\n },\n },\n target: \"es2022\",\n },\n module: { type: \"es6\" },\n sourceMaps: true,\n });\n\n return {\n code: result.code,\n map: result.map ?? null,\n };\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n throw new Error(`ShadowJS (vite): SWC failed for ${id}: ${msg}`);\n }\n },\n } satisfies PluginOption;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shadow-js/vite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite plugin for ShadowJS (JSX + transforms via SWC)",
|
|
5
|
+
"author": "Jehaad AL-Johani",
|
|
6
|
+
"homepage": "https://github.com/shadowjs-dev/shadow#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/shadowjs-dev/shadow.git",
|
|
10
|
+
"directory": "packages/vite"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/shadowjs-dev/shadow/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"shadowjs",
|
|
17
|
+
"vite",
|
|
18
|
+
"plugin",
|
|
19
|
+
"jsx",
|
|
20
|
+
"swc"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "dist/index.cjs",
|
|
29
|
+
"module": "dist/index.js",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"require": "./dist/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"dev": "tsup --watch",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@shadow-js/compiler": "^0.1.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@swc/core": "^1.3.96",
|
|
53
|
+
"shadow-js": "^0.1.0",
|
|
54
|
+
"vite": "^7.1.3"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@swc/core": "^1.13.3",
|
|
58
|
+
"@types/node": "^22.5.4",
|
|
59
|
+
"tsup": "^8.1.0",
|
|
60
|
+
"typescript": "^5.5.4"
|
|
61
|
+
},
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
}
|
|
65
|
+
}
|