ssr-plugin-react 7.0.6 → 7.0.7

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +305 -450
  2. package/README.md +60 -60
  3. package/cjs/entry/context.d.ts +1 -1
  4. package/cjs/entry/context.d.ts.map +1 -1
  5. package/cjs/entry/context.js +5 -5
  6. package/cjs/entry/context.js.map +1 -1
  7. package/cjs/entry/create.d.ts +1 -1
  8. package/cjs/entry/create.d.ts.map +1 -1
  9. package/cjs/entry/create.js +1 -1
  10. package/cjs/entry/create.js.map +1 -1
  11. package/cjs/entry/react17-client-entry.d.ts.map +1 -1
  12. package/cjs/entry/react17-client-entry.js +6 -7
  13. package/cjs/entry/react17-client-entry.js.map +1 -1
  14. package/cjs/entry/react18-client-entry.d.ts.map +1 -1
  15. package/cjs/entry/react18-client-entry.js +5 -7
  16. package/cjs/entry/react18-client-entry.js.map +1 -1
  17. package/cjs/entry/server-entry.d.ts +2 -2
  18. package/cjs/entry/server-entry.d.ts.map +1 -1
  19. package/cjs/entry/server-entry.js +31 -27
  20. package/cjs/entry/server-entry.js.map +1 -1
  21. package/cjs/index.js +9 -9
  22. package/cjs/index.js.map +1 -1
  23. package/esm/entry/context.d.ts +1 -1
  24. package/esm/entry/context.d.ts.map +1 -1
  25. package/esm/entry/context.js +7 -7
  26. package/esm/entry/context.js.map +1 -1
  27. package/esm/entry/create.d.ts +1 -1
  28. package/esm/entry/create.d.ts.map +1 -1
  29. package/esm/entry/create.js +7 -7
  30. package/esm/entry/create.js.map +1 -1
  31. package/esm/entry/react17-client-entry.d.ts.map +1 -1
  32. package/esm/entry/react17-client-entry.js +13 -14
  33. package/esm/entry/react17-client-entry.js.map +1 -1
  34. package/esm/entry/react18-client-entry.d.ts.map +1 -1
  35. package/esm/entry/react18-client-entry.js +12 -14
  36. package/esm/entry/react18-client-entry.js.map +1 -1
  37. package/esm/entry/server-entry.d.ts +2 -2
  38. package/esm/entry/server-entry.d.ts.map +1 -1
  39. package/esm/entry/server-entry.js +40 -36
  40. package/esm/entry/server-entry.js.map +1 -1
  41. package/esm/index.js +11 -11
  42. package/esm/index.js.map +1 -1
  43. package/package.json +31 -32
  44. package/src/entry/context.tsx +33 -29
  45. package/src/entry/create.ts +26 -26
  46. package/src/entry/react17-client-entry.tsx +66 -66
  47. package/src/entry/react18-client-entry.tsx +72 -72
  48. package/src/entry/server-entry.tsx +191 -181
  49. package/src/index.ts +38 -38
  50. package/tsconfig.cjs.json +2 -4
  51. package/tsconfig.esm.json +2 -4
