vite-plugin-vanjs 0.1.19 → 0.1.21

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![NPM Version](https://img.shields.io/npm/v/vite-plugin-vanjs.svg)](https://www.npmjs.com/package/vite-plugin-vanjs)
6
6
  [![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-vanjs.svg)](http://npm-stat.com/charts.html?package=vite-plugin-vanjs)
7
7
 
8
- A mini meta-framework for [VanJS](https://vanjs.org/) developed around the awesome [vite](https://vite.dev) and tested with [vitest](https://vitest.dev). The plugin comes with a set of modules to streamline your workflow:
8
+ An async first mini meta-framework for [VanJS](https://vanjs.org/) developed around the awesome [vite](https://vite.dev) and tested with [vitest](https://vitest.dev). The plugin comes with a set of modules to streamline your workflow:
9
9
  * ***@vanjs/router*** - one of the most important part of an application which allows you to split code and lazy load page like components with ease, handles both Client Side Rendering (SSR) and Server Side Rendering (CSR) and makes it really easy to work with;
10
10
  * ***@vanjs/meta*** - allows you to create metadata for your pages as well as load additional assets with ease;
11
11
  * ***@vanjs/jsx*** - enables JSX transformation with automatic namespace resolution;
package/meta/tags.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import van from "vanjs-core";
2
2
  import { addMeta } from "./Head.mjs";
3
3
 
4
- /** @typedef {import("vanjs-core").PropsWithKnownKeys} PropsWithKnownKeys */
4
+ /** @typedef {import("./types.d.ts").SupportedTags} SupportedTags */
5
+ /** @typedef {import("vanjs-core").PropsWithKnownKeys<T = SupportedTags>} PropsWithKnownKeys<T> */
5
6
  /** @typedef {import("./types.d.ts").TagProps} TagProps */
6
7
 
7
8
  /**
@@ -31,9 +32,25 @@ export const Meta = (props) => {
31
32
  export const Link = (props) => {
32
33
  const { link } = van.tags;
33
34
  if (props.rel === "stylesheet") {
34
- console.warn("Link doesn't support stylesheets.");
35
+ console.warn("Link doesn't support stylesheets, check the Wiki.");
35
36
  } else {
36
37
  addMeta(link(props));
37
38
  }
38
39
  return null;
39
40
  };
41
+
42
+ /**
43
+ * Add a new `<script>` tag, not to be used for .js files
44
+ * Only to be used as `<script type="application/ld+json">`
45
+ * for SEO purposes.
46
+ * @type {(props: PropsWithKnownKeys<HTMLScriptElement>) => null}
47
+ */
48
+ export const Script = (props) => {
49
+ const { script } = van.tags;
50
+ if (!props.type || props.type !== "application/ld+json") {
51
+ console.warn("Script doesn't support this type, check the Wiki.");
52
+ } else {
53
+ addMeta(script(props));
54
+ }
55
+ return null;
56
+ };
package/meta/types.d.ts CHANGED
@@ -15,6 +15,7 @@ export const initializeHeadTags: () => void | (() => Promise<void>);
15
15
  export type SupportedTags =
16
16
  | HTMLTitleElement
17
17
  | HTMLLinkElement
18
+ | HTMLScriptElement
18
19
  | HTMLMetaElement;
19
20
 
20
21
  export type AllHeadTags =
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "vite-plugin-vanjs",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "author": "thednp",
5
5
  "license": "MIT",
6
- "description": "A mini meta-framework for VanJS powered by Vite",
6
+ "description": "An async first mini meta-framework for VanJS powered by Vite",
7
7
  "repository": "https://github.com/thednp/vite-plugin-vanjs",
8
8
  "type": "module",
9
9
  "sideEffects": false,
@@ -7,6 +7,7 @@
7
7
  import { dirname, join, posix, win32 } from "node:path";
8
8
  import { existsSync } from "node:fs";
9
9
  import { readdir } from "node:fs/promises";
10
+ import process from "node:process";
10
11
 
11
12
  /**
12
13
  * Get the file most probable route path for a given potential route.
@@ -194,10 +195,21 @@ export const processLayoutRoutes = (routes, config, pluginConfig) => {
194
195
  * Scan and process routes and return them
195
196
  * @type {typeof import("./types").getRoutes}
196
197
  */
197
- // export const scanRoutes = async (config, pluginConfig) => {}
198
198
  export const getRoutes = async (config, pluginConfig) => {
199
199
  const routes = await scanRoutes(config, pluginConfig);
200
- return processLayoutRoutes(routes, config, pluginConfig);
200
+ const processed = processLayoutRoutes(routes, config, pluginConfig);
201
+ // istanbul ignore next - defaults can't be checked
202
+ const { excludeRoutes = [], excludeRoutesProd = [] } = pluginConfig;
203
+ const isProd = process.env.NODE_ENV === "production";
204
+ const allExcludes = [...excludeRoutes, ...(isProd ? excludeRoutesProd : [])];
205
+ if (allExcludes.length) {
206
+ return processed.filter((r) =>
207
+ !allExcludes.some((p) =>
208
+ r.routePath === p || r.routePath.startsWith(p + "/")
209
+ )
210
+ );
211
+ }
212
+ return processed;
201
213
  };
202
214
 
203
215
  /** @type {(route: RouteFile) => string} */
package/plugin/index.mjs CHANGED
@@ -22,6 +22,8 @@ const toAbsolute = (p) => resolve(__dirname, p);
22
22
  const pluginDefaults = {
23
23
  routesDir: "src/routes",
24
24
  extensions: [".tsx", ".jsx", ".ts", ".js"],
25
+ excludeRoutes: [],
26
+ excludeRoutesProd: [],
25
27
  };
26
28
 
27
29
  // Define module mapping configuration
package/plugin/types.d.ts CHANGED
@@ -10,6 +10,8 @@ import type { Plugin } from "vite";
10
10
  export type VanJSPluginOptions = {
11
11
  routesDir?: string;
12
12
  extensions?: string[];
13
+ excludeRoutes?: string[]; // NEW — excluded in all envs
14
+ excludeRoutesProd?: string[]; // NEW — excluded in production only
13
15
  };
14
16
 
15
17
  export declare const VitePluginVanJS: (
package/server/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { basename } from "node:path";
2
- // import { routerState } from "../router/index.mjs";
3
2
  import * as dataCache from "../router/dataCache.mjs";
4
3
  export * from "../plugin/helpers.mjs";
5
4
 
@@ -20,7 +19,10 @@ export const renderToString = async (inputSource) => {
20
19
  if (typeof source === "boolean") {
21
20
  return String(source);
22
21
  }
23
- if (typeof source === "object" && "render" in source) {
22
+ if (
23
+ source && typeof source === "object" && "render" in source &&
24
+ typeof source.render === "function"
25
+ ) {
24
26
  return source.render();
25
27
  }
26
28
  if (source instanceof Promise) {
@@ -46,25 +48,17 @@ export const renderToString = async (inputSource) => {
46
48
  * @param {string} file
47
49
  * @returns {string}
48
50
  */
51
+ // return `<link rel="preload" href="${file}" as="style" crossorigin>`;
49
52
  function renderPreloadLink(file) {
50
53
  if (file.endsWith(".js")) {
51
- return `<link rel="preload" href="${file}" as="script" crossorigin>`;
54
+ return `<link rel="modulepreload" href="${file}" crossorigin>`;
52
55
  } else if (file.endsWith(".css")) {
53
- return `<link rel="preload" href="${file}" as="style" crossorigin>`;
56
+ return `<link rel="stylesheet" href="${file}" crossorigin>`;
54
57
  } else if (file.endsWith(".woff")) {
55
- return ` <link rel="preload" href="${file}" as="font" type="font/woff" crossorigin>`;
58
+ return `<link rel="preload" href="${file}" as="font" type="font/woff" crossorigin>`;
56
59
  } else if (file.endsWith(".woff2")) {
57
- return ` <link rel="preload" href="${file}" as="font" type="font/woff2" crossorigin>`;
58
- } else if (file.endsWith(".gif")) {
59
- return `<link rel="preload" href="${file}" as="image" type="image/gif">`;
60
- } else if (file.endsWith(".jpg") || file.endsWith(".jpeg")) {
61
- return `<link rel="preload" href="${file}" as="image" type="image/jpeg">`;
62
- } else if (file.endsWith(".png")) {
63
- return `<link rel="preload" href="${file}" as="image" type="image/png">`;
64
- } else if (file.endsWith(".webp")) {
65
- return `<link rel="preload" href="${file}" as="image" type="image/webp">`;
60
+ return `<link rel="preload" href="${file}" as="font" type="font/woff2" crossorigin>`;
66
61
  } else {
67
- // @ts-ignore - this is server side code
68
62
  console.warn("Render error! File format not recognized: " + file);
69
63
  return "";
70
64
  }
@@ -131,7 +125,7 @@ export const getDataPreload = () => {
131
125
 
132
126
  if (cacheEntries.length > 0) {
133
127
  const json = JSON.stringify(cacheJSON);
134
- return `<script id="data-cache">window.__DATA_CACHE=${json}</script>`;
128
+ return `<script id="data-cache">window.__DATA_CACHE=${json};</script>`;
135
129
  }
136
130
  return "";
137
131
  };