vite-plugin-vanjs 0.0.3 → 0.0.4

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.
@@ -0,0 +1,97 @@
1
+ import { basename } from "node:path";
2
+
3
+ /**
4
+ * @type {typeof import("./types.d.ts").renderToString}
5
+ */
6
+ export const renderToString = async (inputSource) => {
7
+ const source = typeof inputSource === "function"
8
+ ? inputSource()
9
+ : inputSource;
10
+ if (typeof source === "number") {
11
+ return String(source);
12
+ }
13
+ if (typeof source === "string") {
14
+ return source.trim();
15
+ }
16
+ if (typeof source === "boolean") {
17
+ return String(source);
18
+ }
19
+ if (typeof source === "object" && "render" in source) {
20
+ return source.render();
21
+ }
22
+ if (source instanceof Promise) {
23
+ return renderToString(await source);
24
+ }
25
+ /* istanbul ignore else */
26
+ if (Array.isArray(source)) {
27
+ const elements = [];
28
+ for (const el of source) {
29
+ elements.push(await renderToString(el));
30
+ }
31
+ return elements.join("");
32
+ }
33
+ // return String(source)
34
+
35
+ // no source provided
36
+ // @ts-ignore - this is server side code
37
+ console.warn("Render error! Source not recognized: " + source);
38
+ return "";
39
+ };
40
+
41
+ /**
42
+ * @param {string} file
43
+ * @returns {string}
44
+ */
45
+ function renderPreloadLink(file) {
46
+ if (file.endsWith(".js") || file.endsWith(".mjs")) {
47
+ return `<link rel="modulepreload" as="script" crossorigin href="${file}">`;
48
+ } else if (file.endsWith(".css")) {
49
+ return `<link rel="stylesheet" href="${file}">`;
50
+ } else if (file.endsWith(".woff")) {
51
+ return ` <link rel="preload" href="${file}" as="font" type="font/woff" crossorigin>`;
52
+ } else if (file.endsWith(".woff2")) {
53
+ return ` <link rel="preload" href="${file}" as="font" type="font/woff2" crossorigin>`;
54
+ } else if (file.endsWith(".gif")) {
55
+ return ` <link rel="preload" href="${file}" as="image" type="image/gif">`;
56
+ } else if (file.endsWith(".jpg") || file.endsWith(".jpeg")) {
57
+ return ` <link rel="preload" href="${file}" as="image" type="image/jpeg">`;
58
+ } else if (file.endsWith(".png")) {
59
+ return ` <link rel="preload" href="${file}" as="image" type="image/png">`;
60
+ } else if (file.endsWith(".webp")) {
61
+ return ` <link rel="preload" href="${file}" as="image" type="image/webp">`;
62
+ } else {
63
+ // @ts-ignore - this is server side code
64
+ console.warn("Render error! File format not recognized: " + file);
65
+ return "";
66
+ }
67
+ }
68
+
69
+ /**
70
+ * @type {typeof import("./types.d.ts").renderPreloadLinks}
71
+ */
72
+ export function renderPreloadLinks(modules, manifest) {
73
+ let links = "";
74
+ const seen = new Set();
75
+ modules.forEach((id) => {
76
+ const files = manifest[id];
77
+ /* istanbul ignore else */
78
+ if (files?.length) {
79
+ files.forEach((file) => {
80
+ /* istanbul ignore else */
81
+ if (!seen.has(file)) {
82
+ seen.add(file);
83
+ const filename = basename(file);
84
+ /* istanbul ignore next - impossible to test */
85
+ if (manifest[filename]) {
86
+ for (const depFile of manifest[filename]) {
87
+ links += renderPreloadLink(depFile);
88
+ seen.add(depFile);
89
+ }
90
+ }
91
+ links += renderPreloadLink(file);
92
+ }
93
+ });
94
+ }
95
+ });
96
+ return links;
97
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "server",
3
+ "main": "./index.mjs",
4
+ "module": "./index.mjs",
5
+ "types": "./types.d.ts",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./types.d.ts",
10
+ "import": "./index.mjs"
11
+ }
12
+ },
13
+ "peerDependencies": {
14
+ "vite-plugin-vanjs": "*",
15
+ "mini-van-plate": "*",
16
+ "vanjs-core": "*",
17
+ "vanjs-ext": "*"
18
+ },
19
+ "sideEffects": false
20
+ }
@@ -0,0 +1,38 @@
1
+ /// <reference path="global.d.ts" />
2
+ import type { Element as VanElement, TagFunc } from "mini-van-plate/van-plate";
3
+
4
+ /**
5
+ * A function that takes a list of files and a manifest and returns a string
6
+ * representing the HTML markup for preload links.
7
+ * @param files the list of files
8
+ * @param manifest the vite manifest
9
+ * @returns HTML string
10
+ */
11
+ export const renderPreloadLinks: (
12
+ files: string[],
13
+ manifest: Record<string, string[]>,
14
+ ) => string;
15
+
16
+ type ValidVanNode =
17
+ | boolean
18
+ | number
19
+ | string
20
+ | VanElement
21
+ | TagFunc;
22
+
23
+ type VanComponent = () => ValidVanNode | ValidVanNode[];
24
+ export type Source =
25
+ | Promise<ValidVanNode>
26
+ | VanComponent
27
+ | (() => VanComponent)
28
+ | ValidVanNode
29
+ | ValidVanNode[]
30
+ | undefined;
31
+
32
+ /**
33
+ * A function that takes a multitude of source types and returns a string
34
+ * representing the HTML output.
35
+ * @param source the source
36
+ * @returns HTML string
37
+ */
38
+ export const renderToString: (source: Source) => Promise<string>;
@@ -2,11 +2,11 @@
2
2
  "name": "setup",