package/package.json CHANGED
@@ -1,59 +1,58 @@
1
1
  {
2
2
  "name": "ssr-plugin-react",
3
- "version": "7.0.6",
3
+ "version": "7.0.7",
4
4
  "description": "plugin-react for ssr",
5
- "main": "./cjs/index.js",
6
- "module": "./esm/index.js",
7
- "sideEffects": false,
8
- "scripts": {
9
- "build": "concurrently \"tsc -p ./tsconfig.cjs.json \" \" tsc -p ./tsconfig.esm.json\"",
10
- "watch": "concurrently \"tsc -w -p ./tsconfig.cjs.json \" \"tsc -w -p ./tsconfig.esm.json \""
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/zhangyuang/ssr.git"
15
- },
16
5
  "keywords": [
17
6
  "cli",
7
+ "react",
18
8
  "serverless",
19
9
  "ssr",
20
- "react",
21
10
  "vue"
22
11
  ],
23
- "author": "zhangyuang",
24
- "license": "MIT",
12
+ "homepage": "https://github.com/zhangyuang/ssr#readme",
25
13
  "bugs": {
26
14
  "url": "https://github.com/zhangyuang/ssr/issues"
27
15
  },
28
- "engines": {
29
- "node": ">=14.17.0"
16
+ "license": "MIT",
17
+ "author": "zhangyuang",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/zhangyuang/ssr.git"
21
+ },
22
+ "sideEffects": false,
23
+ "main": "./cjs/index.js",
24
+ "module": "./esm/index.js",
25
+ "scripts": {
26
+ "build": "concurrently \"tsc -p ./tsconfig.cjs.json \" \" tsc -p ./tsconfig.esm.json\"",
27
+ "watch": "concurrently \"tsc -w -p ./tsconfig.cjs.json \" \"tsc -w -p ./tsconfig.esm.json \""
30
28
  },
31
- "homepage": "https://github.com/zhangyuang/ssr#readme",
32
29
  "dependencies": {
33
- "ssr-common-utils": "^7.0.0",
34
- "@vitejs/plugin-react-oxc": "^0.3.0",
35
- "@vitejs/plugin-react": "^4.0.0",
30
+ "@pmmmwh/react-refresh-webpack-plugin": "^0.5.5",
31
+ "@vitejs/plugin-react": "^6.0.1",
36
32
  "babel-preset-react-app": "^10.0.0",
33
+ "ssr-common-utils": "^7.0.0",
37
34
  "ssr-hoc-react": "^7.0.0",
38
- "@pmmmwh/react-refresh-webpack-plugin": "^0.5.5",
39
35
  "valtio": "^1.13.2"
40
36
  },
41
- "peerDependencies": {
42
- "react": ">=17.0.1",
43
- "react-dom": ">=17.0.0",
44
- "react-router": "^5.1.8",
45
- "react-router-dom": "^5.0.0"
46
- },
47
37
  "devDependencies": {
48
- "@types/react18": "npm:@types/react@^18.0.0",
49
- "@types/react-dom18": "npm:@types/react-dom@^18.0.0",
50
38
  "@types/react": "^17.0.0",
51
39
  "@types/react-dom": "^17.0.0",
40
+ "@types/react-dom18": "npm:@types/react-dom@^18.0.0",
52
41
  "@types/react-router-dom": "^5.1.3",
42
+ "@types/react18": "npm:@types/react@^18.0.0",
53
43
  "@types/webpack": "^4.41.10",
54
44
  "concurrently": "^5.1.0",
55
45
  "ssr-types": "workspace:*",
56
- "ssr-webpack": "workspace:*",
57
- "ssr-vite": "workspace:*"
46
+ "ssr-vite": "workspace:*",
47
+ "ssr-webpack": "workspace:*"
48
+ },
49
+ "peerDependencies": {
50
+ "react": ">=17.0.1",
51
+ "react-dom": ">=17.0.0",
52
+ "react-router": "^5.1.8",
53
+ "react-router-dom": "^5.0.0"
54
+ },
55
+ "engines": {
56
+ "node": ">=14.17.0"
58
57
  }
59
58
  }
@@ -1,42 +1,46 @@
1
- import { useReducer, createElement } from 'react'
2
- import { IProps, Action, IWindow, ReactRoutesType } from 'ssr-types'
3
- import { Routes } from './create'
1
+ import { useReducer, createElement } from "react";
2
+ import { IProps, Action, IWindow, ReactRoutesType } from "ssr-types";
3
+ import { Routes } from "./create";
4
4
 
5
- const { reducer, state } = Routes as ReactRoutesType
5
+ const { reducer, state } = Routes as ReactRoutesType;
6
6
 
7
- const userState = state ?? {}
8
- const userReducer = reducer ?? function () {}
7
+ const userState = state ?? {};
8
+ const userReducer = reducer ?? function () {};
9
9
 
10
- const isDev = process.env.NODE_ENV !== 'production'
10
+ const isDev = process.env.NODE_ENV !== "production";
11
11
 
12
- declare const window: IWindow
12
+ declare const window: IWindow;
13
13
 
14
14
  function defaultReducer(state: any, action: Action) {
15
- switch (action.type) {
16
- case 'updateContext':
17
- if (isDev) {
18
- console.log('[SSR:updateContext]: dispatch updateContext with action')
19
- console.log(action)
20
- }
21
- return { ...state, ...action.payload }
22
- }
15
+ switch (action.type) {
16
+ case "updateContext":
17
+ if (isDev) {
18
+ console.log("[SSR:updateContext]: dispatch updateContext with action");
19
+ console.log(action);
20
+ }
21
+ return { ...state, ...action.payload };
22
+ }
23
23
  }
24
24
 
25
25
  function combineReducer(state: any, action: any) {
26
- return defaultReducer(state, action) || userReducer(state, action)
26
+ return defaultReducer(state, action) || userReducer(state, action);
27
27
  }
28
28
 
