vite-plugin-vanjs 0.1.12 → 0.1.13

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vanjs",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
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.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,10 @@ export default function VitePluginVanJS(options = {}) {
57
63
  let config;
58
64
  /** @type {RouteFile[] | null} */
59
65
  let routeCache = null;
66
+ /** @type {PluginContext} */
67
+ let context;
68
+ let viteVersion = "8.0.0";
69
+ let isOxc = true;
60
70
 
61
71
  const virtualModuleId = "virtual:@vanjs/routes";
62
72
  const resolvedVirtualModuleId = "\0" + virtualModuleId;
@@ -64,6 +74,12 @@ export default function VitePluginVanJS(options = {}) {
64
74
  return {
65
75
  name: "vanjs",
66
76
  enforce: "pre",
77
+ buildStart() {
78
+ context = this;
79
+ viteVersion = context.meta?.viteVersion[0];
80
+ isOxc = Number(viteVersion) >= 8;
81
+ },
82
+ // @ts-expect-error - this is temporary esbuild will be
67
83
  config() {
68
84
  return {
69
85
  optimizeDeps: {
@@ -89,17 +105,29 @@ export default function VitePluginVanJS(options = {}) {
89
105
  "@vanjs/jsx": toAbsolute("../jsx"),
90
106
  },
91
107
  },
92
- oxc: {
93
- jsx: "automatic",
94
- jsxImportSource: "@vanjs/jsx",
95
- },
108
+ ...(
109
+ isOxc
110
+ ? {
111
+ oxc: {
112
+ include: /\.(jsx?|tsx?)$/,
113
+ jsx: {
114
+ runtime: "preserve", // Options: 'automatic', 'classic', 'preserve'
115
+ importSource: "@vanjs/jsx", // Default import source
116
+ },
117
+ },
118
+ }
119
+ : {
120
+ esbuild: {
121
+ jsx: "automatic",
122
+ jsxImportSource: "@vanjs/jsx",
123
+ },
124
+ }
125
+ ),
96
126
  };
97
127
  },
98
- /** @param {import("vite").ResolvedConfig} resolvedConfig */
99
128
  configResolved(resolvedConfig) {
100
129
  config = resolvedConfig;
101
130
  },
102
- /** @param {import("vite").ViteDevServer} server */
103
131
  configureServer(server) {
104
132
  // Watch routes directory
105
133
  const pagesPath = join(config.root, routesDir);
@@ -129,7 +157,6 @@ export default function VitePluginVanJS(options = {}) {
129
157
  server.watcher.on("unlinkDir", changeHandler);
130
158
  server.watcher.on("change", changeHandler);
131
159
  },
132
- /** @type {(source: string, importer: string | undefined, ops: { ssr: boolean }) => string | null} */
133
160
  resolveId(source, importer, { ssr }) {
134
161
  // Handle virtual module
135
162
  if (source === virtualModuleId) {
@@ -171,11 +198,12 @@ export default function VitePluginVanJS(options = {}) {
171
198
  aliases = { ...aliases, ...debugModuleAliases };
172
199
  }
173
200
  // Check if the source is a known module
201
+ /* istanbul ignore next @preserve */
174
202
  const matchedSource =
175
203
  isResolvedVanFile || (source === "vanjs-core" && !isSetupFile)
176
204
  ? "@vanjs/van"
177
205
  : isResolvedVanXFile ||
178
- (source === "vanjs-ext" && /* istanbul ignore next @preserve */
206
+ (source === "vanjs-ext" &&
179
207
  !isImportedVanXFile)
180
208
  ? "@vanjs/vanX"
181
209
  : isResolvedSetupFile && (!ssr && isSetupFile || ssr && !isSetupFile)
@@ -189,7 +217,6 @@ export default function VitePluginVanJS(options = {}) {
189
217
 
190
218
  return null;
191
219
  },
192
- /** @type {(id: string, ops: { ssr: boolean }) => Promise<({ code: string, map: null } | null)>} */
193
220
  async load(id, ops) {
194
221
  // istanbul ignore else
195
222
  if (id === resolvedVirtualModuleId) {
@@ -212,21 +239,31 @@ routes.length = 0;
212
239
  // Register routes
213
240
  ${currentRoutes.map(generateRoute).join("\n")}
214
241
  ${
215
- ops.ssr && currentRoutes.length
242
+ (ops && ops.ssr && currentRoutes.length)
216
243
  ? `console.log(\`🍦 @vanjs/router registered ${currentRoutes.length} routes.\`)`
217
- : /* istanbul ignore next @preserve - satisfied */ ""
244
+ : /* istanbul ignore next @preserve */ ""
218
245
  }
219
246
  `;
220
247
 
221
- const result = await transformWithOxc(
222
- routesScript,
223
- id,
224
- { lang: "js" },
225
- );
248
+ const vite = await import("vite");
249
+ const transformer = isOxc ? "transformWithOxc" : "transformWithEsbuild";
250
+ const langProp = isOxc ? "lang" : "loader";
251
+ // const mapProp = isOxc ? "source_map" : "sourcemap";
252
+
253
+ const result = await vite[transformer](routesScript, id, {
254
+ [langProp]: "js",
255
+ sourcemap: true,
256
+ });
226
257
 
227
258
  return {
228
259
  code: result.code,
229
- map: null,
260
+ map: result.map
261
+ ? (
262
+ typeof result.map === "string"
263
+ ? JSON.parse(result.map)
264
+ : /* istanbul ignore next @preserve */ result.map
265
+ )
266
+ : /* istanbul ignore next @preserve */ null,
230
267
  };
231
268
  }
232
269
  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 { PluginOption } from "vite";
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
- ) => PluginOption<VanJSPluginOptions>;
17
+ ) => Plugin<VanJSPluginOptions>;
18
18
 
19
19
  export default VitePluginVanJS;