vite-react-ssg 0.3.1 → 0.3.3

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
@@ -2,6 +2,8 @@
2
2
 
3
3
  Static-site generation for React on Vite.
4
4
 
5
+ See demo(also document): [docs](https://vite-react-ssg.netlify.app/)
6
+
5
7
  [![NPM version](https://img.shields.io/npm/v/vite-react-ssg?color=a1b858&label=)](https://www.npmjs.com/package/vite-react-ssg)
6
8
 
7
9
  # Table of contents
package/dist/index.cjs CHANGED
@@ -149,6 +149,17 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
149
149
  if (isClient) {
150
150
  (async () => {
151
151
  const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
152
+ const lazeMatches = reactRouterDom.matchRoutes(routerOptions.routes, window.location)?.filter(
153
+ (m) => m.route.lazy
154
+ );
155
+ if (lazeMatches && lazeMatches?.length > 0) {
156
+ await Promise.all(
157
+ lazeMatches.map(async (m) => {
158
+ const routeModule = await m.route.lazy();
159
+ Object.assign(m.route, { ...routeModule, lazy: void 0 });
160
+ })
161
+ );
162
+ }
152
163
  const { router } = await createRoot(true);
153
164
  const app = /* @__PURE__ */ React__default.createElement(reactHelmetAsync.HelmetProvider, null, /* @__PURE__ */ React__default.createElement(SiteMetadataDefaults.SiteMetadataDefaults, null), /* @__PURE__ */ React__default.createElement(reactRouterDom.RouterProvider, { router }));
154
165
  if (!ssrWhenDev && undefined.DEV) {
@@ -157,17 +168,6 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
157
168
  root.render(app);
158
169
  });
159
170
  } else {
160
- const lazeMatches = reactRouterDom.matchRoutes(routerOptions.routes, window.location)?.filter(
161
- (m) => m.route.lazy
162
- );
163
- if (lazeMatches && lazeMatches?.length > 0) {
164
- await Promise.all(
165
- lazeMatches.map(async (m) => {
166
- const routeModule = await m.route.lazy();
167
- Object.assign(m.route, { ...routeModule, lazy: void 0 });
168
- })
169
- );
170
- }
171
171
  React__default.startTransition(() => {
172
172
  client.hydrateRoot(container, app);
173
173
  });
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RouterOptions, V as ViteReactSSGContext, a as ViteReactSSGClientOptions } from './types-c03d6a58.js';
2
- export { I as IndexRouteRecord, N as NonIndexRouteRecord, c as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './types-c03d6a58.js';
1
+ import { R as RouterOptions, V as ViteReactSSGContext, a as ViteReactSSGClientOptions } from './types-3aee4b6b.js';
2
+ export { I as IndexRouteRecord, N as NonIndexRouteRecord, c as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './types-3aee4b6b.js';
3
3
  import React, { ReactNode } from 'react';
4
4
  import { HelmetProps } from 'react-helmet-async';
5
5
  import { LinkProps, NavLinkProps } from 'react-router-dom';
package/dist/index.mjs CHANGED
@@ -144,6 +144,17 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
144
144
  if (isClient) {
145
145
  (async () => {
146
146
  const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
147
+ const lazeMatches = matchRoutes(routerOptions.routes, window.location)?.filter(
148
+ (m) => m.route.lazy
149
+ );
150
+ if (lazeMatches && lazeMatches?.length > 0) {
151
+ await Promise.all(
152
+ lazeMatches.map(async (m) => {
153
+ const routeModule = await m.route.lazy();
154
+ Object.assign(m.route, { ...routeModule, lazy: void 0 });
155
+ })
156
+ );
157
+ }
147
158
  const { router } = await createRoot$1(true);
148
159
  const app = /* @__PURE__ */ React.createElement(HelmetProvider, null, /* @__PURE__ */ React.createElement(SiteMetadataDefaults, null), /* @__PURE__ */ React.createElement(RouterProvider, { router }));
149
160
  if (!ssrWhenDev && import.meta.env.DEV) {
@@ -152,17 +163,6 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
152
163
  root.render(app);
153
164
  });
154
165
  } else {
155
- const lazeMatches = matchRoutes(routerOptions.routes, window.location)?.filter(
156
- (m) => m.route.lazy
157
- );
158
- if (lazeMatches && lazeMatches?.length > 0) {
159
- await Promise.all(
160
- lazeMatches.map(async (m) => {
161
- const routeModule = await m.route.lazy();
162
- Object.assign(m.route, { ...routeModule, lazy: void 0 });
163
- })
164
- );
165
- }
166
166
  React.startTransition(() => {
167
167
  hydrateRoot(container, app);
168
168
  });
package/dist/node/cli.cjs CHANGED
@@ -4,7 +4,7 @@ const kolorist = require('kolorist');
4
4
  const yargs = require('yargs');
5
5
  const helpers = require('yargs/helpers');
6
6
  const webFetch = require('@remix-run/web-fetch');
7
- const dev = require('../shared/vite-react-ssg.9cf631b2.cjs');
7
+ const dev = require('../shared/vite-react-ssg.69de2462.cjs');
8
8
  require('node:path');
9
9
  require('node:module');
10
10
  require('fs-extra');
package/dist/node/cli.mjs CHANGED
@@ -2,7 +2,7 @@ import { gray, bold, red, reset, underline } from 'kolorist';
2
2
  import yargs from 'yargs';
3
3
  import { hideBin } from 'yargs/helpers';
4
4
  import { Request, Response, fetch as fetch$1, Headers, FormData } from '@remix-run/web-fetch';
5
- import { b as build, d as dev } from '../shared/vite-react-ssg.f12aa7be.mjs';
5
+ import { b as build, d as dev } from '../shared/vite-react-ssg.f8eacfb1.mjs';
6
6
  import 'node:path';
7
7
  import 'node:module';
8
8
  import 'fs-extra';
package/dist/node.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const dev = require('./shared/vite-react-ssg.9cf631b2.cjs');
3
+ const dev = require('./shared/vite-react-ssg.69de2462.cjs');
4
4
  require('node:path');
5
5
  require('node:module');
6
6
  require('kolorist');
package/dist/node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { InlineConfig } from 'vite';
2
- import { b as ViteReactSSGOptions } from './types-c03d6a58.js';
2
+ import { b as ViteReactSSGOptions } from './types-3aee4b6b.js';
3
3
  import 'critters';
4
4
  import 'react';
5
5
  import 'react-router-dom';
package/dist/node.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { b as build, d as dev } from './shared/vite-react-ssg.f12aa7be.mjs';
1
+ export { b as build, d as dev } from './shared/vite-react-ssg.f8eacfb1.mjs';
2
2
  import 'node:path';
3
3
  import 'node:module';
4
4
  import 'kolorist';
@@ -867,7 +867,7 @@ ${kolorist.gray("[vite-react-ssg]")} ${kolorist.yellow(text)}${count ? kolorist.
867
867
  function getSize(str) {
868
868
  return `${(str.length / 1024).toFixed(2)} KiB`;
869
869
  }
870
- function routesToPaths(routes) {
870
+ async function routesToPaths(routes) {
871
871
  const pathToEntry = {};
872
872
  function addEntry(path, entry) {
873
873
  if (!entry)
@@ -883,17 +883,17 @@ function routesToPaths(routes) {
883
883
  return { paths: ["/"], pathToEntry };
884
884
  const paths = /* @__PURE__ */ new Set();
885
885
  const lazyPaths = /* @__PURE__ */ new Set();
886
- const getPaths = (routes2, prefix = "") => {
886
+ const getPaths = async (routes2, prefix = "") => {
887
887
  prefix = prefix.replace(/\/$/g, "");
888
888
  for (const route of routes2) {
889
889
  let path = route.path;
890
890
  path = handlePath(path, prefix, route.entry);
891
891
  if (route.getStaticPaths && path?.includes(":")) {
892
- const staticPaths = route.getStaticPaths();
892
+ const staticPaths = await route.getStaticPaths();
893
893
  for (let staticPath of staticPaths) {
894
894
  staticPath = handlePath(staticPath, prefix, route.entry);
895
895
  if (Array.isArray(route.children))
896
- getPaths(route.children, staticPath);
896
+ await getPaths(route.children, staticPath);
897
897
  }
898
898
  }
899
899
  if (route.lazy)
@@ -901,10 +901,10 @@ function routesToPaths(routes) {
901
901
  if (route.index)
902
902
  addEntry(prefix, route.entry);
903
903
  if (Array.isArray(route.children))
904
- getPaths(route.children, path);
904
+ await getPaths(route.children, path);
905
905
  }
906
906
  };
907
- getPaths(routes);
907
+ await getPaths(routes);
908
908
  return { paths: Array.from(paths), pathToEntry, lazyPaths: Array.from(lazyPaths) };
909
909
  function handlePath(path, prefix, entry) {
910
910
  if (path != null) {
@@ -950,7 +950,7 @@ async function resolveAlias(config, entry) {
950
950
  return result || node_path.join(config.root, entry);
951
951
  }
952
952
  const { version } = JSON.parse(
953
- node_fs.readFileSync(new URL("../../package.json", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('shared/vite-react-ssg.9cf631b2.cjs', document.baseURI).href)))).toString()
953
+ node_fs.readFileSync(new URL("../../package.json", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('shared/vite-react-ssg.69de2462.cjs', document.baseURI).href)))).toString()
954
954
  );
955
955
  function createRequest(path) {
956
956
  const url = new URL(path, "http://vite-react-ssg.com");
@@ -1233,13 +1233,13 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1233
1233
  const prefix = format === "esm" && process.platform === "win32" ? "file://" : "";
1234
1234
  const ext = format === "esm" ? ".mjs" : ".cjs";
1235
1235
  const serverEntry = node_path.join(prefix, ssgOut, node_path.parse(ssrEntry).name + ext);
1236
- const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('shared/vite-react-ssg.9cf631b2.cjs', document.baseURI).href)));
1236
+ const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('shared/vite-react-ssg.69de2462.cjs', document.baseURI).href)));
1237
1237
  const { createRoot, includedRoutes: serverEntryIncludedRoutes } = format === "esm" ? await import(serverEntry) : _require(serverEntry);
1238
1238
  const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes;
1239
1239
  const { routes } = await createRoot(false);
1240
- const { lazyPaths } = routesToPaths(routes);
1240
+ const { lazyPaths } = await routesToPaths(routes);
1241
1241
  const dataRoutes = await preLoad([...routes], lazyPaths);
1242
- const { paths, pathToEntry } = routesToPaths(dataRoutes);
1242
+ const { paths, pathToEntry } = await routesToPaths(dataRoutes);
1243
1243
  let routesPaths = includeAllRoutes ? paths : await includedRoutes(paths, routes || []);
1244
1244
  routesPaths = DefaultIncludedRoutes(routesPaths);
1245
1245
  routesPaths = Array.from(new Set(routesPaths));
@@ -859,7 +859,7 @@ ${gray("[vite-react-ssg]")} ${yellow(text)}${count ? blue(` (${count})`) : ""}`)
859
859
  function getSize(str) {
860
860
  return `${(str.length / 1024).toFixed(2)} KiB`;
861
861
  }
862
- function routesToPaths(routes) {
862
+ async function routesToPaths(routes) {
863
863
  const pathToEntry = {};
864
864
  function addEntry(path, entry) {
865
865
  if (!entry)
@@ -875,17 +875,17 @@ function routesToPaths(routes) {
875
875
  return { paths: ["/"], pathToEntry };
876
876
  const paths = /* @__PURE__ */ new Set();
877
877
  const lazyPaths = /* @__PURE__ */ new Set();
878
- const getPaths = (routes2, prefix = "") => {
878
+ const getPaths = async (routes2, prefix = "") => {
879
879
  prefix = prefix.replace(/\/$/g, "");
880
880
  for (const route of routes2) {
881
881
  let path = route.path;
882
882
  path = handlePath(path, prefix, route.entry);
883
883
  if (route.getStaticPaths && path?.includes(":")) {
884
- const staticPaths = route.getStaticPaths();
884
+ const staticPaths = await route.getStaticPaths();
885
885
  for (let staticPath of staticPaths) {
886
886
  staticPath = handlePath(staticPath, prefix, route.entry);
887
887
  if (Array.isArray(route.children))
888
- getPaths(route.children, staticPath);
888
+ await getPaths(route.children, staticPath);
889
889
  }
890
890
  }
891
891
  if (route.lazy)
@@ -893,10 +893,10 @@ function routesToPaths(routes) {
893
893
  if (route.index)
894
894
  addEntry(prefix, route.entry);
895
895
  if (Array.isArray(route.children))
896
- getPaths(route.children, path);
896
+ await getPaths(route.children, path);
897
897
  }
898
898
  };
899
- getPaths(routes);
899
+ await getPaths(routes);
900
900
  return { paths: Array.from(paths), pathToEntry, lazyPaths: Array.from(lazyPaths) };
901
901
  function handlePath(path, prefix, entry) {
902
902
  if (path != null) {
@@ -1229,9 +1229,9 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1229
1229
  const { createRoot, includedRoutes: serverEntryIncludedRoutes } = format === "esm" ? await import(serverEntry) : _require(serverEntry);
1230
1230
  const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes;
1231
1231
  const { routes } = await createRoot(false);
1232
- const { lazyPaths } = routesToPaths(routes);
1232
+ const { lazyPaths } = await routesToPaths(routes);
1233
1233
  const dataRoutes = await preLoad([...routes], lazyPaths);
1234
- const { paths, pathToEntry } = routesToPaths(dataRoutes);
1234
+ const { paths, pathToEntry } = await routesToPaths(dataRoutes);
1235
1235
  let routesPaths = includeAllRoutes ? paths : await includedRoutes(paths, routes || []);
1236
1236
  routesPaths = DefaultIncludedRoutes(routesPaths);
1237
1237
  routesPaths = Array.from(new Set(routesPaths));
@@ -153,7 +153,7 @@ interface CommonRouteOptions {
153
153
  *
154
154
  * @example () => ['path1', 'path2']
155
155
  */
156
- getStaticPaths?: () => string[];
156
+ getStaticPaths?: () => string[] | Promise<string[]>;
157
157
  }
158
158
  type NonIndexRouteRecord = Omit<NonIndexRouteObject, 'children'> & {
159
159
  children?: RouteRecord[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-react-ssg",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.3.3",
5
5
  "packageManager": "pnpm@8.6.6",
6
6
  "description": "",
7
7
  "author": "Riri <Daydreamerriri@outlook.com>",
@@ -62,6 +62,7 @@
62
62
  "build": "unbuild",
63
63
  "dev": "unbuild --stub",
64
64
  "lint": "eslint .",
65
+ "test": "pnpm -r --filter=./examples/* build && vitest run",
65
66
  "prepublishOnly": "nr build",
66
67
  "release": "bumpp && npm publish",
67
68
  "start": "esno src/index.ts",
@@ -110,9 +111,10 @@
110
111
  "@types/react-dom": "^18.2.6",
111
112
  "@types/yargs": "^17.0.24",
112
113
  "bumpp": "^9.1.0",
113
- "critters": "^0.0.19",
114
+ "critters": "^0.0.20",
114
115
  "eslint": "^8.45.0",
115
116
  "esno": "^0.17.0",
117
+ "fast-glob": "3.3.1",
116
118
  "p-queue": "^7.3.4",
117
119
  "react": "^18.2.0",
118
120
  "react-dom": "^18.2.0",
@@ -121,8 +123,8 @@
121
123
  "styled-components": "6.0.5",
122
124
  "typescript": "5.1.6",
123
125
  "unbuild": "^1.2.1",
124
- "vite": "^4.4.0",
126
+ "vite": "^4.4.9",
125
127
  "vite-plugin-pwa": "^0.16.4",
126
- "vitest": "0.33.0"
128
+ "vitest": "0.34.1"
127
129
  }
128
130
  }