29
29
  export function AppContext(props: IProps) {
30
- const initialState = Object.assign({}, userState ?? {}, __isBrowser__ ? window?.__INITIAL_DATA__ : props.initialState)
31
- const [state, dispatch] = useReducer(combineReducer, initialState)
32
- return createElement(
33
- props.context.Provider,
34
- {
35
- value: {
36
- state,
37
- dispatch
38
- }
39
- },
40
- props.children as any
41
- )
30
+ const initialState = Object.assign(
31
+ {},
32
+ userState ?? {},
33
+ __isBrowser__ ? window?.__INITIAL_DATA__ : props.initialState,
34
+ );
35
+ const [state, dispatch] = useReducer(combineReducer, initialState);
36
+ return createElement(
37
+ props.context.Provider,
38
+ {
39
+ value: {
40
+ state,
41
+ dispatch,
42
+ },
43
+ },
44
+ props.children as any,
45
+ );
42
46
  }
@@ -1,31 +1,31 @@
1
- import * as declareRoutes from '_build/ssr-declare-routes'
2
- import * as ManualRoutes from '_build/ssr-manual-routes'
3
- import { createContext } from 'react'
4
- import { combineRoutes } from 'ssr-common-utils'
5
- import { deepClone } from 'ssr-deepclone'
6
- import { proxy } from 'valtio'
7
- import type { IContext, ReactRoutesType } from 'ssr-types'
1
+ import * as declareRoutes from "_build/ssr-declare-routes";
2
+ import * as ManualRoutes from "_build/ssr-manual-routes";
3
+ import { createContext } from "react";
4
+ import { combineRoutes } from "ssr-common-utils";
5
+ import { deepClone } from "ssr-deepclone";
6
+ import { proxy } from "valtio";
7
+ import type { IContext, ReactRoutesType } from "ssr-types";
8
8
 
9
- export const Routes = combineRoutes(declareRoutes, ManualRoutes) as ReactRoutesType
9
+ export const Routes = combineRoutes(declareRoutes, ManualRoutes) as ReactRoutesType;
10
10
  export const ssrCreateContext = () => {
11
- const STORE_CONTEXT = createContext<IContext>({
12
- state: {}
13
- })
14
- if (__isBrowser__) {
15
- window.STORE_CONTEXT = STORE_CONTEXT
16
- }
17
- return STORE_CONTEXT
18
- }
11
+ const STORE_CONTEXT = createContext<IContext>({
12
+ state: {},
13
+ });
14
+ if (__isBrowser__) {
15
+ window.STORE_CONTEXT = STORE_CONTEXT;
16
+ }
17
+ return STORE_CONTEXT;
18
+ };
19
19
 
20
20
  export function createStore() {
21
- const { store } = Routes
22
- const storeInstance = __isBrowser__ ? store : proxy(deepClone(store))
23
- if (__isBrowser__) {
24
- for (const key in storeInstance) {
25
- if (__isBrowser__ && window.__VALTIO_DATA__?.[key]) {
26
- Object.assign(storeInstance[key], window.__VALTIO_DATA__[key])
27
- }
28
- }
29
- }
30
- return storeInstance
21
+ const { store } = Routes;
22
+ const storeInstance = __isBrowser__ ? store : proxy(deepClone(store));
23
+ if (__isBrowser__) {
24
+ for (const key in storeInstance) {
25
+ if (__isBrowser__ && window.__VALTIO_DATA__?.[key]) {
26
+ Object.assign(storeInstance[key], window.__VALTIO_DATA__[key]);
27
+ }
28
+ }
29
+ }
30
+ return storeInstance;
31
31
  }
@@ -1,73 +1,73 @@
1
- import type { LayoutProps } from 'ssr-types'
2
- import ReactDOM from 'react-dom'
3
- import { createElement } from 'react'
4
- import { BrowserRouter, Route, Switch } from 'react-router-dom'
5
- import { preloadComponent, isMicro, setStoreContext, setStore } from 'ssr-common-utils'
6
- import { wrapComponent } from 'ssr-hoc-react'
7
- import { ssrCreateContext, Routes, createStore } from './create'
8
- import { AppContext } from './context'
1
+ import type { LayoutProps } from "ssr-types";
2
+ import ReactDOM from "react-dom";
3
+ import { createElement } from "react";
4
+ import { BrowserRouter, Route, Switch } from "react-router-dom";
5
+ import { preloadComponent, isMicro, setStoreContext, setStore } from "ssr-common-utils";
6
+ import { wrapComponent } from "ssr-hoc-react";
7
+ import { ssrCreateContext, Routes, createStore } from "./create";
8
+ import { AppContext } from "./context";
9
9
 
