vite-plugin-vanjs 0.1.12 → 0.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/package.json +2 -2
- package/plugin/index.mjs +51 -19
- package/plugin/types.d.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-vanjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"author": "thednp",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A mini meta-framework for VanJS powered by Vite",
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
"@vitest/browser": "^4.1.2",
|
|
92
92
|
"@vitest/coverage-istanbul": "^4.1.2",
|
|
93
93
|
"@vitest/ui": "^4.1.2",
|
|
94
|
-
"happy-dom": "^20.8.
|
|
94
|
+
"happy-dom": "^20.8.9",
|
|
95
95
|
"typescript": "6.0.2",
|
|
96
96
|
"vite": "^8.0.3",
|
|
97
97
|
"vitest": "^4.1.2"
|
package/plugin/index.mjs
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
/** @typedef {typeof import("./types").VitePluginVanJS} VitePluginVanJS */
|
|
2
|
+
/** @typedef {import("./types").VanJSPluginOptions} VanJSPluginOptions */
|
|
1
3
|
/** @typedef {import("vite").ResolvedConfig} ResolvedConfig */
|
|
2
4
|
/** @typedef {import("./types").PageFile} PageFile */
|
|
3
5
|
/** @typedef {import("./types").RouteFile} RouteFile */
|
|
6
|
+
/** @typedef {import("vite").BuildAppHook} BuildAppHook */
|
|
7
|
+
/** @typedef {import("vite").TransformResult} TransformResult */
|
|
8
|
+
/** @typedef {import("vite").Plugin} Plugin */
|
|
9
|
+
/** @typedef {ThisParameterType<BuildAppHook>} PluginContext */
|
|
4
10
|
|
|
5
11
|
import { fileURLToPath } from "node:url";
|
|
6
12
|
import { dirname, join, resolve } from "node:path";
|
|
7
13
|
import process from "node:process";
|
|
8
|
-
import { transformWithOxc } from "vite";
|
|
9
14
|
import { routes } from "../router/routes.mjs";
|
|
10
15
|
import { generateRoute, getRoutes } from "./helpers.mjs";
|
|
11
16
|
|
|
@@ -49,6 +54,7 @@ const debugModuleAliases = {
|
|
|
49
54
|
"@vanjs/setup": "../setup/index-debug",
|
|
50
55
|
};
|
|
51
56
|
|
|
57
|
+
/** @type {VitePluginVanJS} */
|
|
52
58
|
export default function VitePluginVanJS(options = {}) {
|
|
53
59
|
const pluginConfig = { ...pluginDefaults, ...options };
|
|
54
60
|
const { routesDir } = pluginConfig;
|
|
@@ -57,6 +63,7 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
57
63
|
let config;
|
|
58
64
|
/** @type {RouteFile[] | null} */
|
|
59
65
|
let routeCache = null;
|
|
66
|
+
let isOxc = true;
|
|
60
67
|
|
|
61
68
|
const virtualModuleId = "virtual:@vanjs/routes";
|
|
62
69
|
const resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
@@ -64,6 +71,11 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
64
71
|
return {
|
|
65
72
|
name: "vanjs",
|
|
66
73
|
enforce: "pre",
|
|
74
|
+
buildStart() {
|
|
75
|
+
const viteVersion = this.meta?.viteVersion;
|
|
76
|
+
isOxc = Number(viteVersion[0]) >= 8;
|
|
77
|
+
},
|
|
78
|
+
// @ts-expect-error - this is temporary esbuild will be
|
|
67
79
|
config() {
|
|
68
80
|
return {
|
|
69
81
|
optimizeDeps: {
|
|
@@ -89,17 +101,29 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
89
101
|
"@vanjs/jsx": toAbsolute("../jsx"),
|
|
90
102
|
},
|
|
91
103
|
},
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
104
|
+
...(
|
|
105
|
+
isOxc
|
|
106
|
+
? {
|
|
107
|
+
oxc: {
|
|
108
|
+
include: /\.(jsx?|tsx?)$/,
|
|
109
|
+
jsx: {
|
|
110
|
+
runtime: "preserve", // Options: 'automatic', 'classic', 'preserve'
|
|
111
|
+
importSource: "@vanjs/jsx", // Default import source
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
: {
|
|
116
|
+
esbuild: {
|
|
117
|
+
jsx: "automatic",
|
|
118
|
+
jsxImportSource: "@vanjs/jsx",
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
),
|
|
96
122
|
};
|
|
97
123
|
},
|
|
98
|
-
/** @param {import("vite").ResolvedConfig} resolvedConfig */
|
|
99
124
|
configResolved(resolvedConfig) {
|
|
100
125
|
config = resolvedConfig;
|
|
101
126
|
},
|
|
102
|
-
/** @param {import("vite").ViteDevServer} server */
|
|
103
127
|
configureServer(server) {
|
|
104
128
|
// Watch routes directory
|
|
105
129
|
const pagesPath = join(config.root, routesDir);
|
|
@@ -129,7 +153,6 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
129
153
|
server.watcher.on("unlinkDir", changeHandler);
|
|
130
154
|
server.watcher.on("change", changeHandler);
|
|
131
155
|
},
|
|
132
|
-
/** @type {(source: string, importer: string | undefined, ops: { ssr: boolean }) => string | null} */
|
|
133
156
|
resolveId(source, importer, { ssr }) {
|
|
134
157
|
// Handle virtual module
|
|
135
158
|
if (source === virtualModuleId) {
|
|
@@ -171,11 +194,12 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
171
194
|
aliases = { ...aliases, ...debugModuleAliases };
|
|
172
195
|
}
|
|
173
196
|
// Check if the source is a known module
|
|
197
|
+
/* istanbul ignore next @preserve */
|
|
174
198
|
const matchedSource =
|
|
175
199
|
isResolvedVanFile || (source === "vanjs-core" && !isSetupFile)
|
|
176
200
|
? "@vanjs/van"
|
|
177
201
|
: isResolvedVanXFile ||
|
|
178
|
-
(source === "vanjs-ext" &&
|
|
202
|
+
(source === "vanjs-ext" &&
|
|
179
203
|
!isImportedVanXFile)
|
|
180
204
|
? "@vanjs/vanX"
|
|
181
205
|
: isResolvedSetupFile && (!ssr && isSetupFile || ssr && !isSetupFile)
|
|
@@ -189,7 +213,6 @@ export default function VitePluginVanJS(options = {}) {
|
|
|
189
213
|
|
|
190
214
|
return null;
|
|
191
215
|
},
|
|
192
|
-
/** @type {(id: string, ops: { ssr: boolean }) => Promise<({ code: string, map: null } | null)>} */
|
|
193
216
|
async load(id, ops) {
|
|
194
217
|
// istanbul ignore else
|
|
195
218
|
if (id === resolvedVirtualModuleId) {
|
|
@@ -212,21 +235,30 @@ routes.length = 0;
|
|
|
212
235
|
// Register routes
|
|
213
236
|
${currentRoutes.map(generateRoute).join("\n")}
|
|
214
237
|
${
|
|
215
|
-
ops.ssr && currentRoutes.length
|
|
216
|
-
|
|
217
|
-
: /* istanbul ignore next @preserve - satisfied */ ""
|
|
238
|
+
(ops && ops.ssr && currentRoutes.length) &&
|
|
239
|
+
`console.log(\`🍦 @vanjs/router registered ${currentRoutes.length} routes.\`)`
|
|
218
240
|
}
|
|
219
241
|
`;
|
|
220
242
|
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
243
|
+
const vite = await import("vite");
|
|
244
|
+
const transformer = isOxc ? "transformWithOxc" : "transformWithEsbuild";
|
|
245
|
+
const langProp = isOxc ? "lang" : "loader";
|
|
246
|
+
// const mapProp = isOxc ? "source_map" : "sourcemap";
|
|
247
|
+
|
|
248
|
+
const result = await vite[transformer](routesScript, id, {
|
|
249
|
+
[langProp]: "js",
|
|
250
|
+
sourcemap: true,
|
|
251
|
+
});
|
|
226
252
|
|
|
227
253
|
return {
|
|
228
254
|
code: result.code,
|
|
229
|
-
map:
|
|
255
|
+
map: result.map
|
|
256
|
+
? (
|
|
257
|
+
typeof result.map === "string"
|
|
258
|
+
? JSON.parse(result.map)
|
|
259
|
+
: /* istanbul ignore next @preserve */ result.map
|
|
260
|
+
)
|
|
261
|
+
: /* istanbul ignore next @preserve */ null,
|
|
230
262
|
};
|
|
231
263
|
}
|
|
232
264
|
return null;
|
package/plugin/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export * from "../router/types.d.ts";
|
|
|
5
5
|
export * from "../meta/types.d.ts";
|
|
6
6
|
export * from "../server/types.d.ts";
|
|
7
7
|
export * from "../client/types.d.ts";
|
|
8
|
-
import type {
|
|
8
|
+
import type { Plugin } from "vite";
|
|
9
9
|
|
|
10
10
|
export type VanJSPluginOptions = {
|
|
11
11
|
routesDir?: string;
|
|
@@ -14,6 +14,6 @@ export type VanJSPluginOptions = {
|
|
|
14
14
|
|
|
15
15
|
export declare const VitePluginVanJS: (
|
|
16
16
|
config?: VanJSPluginOptions,
|
|
17
|
-
) =>
|
|
17
|
+
) => Plugin<VanJSPluginOptions>;
|
|
18
18
|
|
|
19
19
|
export default VitePluginVanJS;
|