3
3
  "main": "./index.mjs",
4
4
  "module": "./index.mjs",
5
- "types": "./index.d.ts",
5
+ "types": "./types.d.ts",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {
9
- "types": "./index.d.ts",
9
+ "types": "./types.d.ts",
10
10
  "import": "./index.mjs"
11
11
  },
12
12
  "./van": {
package/setup/van.d.ts ADDED
@@ -0,0 +1 @@
1
+ export type { Van } from "vanjs-core";
@@ -0,0 +1 @@
1
+ export * from "vanjs-ext";
package/setup/vanX.mjs CHANGED
@@ -10,5 +10,5 @@ export const {
10
10
  list,
11
11
  replace,
12
12
  compact,
13
- } = "default" in vanX ? vanX.default : vanX;
13
+ } = "default" in vanX ? vanX.default : /* istanbul ignore next */ vanX;
14
14
  export default vanX;
package/src/index.mjs CHANGED
@@ -10,18 +10,16 @@ export default function VitePluginVanJS() {
10
10
  enforce: "pre",
11
11
  config() {
12
12
  return {
13
- build: {
14
- optimizeDeps: {
15
- include: ["vanjs-core", "vanjs-ext", "mini-van-plate"],
16
- },
17
- dedupe: ["vanjs-core", "vanjs-ext", "mini-van-plate"],
18
- },
19
13
  resolve: {
20
14
  alias: {
21
- "@vanjs/jsx": resolve(__dirname, "../jsx"),
22
15
  "@vanjs/setup": resolve(__dirname, "../setup"),
23
16
  "@vanjs/van": resolve(__dirname, "../setup/van"),
24
17
  "@vanjs/vanX": resolve(__dirname, "../setup/vanX"),
18
+ "@vanjs/client": resolve(__dirname, "../client"),
19
+ "@vanjs/server": resolve(__dirname, "../server"),
20
+ "@vanjs/meta": resolve(__dirname, "../meta"),
21
+ "@vanjs/router": resolve(__dirname, "../router"),
22
+ "@vanjs/jsx": resolve(__dirname, "../jsx"),
25
23
  },
26
24
  },
27
25
  esbuild: {
@@ -32,20 +30,23 @@ export default function VitePluginVanJS() {
32
30
  };
33
31
  },
34
32
  transform(code, id) {
35
- let newCode = code;
33
+ let newCode = String(code);
36
34
 
37
35
  const vanCoreReg = /import\s*.*\s*from\s*['"]vanjs-core['"]/g;
38
36
  const vanExtReg = /import\s*.*\s*from\s*['"]vanjs-ext['"]/g;
39
37
  const isSetupFile = /vite-plugin-vanjs[\\/]setup/.test(id);
40
38
  const isVanXFile = /vanjs-ext[\\/]src[\\/]van-x/.test(id);
41
39
 
40
+ /* istanbul ignore else */
42
41
  if (!isSetupFile && !isVanXFile) {
42
+ /* istanbul ignore next - the plugin works, istanbul isn't instrumenting this part properly */
43
43
  if (vanCoreReg.test(newCode)) {
44
44
  newCode = newCode.replace(
45
45
  vanCoreReg,
46
46
  (match) => match.replace("vanjs-core", "@vanjs/van"),
47
47
  );
48
48
  }
49
+ /* istanbul ignore next - the plugin works, istanbul isn't instrumenting this part properly */
49
50
  if (vanExtReg.test(newCode)) {
50
51
  newCode = newCode.replace(
51
52
  vanExtReg,
package/src/types.d.ts CHANGED
@@ -1,4 +1,29 @@
1
- import type { Plugin } from "vite";
2
- export * from "../jsx";
3
- export * from "../setup";
4
- export default function VitePluginVanJS(): Plugin;
1
+ export * from "../jsx/types";
2
+ export * from "../setup/types";
3
+ export * from "../router/types";
4
+ export * from "../meta/types";
5
+ export * from "../server/types";
6
+ export * from "../client/types";
7
+
8
+ declare const VitePluginVanJS: () => {
9
+ name: "string";
10
+ enforce: "pre" | "post" | undefined;
11
+ apply: "build";
12
+ config(): {
13
+ resolve: {
14
+ alias: Record<string, string>;
15
+ };
16
+ esBuild: {
17
+ jsx: "string";
18
+ jsxFactory: "string";
19
+ jsxFragment: "string";
20
+ };
21
+ };
22
+ transform(code: string, id?: string): {
23
+ code: string;
24
+ map: null;
25
+ };
26
+ };
27
+ export default VitePluginVanJS;
28
+
29
+ export {};
package/tsconfig.json CHANGED
@@ -26,8 +26,20 @@
26
26
  "noEmit": true,
27
27
  "checkJs": true,
28
28
  "jsx": "preserve",
29
- "jsxImportSource": "vite-plugin-vanjs",
29
+ "jsxImportSource": "@vanjs/jsx",
30
+ // "types": ["vanjs-core", "vanjs-ext", "mini-van-plate"],
30
31
  },
31
- "include": ["./src/**/*.ts", "./jsx/*.d.ts", "setup/**/*"],
32
+ "include": [
33
+ "./tests/*.ts",
34
+ "./tests/*.tsx",
35
+ "./src/*.ts",
36
+ "./jsx/*.ts",
37
+ "./setup/*.ts",
38
+ "./router/*.ts",
39
+ "./meta/*.ts",
40
+ "./server/*.ts",
41
+ "./client/*.ts",
42
+ // "./tests/vite-env.d.ts"
43
+ ],
32
44
  "exclude": ["node_modules", "experiments", "coverage"],
33
45
  }
package/jsx/index.d.ts DELETED
@@ -1,16 +0,0 @@
1
- /// <reference path="global.d.ts" />
2
- import type { JSX } from "./jsx.d.ts";
3
- import type { Fragment } from "./fragment.d.ts";
4
-
5
- declare function jsx(
6
- type: any,
7
- props: any,
8
- ): () =>
9
- | (Node & {
10
- [key: string]: any;
11
- })
12
- | (Node & {
13
- [key: string]: any;
14
- })[];
15
-
16
- export { Fragment, jsx, jsx as jsxDEV, jsx as jsxs };
package/jsx/utils.mjs DELETED
@@ -1,24 +0,0 @@
1
- export const setAttribute = (element, key, value) => {
2
- if (value == null || value === false || value === "") {
3
- element.removeAttribute(key);
4
- } else {
5
- const attr = value === true ? "" : String(value);
6
- element.setAttribute(key, attr);
7
- }
8
- };
9
-
10
- export const styleToString = (style) => {
11
- return typeof style === "string"
12
- ? style
13
- : typeof style === "object"
14
- ? Object.entries(style).reduce((acc, key) =>
15
- acc +
16
- key[0]
17
- .split(/(?=[A-Z])/)
18
- .join("-")
19
- .toLowerCase() +
20
- ":" +
21
- key[1] +
22
- ";", "")
23
- : "";
24
- };
File without changes