vite-react-ssg 0.1.1 → 0.2.0

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
@@ -6,8 +6,6 @@ Static-site generation for React on Vite.
6
6
 
7
7
  ## Install
8
8
 
9
- > **This library requires Node.js version >= 17**
10
- > or `Request` is available
11
9
  <pre>
12
10
  <b>npm i -D vite-react-ssg</b> <em>react-router-dom</em>
13
11
  </pre>
@@ -176,11 +174,14 @@ Use the `getStyleCollector` option to specify an SSR/SSG style collector. Curren
176
174
 
177
175
  ```tsx
178
176
  import { ViteReactSSG } from 'vite-react-ssg'
179
- import { getStyledComponentsCollector } from 'vite-react-ssg/style-collectors'
177
+ import getStyledComponentsCollector from 'vite-react-ssg/style-collectors/styled-components'
180
178
  import { routes } from './App.js'
181
179
  import './index.css'
182
180
 
183
- export const createRoot = ViteReactSSG({ routes }, () => {}, { getStyleCollector: getStyledComponentsCollector })
181
+ export const createRoot = ViteReactSSG(
182
+ { routes },
183
+ () => { },
184
+ { getStyleCollector: getStyledComponentsCollector })
184
185
  ```
185
186
 
186
187
  You can provide your own by looking at the [implementation](./src/style-collectors/) of any of the existing collectors.
package/dist/node/cli.cjs CHANGED
@@ -3,7 +3,8 @@
3
3
  const kolorist = require('kolorist');
4
4
  const yargs = require('yargs');
5
5
  const helpers = require('yargs/helpers');
6
- const dev = require('../shared/vite-react-ssg.8cbf43cc.cjs');
6
+ const webFetch = require('@remix-run/web-fetch');
7
+ const dev = require('../shared/vite-react-ssg.b0fd1823.cjs');
7
8
  require('node:path');
8
9
  require('node:module');
9
10
  require('fs-extra');
@@ -22,6 +23,44 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
22
23
 
23
24
  const yargs__default = /*#__PURE__*/_interopDefaultCompat(yargs);
24
25
 