10
- const { FeRoutes, layoutFetch, App } = Routes
10
+ const { FeRoutes, layoutFetch, App } = Routes;
11
11
 
12
12
  const clientRender = async (): Promise<void> => {
13
- const IApp =
14
- App ??
15
- function (props: LayoutProps) {
16
- return props.children!
17
- }
18
- const context = ssrCreateContext() as any
19
- setStoreContext(context)
20
- const store = createStore()
21
- setStore(store ?? {})
22
- const baseName = isMicro() ? window.clientPrefix : window.prefix
23
- const routes = await preloadComponent(FeRoutes, baseName)
24
- const container = document.querySelector(window.ssrDevInfo.rootId ?? '#app')!
25
- const ele = createElement(
26
- //@ts-ignore
27
- BrowserRouter,
28
- {
29
- basename: baseName
30
- },
31
- createElement(AppContext, {
32
- context,
33
- children: createElement(
34
- Switch,
35
- null,
36
- createElement(
37
- IApp as any,
38
- null,
39
- createElement(
40
- Switch,
41
- null,
42
- routes.map((item) => {
43
- const { fetch, component, path } = item
44
- component.fetch = fetch
45
- component.layoutFetch = layoutFetch
46
- const WrappedComponent = wrapComponent(component)
47
- return createElement(Route, {
48
- exact: true,
49
- key: path,
50
- path: path,
51
- render: (props) =>
52
- createElement(WrappedComponent, {
53
- ...props,
54
- key: props.history.location.key
55
- })
56
- })
57
- })
58
- )
59
- )
60
- )
61
- })
62
- )
13
+ const IApp =
14
+ App ??
15
+ function (props: LayoutProps) {
16
+ return props.children!;
17
+ };
18
+ const context = ssrCreateContext() as any;
19
+ setStoreContext(context);
20
+ const store = createStore();
21
+ setStore(store ?? {});
22
+ const baseName = isMicro() ? window.clientPrefix : window.prefix;
23
+ const routes = await preloadComponent(FeRoutes, baseName);
24
+ const container = document.querySelector(window.ssrDevInfo.rootId ?? "#app")!;
25
+ const ele = createElement(
26
+ //@ts-ignore
27
+ BrowserRouter,
28
+ {
29
+ basename: baseName,
30
+ },
31
+ createElement(AppContext, {
32
+ context,
33
+ children: createElement(
34
+ Switch,
35
+ null,
36
+ createElement(
37
+ IApp as any,
38
+ null,
39
+ createElement(
40
+ Switch,
41
+ null,
42
+ routes.map((item) => {
43
+ const { fetch, component, path } = item;
44
+ component.fetch = fetch;
45
+ component.layoutFetch = layoutFetch;
46
+ const WrappedComponent = wrapComponent(component);
47
+ return createElement(Route, {
48
+ exact: true,
49
+ key: path,
50
+ path: path,
51
+ render: (props) =>
52
+ createElement(WrappedComponent, {
53
+ ...props,
54
+ key: props.history.location.key,
55
+ }),
56
+ });
57
+ }),
58
+ ),
59
+ ),
60
+ ),
61
+ }),
62
+ );
63
63
 
64
- ReactDOM[window.__USE_SSR__ ? 'hydrate' : 'render'](ele, container)
64
+ ReactDOM[window.__USE_SSR__ ? "hydrate" : "render"](ele, container);
65
65
 
66
- if (!window.__USE_VITE__) {
67
- ;(module as any)?.hot?.accept?.()
68
- }
69
- }
66
+ if (!window.__USE_VITE__) {
67
+ (module as any)?.hot?.accept?.();
68
+ }
69
+ };
70
70
 
71
- clientRender()
71
+ clientRender();
72
72
 
73
- export { clientRender }
73
+ export { clientRender };
@@ -1,78 +1,78 @@
1
- import type { hydrateRoot as HydrateRoot, createRoot as CreateRoot } from 'react-dom18/client'
2
- import type { LayoutProps } from 'ssr-types'
1
+ import type { hydrateRoot as HydrateRoot, createRoot as CreateRoot } from "react-dom18/client";
2
+ import type { LayoutProps } from "ssr-types";
3
3
 
4
4
  //@ts-ignore
5
- import { hydrateRoot, createRoot } from 'react-dom/client'
6
- import { createElement } from 'react'
7
- import { BrowserRouter, Route, Switch } from 'react-router-dom'
8
- import { preloadComponent, isMicro, setStoreContext, setStore } from 'ssr-common-utils'
9
- import { wrapComponent } from 'ssr-hoc-react'
10
- import { ssrCreateContext, Routes, createStore } from './create'
11
- import { AppContext } from './context'
12
- const { FeRoutes, layoutFetch, App } = Routes
5
+ import { hydrateRoot, createRoot } from "react-dom/client";
6
+ import { createElement } from "react";
7
+ import { BrowserRouter, Route, Switch } from "react-router-dom";
8
+ import { preloadComponent, isMicro, setStoreContext, setStore } from "ssr-common-utils";
9
+ import { wrapComponent } from "ssr-hoc-react";
10
+ import { ssrCreateContext, Routes, createStore } from "./create";
11
+ import { AppContext } from "./context";
12
+ const { FeRoutes, layoutFetch, App } = Routes;
13
13
  const clientRender = async (): Promise<void> => {
14
- const IApp =
15
- App ??
16
- function (props: LayoutProps) {
17
- return props.children!
18
- }
19
- const context = ssrCreateContext() as any
20
- setStoreContext(context)
21
- const store = createStore()
22
- setStore(store ?? {})
23
- const baseName = isMicro() ? window.clientPrefix : window.prefix
24
- const routes = await preloadComponent(FeRoutes, baseName)
25
- const container = document.querySelector(window.ssrDevInfo.rootId ?? '#app')!
26
- const ele = createElement(
27
- //@ts-ignore
28
- BrowserRouter,
29
- {
30
- basename: baseName
31
- },
32
- createElement(AppContext, {
33
- context,
34
- children: createElement(
35
- Switch,
36
- null,
37
- createElement(
38
- IApp as any,
39
- null,
40
- createElement(
41
- Switch,
42
- null,
43
- routes.map((item) => {
44
- const { fetch, component, path } = item
45
- component.fetch = fetch
46
- component.layoutFetch = layoutFetch
47
- const WrappedComponent = wrapComponent(component)
48
- return createElement(Route, {
49
- exact: true,
50
- key: path,
51
- path: path,
52
- render: (props) =>
53
- createElement(WrappedComponent, {
54
- ...props,
55
- key: props.history.location.key
56
- })
57
- })
58
- })
59
- )
60
- )
61
- )
62
- })
63
- )
64
- if (window.__USE_SSR__) {
65
- ;(hydrateRoot as typeof HydrateRoot)(container, ele)
66
- } else {
67
- const root = (createRoot as typeof CreateRoot)(container)
68
- root.render(ele)
69
- }
14
+ const IApp =
15
+ App ??
16
+ function (props: LayoutProps) {
17
+ return props.children!;
18
+ };
19
+ const context = ssrCreateContext() as any;
20
+ setStoreContext(context);
21
+ const store = createStore();
22
+ setStore(store ?? {});
23
+ const baseName = isMicro() ? window.clientPrefix : window.prefix;
24
+ const routes = await preloadComponent(FeRoutes, baseName);
25
+ const container = document.querySelector(window.ssrDevInfo.rootId ?? "#app")!;
26
+ const ele = createElement(
27
+ //@ts-ignore
28
+ BrowserRouter,
29
+ {
30
+ basename: baseName,
31
+ },
32
+ createElement(AppContext, {
33
+ context,
34
+ children: createElement(
35
+ Switch,
36
+ null,
37
+ createElement(
38
+ IApp as any,
39
+ null,
40
+ createElement(
41
+ Switch,
42
+ null,
43
+ routes.map((item) => {
44
+ const { fetch, component, path } = item;
45
+ component.fetch = fetch;
46
+ component.layoutFetch = layoutFetch;
47
+ const WrappedComponent = wrapComponent(component);
48
+ return createElement(Route, {
49
+ exact: true,
50
+ key: path,
51
+ path: path,
52
+ render: (props) =>
53
+ createElement(WrappedComponent, {
54
+ ...props,
55
+ key: props.history.location.key,
56
+ }),
57
+ });
58
+ }),
59
+ ),
60
+ ),
61
+ ),
62
+ }),
63
+ );
64
+ if (window.__USE_SSR__) {
65
+ (hydrateRoot as typeof HydrateRoot)(container, ele);
66
+ } else {
67
+ const root = (createRoot as typeof CreateRoot)(container);
68
+ root.render(ele);
69
+ }
70
70
 
71
- if (!window.__USE_VITE__) {
72
- ;(module as any)?.hot?.accept?.()
73
- }
74
- }
71
+ if (!window.__USE_VITE__) {
72
+ (module as any)?.hot?.accept?.();
73
+ }
74
+ };
75
75
 
76
- clientRender()
76
+ clientRender();
77
77
 
78
- export { clientRender }
78
+ export { clientRender };