26
+ class NodeRequest extends webFetch.Request {
27
+ constructor(info, init) {
28
+ super(info, init);
29
+ }
30
+ get headers() {
31
+ return super.headers;
32
+ }
33
+ clone() {
34
+ return new NodeRequest(this);
35
+ }
36
+ }
37
+ class NodeResponse extends webFetch.Response {
38
+ get headers() {
39
+ return super.headers;
40
+ }
41
+ clone() {
42
+ return super.clone();
43
+ }
44
+ }
45
+ const fetch = (info, init) => {
46
+ init = {
47
+ // Disable compression handling so people can return the result of a fetch
48
+ // directly in the loader without messing with the Content-Encoding header.
49
+ compress: false,
50
+ ...init
51
+ };
52
+ return webFetch.fetch(info, init);
53
+ };
54
+
55
+ function installGlobals() {
56
+ global.Headers = webFetch.Headers;
57
+ global.Request = NodeRequest;
58
+ global.Response = NodeResponse;
59
+ global.fetch = fetch;
60
+ global.FormData = webFetch.FormData;
61
+ }
62
+
63
+ installGlobals();
25
64
  yargs__default(helpers.hideBin(process.argv)).scriptName("vite-react-ssg").usage("$0 [args]").command(
26
65
  "build",
27
66
  "Build SSG",
package/dist/node/cli.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import { gray, bold, red, reset, underline } from 'kolorist';
2
2
  import yargs from 'yargs';
3
3
  import { hideBin } from 'yargs/helpers';
4
- import { b as build, d as dev } from '../shared/vite-react-ssg.5291bf32.mjs';
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.b83f6ed3.mjs';
5
6
  import 'node:path';
6
7
  import 'node:module';
7
8
  import 'fs-extra';
@@ -16,6 +17,44 @@ import 'react-router-dom/server.js';
16
17
  import 'node:stream';
17
18
  import 'react-dom/server';
18
19
 
20
+ class NodeRequest extends Request {
21
+ constructor(info, init) {
22
+ super(info, init);
23
+ }
24
+ get headers() {
25
+ return super.headers;
26
+ }
27
+ clone() {
28
+ return new NodeRequest(this);
29
+ }
30
+ }
31
+ class NodeResponse extends Response {
32
+ get headers() {
33
+ return super.headers;
34
+ }
35
+ clone() {
36
+ return super.clone();
37
+ }
38
+ }
39
+ const fetch = (info, init) => {
40
+ init = {
41
+ // Disable compression handling so people can return the result of a fetch
42
+ // directly in the loader without messing with the Content-Encoding header.
43
+ compress: false,
44
+ ...init
45
+ };
46
+ return fetch$1(info, init);
47
+ };
48
+
49
+ function installGlobals() {
50
+ global.Headers = Headers;
51
+ global.Request = NodeRequest;
52
+ global.Response = NodeResponse;
53
+ global.fetch = fetch;
54
+ global.FormData = FormData;
55
+ }
56
+
57
+ installGlobals();
19
58
  yargs(hideBin(process.argv)).scriptName("vite-react-ssg").usage("$0 [args]").command(
20
59
  "build",
21
60
  "Build SSG",
package/dist/node.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const dev = require('./shared/vite-react-ssg.8cbf43cc.cjs');
3
+ const dev = require('./shared/vite-react-ssg.b0fd1823.cjs');
4
4
  require('node:path');
5
5
  require('node:module');
6
6
  require('kolorist');
package/dist/node.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { b as build, d as dev } from './shared/vite-react-ssg.5291bf32.mjs';
1
+ export { b as build, d as dev } from './shared/vite-react-ssg.b83f6ed3.mjs';
2
2
  import 'node:path';
3
3
  import 'node:module';
4
4
  import 'kolorist';
@@ -934,7 +934,7 @@ async function resolveAlias(config, entry) {
934
934
  return result || node_path.join(config.root, entry);
935
935
  }
936
936
  const { version } = JSON.parse(
937
- 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.8cbf43cc.cjs', document.baseURI).href)))).toString()
937
+ 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.b0fd1823.cjs', document.baseURI).href)))).toString()
938
938
  );
939
939
 
940
940
  async function getCritters(outDir, options = {}) {
@@ -1019,9 +1019,8 @@ async function render(routes, request, styleCollector) {
1019
1019
  helmet.script.toString()
1020
1020
  ];
1021
1021
  const styleTag = styleCollector?.toString?.(appHTML) ?? "";
1022
- metaStrings.push(styleTag);
1023
1022
  const metaAttributes = metaStrings.filter(Boolean);
1024
- return { appHTML, htmlAttributes, bodyAttributes, metaAttributes };
1023
+ return { appHTML, htmlAttributes, bodyAttributes, metaAttributes, styleTag };
1025
1024
  }
1026
1025
 
1027
1026
  function renderPreloadLinks(document, modules, ssrManifest) {
@@ -1201,7 +1200,7 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1201
1200
  const prefix = format === "esm" && process.platform === "win32" ? "file://" : "";
1202
1201
  const ext = format === "esm" ? ".mjs" : ".cjs";
1203
1202
  const serverEntry = node_path.join(prefix, ssgOut, node_path.parse(ssrEntry).name + ext);
1204
- 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.8cbf43cc.cjs', document.baseURI).href)));
1203
+ 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.b0fd1823.cjs', document.baseURI).href)));
1205
1204
  const { createRoot, includedRoutes: serverEntryIncludedRoutes } = format === "esm" ? await import(serverEntry) : _require(serverEntry);
1206
1205
  const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes;
1207
1206
  const { routes } = await createRoot(false);
@@ -1229,7 +1228,7 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1229
1228
  url.hash = "";
1230
1229
  url.pathname = route;
1231
1230
  const request = new Request(url.href);
1232
- const { appHTML, bodyAttributes, htmlAttributes, metaAttributes } = await render([...routes2], request, styleCollector);
1231
+ const { appHTML, bodyAttributes, htmlAttributes, metaAttributes, styleTag } = await render([...routes2], request, styleCollector);
1233
1232
  await triggerOnSSRAppRendered?.(route, appHTML, appCtx);
1234
1233
  const renderedHTML = await renderHTML({
1235
1234
  rootContainerId,
@@ -1247,6 +1246,8 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1247
1246
  let transformed = await onPageRendered?.(route, html, appCtx) || html;
1248
1247
  if (critters)
1249
1248
  transformed = await critters.process(transformed);
1249
+ if (styleTag)
1250
+ transformed = transformed.replace("<head>", `<head>${styleTag}`);
1250
1251
  const formatted = await formatHtml(transformed, formatting);
1251
1252
  const relativeRouteFile = `${(route.endsWith("/") ? `${route}index` : route).replace(/^\//g, "")}.html`;
1252
1253
  const filename = dirStyle === "nested" ? node_path.join(route.replace(/^\//g, ""), "index.html") : relativeRouteFile;
@@ -1426,7 +1427,8 @@ async function dev(ssgOptions = {}, viteConfig = {}) {
1426
1427
  const { routes, getStyleCollector } = appCtx;
1427
1428
  const transformedIndexHTML = await onBeforePageRender?.(url, indexHTML, appCtx) || indexHTML;
1428
1429
  const styleCollector = getStyleCollector ? await getStyleCollector() : null;
1429
- const { appHTML, bodyAttributes, htmlAttributes, metaAttributes } = await render([...routes], createFetchRequest(req), styleCollector);
1430
+ const { appHTML, bodyAttributes, htmlAttributes, metaAttributes, styleTag } = await render([...routes], createFetchRequest(req), styleCollector);
1431
+ metaAttributes.push(styleTag);
1430
1432
  const mod = await viteServer.moduleGraph.getModuleByUrl(entry);
1431
1433
  const assetsUrls = /* @__PURE__ */ new Set();
1432
1434
  const collectAssets = async (mod2) => {
@@ -1011,9 +1011,8 @@ async function render(routes, request, styleCollector) {
1011
1011
  helmet.script.toString()
1012
1012
  ];
1013
1013
  const styleTag = styleCollector?.toString?.(appHTML) ?? "";
1014
- metaStrings.push(styleTag);
1015
1014
  const metaAttributes = metaStrings.filter(Boolean);
1016
- return { appHTML, htmlAttributes, bodyAttributes, metaAttributes };
1015
+ return { appHTML, htmlAttributes, bodyAttributes, metaAttributes, styleTag };
1017
1016
  }
1018
1017
 
1019
1018
  function renderPreloadLinks(document, modules, ssrManifest) {
@@ -1221,7 +1220,7 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1221
1220
  url.hash = "";
1222
1221
  url.pathname = route;
1223
1222
  const request = new Request(url.href);
1224
- const { appHTML, bodyAttributes, htmlAttributes, metaAttributes } = await render([...routes2], request, styleCollector);
1223
+ const { appHTML, bodyAttributes, htmlAttributes, metaAttributes, styleTag } = await render([...routes2], request, styleCollector);
1225
1224
  await triggerOnSSRAppRendered?.(route, appHTML, appCtx);
1226
1225
  const renderedHTML = await renderHTML({
1227
1226
  rootContainerId,
@@ -1239,6 +1238,8 @@ async function build(ssgOptions = {}, viteConfig = {}) {
1239
1238
  let transformed = await onPageRendered?.(route, html, appCtx) || html;
1240
1239
  if (critters)
1241
1240
  transformed = await critters.process(transformed);
1241
+ if (styleTag)
1242
+ transformed = transformed.replace("<head>", `<head>${styleTag}`);
1242
1243
  const formatted = await formatHtml(transformed, formatting);
1243
1244
  const relativeRouteFile = `${(route.endsWith("/") ? `${route}index` : route).replace(/^\//g, "")}.html`;
1244
1245
  const filename = dirStyle === "nested" ? join(route.replace(/^\//g, ""), "index.html") : relativeRouteFile;
@@ -1418,7 +1419,8 @@ async function dev(ssgOptions = {}, viteConfig = {}) {
1418
1419
  const { routes, getStyleCollector } = appCtx;
1419
1420
  const transformedIndexHTML = await onBeforePageRender?.(url, indexHTML, appCtx) || indexHTML;
1420
1421
  const styleCollector = getStyleCollector ? await getStyleCollector() : null;
1421
- const { appHTML, bodyAttributes, htmlAttributes, metaAttributes } = await render([...routes], createFetchRequest(req), styleCollector);
1422
+ const { appHTML, bodyAttributes, htmlAttributes, metaAttributes, styleTag } = await render([...routes], createFetchRequest(req), styleCollector);
1423
+ metaAttributes.push(styleTag);
1422
1424
  const mod = await viteServer.moduleGraph.getModuleByUrl(entry);
1423
1425
  const assetsUrls = /* @__PURE__ */ new Set();
1424
1426
  const collectAssets = async (mod2) => {
@@ -1,8 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ const styledComponents = require('styled-components');
4
+
3
5
  async function ssrCollector() {
4
- const { ServerStyleSheet } = await import('styled-components');
5
- const sheet = new ServerStyleSheet();
6
+ const sheet = new styledComponents.ServerStyleSheet();
6
7
  return {
7
8
  collect(app) {
8
9
  return sheet.collectStyles(app);
@@ -15,6 +16,5 @@ async function ssrCollector() {
15
16
  }
16
17
  };
17
18
  }
18
- const styledComponents = undefined.SSR ? ssrCollector : null;
19
19
 
20
- exports.getStyledComponentsCollector = styledComponents;
20
+ module.exports = ssrCollector;
@@ -5,6 +5,5 @@ declare function ssrCollector(): Promise<{
5
5
  toString(): string;
6
6
  cleanup(): void;
7
7
  }>;
8
- declare const _default: typeof ssrCollector | null;
9
8
 
10
- export { _default as getStyledComponentsCollector };
9
+ export { ssrCollector as default };
@@ -1,5 +1,6 @@
1
+ import { ServerStyleSheet } from 'styled-components';
2
+
1
3
  async function ssrCollector() {
2
- const { ServerStyleSheet } = await import('styled-components');
3
4
  const sheet = new ServerStyleSheet();
4
5
  return {
5
6
  collect(app) {
@@ -13,6 +14,5 @@ async function ssrCollector() {
13
14
  }
14
15
  };
15
16
  }
16
- const styledComponents = import.meta.env.SSR ? ssrCollector : null;
17
17
 
18
- export { styledComponents as getStyledComponentsCollector };
18
+ export { ssrCollector as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-react-ssg",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.2.0",
5
5
  "packageManager": "pnpm@8.6.6",
6
6
  "description": "",
7
7
  "author": "Riri <Daydreamerriri@outlook.com>",
@@ -31,10 +31,10 @@
31
31
  "require": "./dist/node.cjs",
32
32
  "import": "./dist/node.mjs"
33
33
  },
34
- "./style-collectors": {
35
- "types": "./dist/style-collectors.d.ts",
36
- "require": "./dist/style-collectors.cjs",
37
- "import": "./dist/style-collectors.mjs"
34
+ "./style-collectors/styled-components": {
35
+ "types": "./dist/style-collectors/styled-components.d.ts",
36
+ "require": "./dist/style-collectors/styled-components.cjs",
37
+ "import": "./dist/style-collectors/styled-components.mjs"
38
38
  }
39
39
  },
40
40
  "main": "./dist/index.mjs",
@@ -45,8 +45,8 @@
45
45
  "node": [
46
46
  "./dist/node.d.ts"
47
47
  ],
48
- "style-collectors": [
49
- "./dist/style-collectors.d.ts"
48
+ "style-collectors/styled-components": [
49
+ "./dist/style-collectors/styled-components.d.ts"
50
50
  ]
51
51
  }
52
52
  },
@@ -87,6 +87,7 @@
87
87
  }
88
88
  },
89
89
  "dependencies": {
90
+ "@remix-run/web-fetch": "^4.3.5",
90
91
  "express": "^4.18.2",
91
92
  "fs-extra": "^11.1.1",
92
93
  "html-minifier": "^4.0.0",