ssr-plugin-react 6.2.84 → 7.0.1-beta.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +0 -0
  3. package/cjs/entry/context.d.ts +0 -1
  4. package/cjs/entry/context.d.ts.map +1 -1
  5. package/cjs/entry/context.js +1 -2
  6. package/cjs/entry/context.js.map +1 -1
  7. package/cjs/entry/create.d.ts +0 -1
  8. package/cjs/entry/create.d.ts.map +1 -1
  9. package/cjs/entry/create.js +6 -6
  10. package/cjs/entry/create.js.map +1 -1
  11. package/cjs/entry/{client-entry.d.ts → react17-client-entry.d.ts} +1 -2
  12. package/cjs/entry/react17-client-entry.d.ts.map +1 -0
  13. package/cjs/entry/{client-entry.js → react17-client-entry.js} +13 -8
  14. package/cjs/entry/react17-client-entry.js.map +1 -0
  15. package/{esm/entry/client-entry.d.ts → cjs/entry/react18-client-entry.d.ts} +1 -2
  16. package/cjs/entry/react18-client-entry.d.ts.map +1 -0
  17. package/cjs/entry/react18-client-entry.js +62 -0
  18. package/cjs/entry/react18-client-entry.js.map +1 -0
  19. package/cjs/entry/server-entry.d.ts +0 -1
  20. package/cjs/entry/server-entry.d.ts.map +1 -1
  21. package/cjs/entry/server-entry.js +44 -16
  22. package/cjs/entry/server-entry.js.map +1 -1
  23. package/cjs/index.d.ts +0 -1
  24. package/cjs/index.d.ts.map +1 -1
  25. package/cjs/index.js +16 -36
  26. package/cjs/index.js.map +1 -1
  27. package/esm/entry/context.d.ts +0 -1
  28. package/esm/entry/context.d.ts.map +1 -1
  29. package/esm/entry/context.js.map +1 -1
  30. package/esm/entry/create.d.ts +0 -1
  31. package/esm/entry/create.d.ts.map +1 -1
  32. package/esm/entry/create.js +4 -4
  33. package/esm/entry/create.js.map +1 -1
  34. package/esm/entry/react17-client-entry.d.ts +3 -0
  35. package/esm/entry/react17-client-entry.d.ts.map +1 -0
  36. package/esm/entry/{client-entry.js → react17-client-entry.js} +13 -8
  37. package/esm/entry/react17-client-entry.js.map +1 -0
  38. package/esm/entry/react18-client-entry.d.ts +3 -0
  39. package/esm/entry/react18-client-entry.d.ts.map +1 -0
  40. package/esm/entry/react18-client-entry.js +59 -0
  41. package/esm/entry/react18-client-entry.js.map +1 -0
  42. package/esm/entry/server-entry.d.ts +0 -1
  43. package/esm/entry/server-entry.d.ts.map +1 -1
  44. package/esm/entry/server-entry.js +45 -17
  45. package/esm/entry/server-entry.js.map +1 -1
  46. package/esm/index.d.ts +0 -1
  47. package/esm/index.d.ts.map +1 -1
  48. package/esm/index.js +16 -21
  49. package/esm/index.js.map +1 -1
  50. package/package.json +27 -55
  51. package/src/entry/context.tsx +22 -19
  52. package/src/entry/create.ts +19 -20
  53. package/src/entry/react17-client-entry.tsx +73 -0
  54. package/src/entry/react18-client-entry.tsx +80 -0
  55. package/src/entry/server-entry.tsx +132 -84
  56. package/src/index.ts +40 -44
  57. package/.turbo/turbo-build.log +0 -7
  58. package/cjs/config/base.d.ts +0 -4
  59. package/cjs/config/base.d.ts.map +0 -1
  60. package/cjs/config/base.js +0 -56
  61. package/cjs/config/base.js.map +0 -1
  62. package/cjs/config/client.d.ts +0 -5
  63. package/cjs/config/client.d.ts.map +0 -1
  64. package/cjs/config/client.js +0 -57
  65. package/cjs/config/client.js.map +0 -1
  66. package/cjs/config/index.d.ts +0 -3
  67. package/cjs/config/index.d.ts.map +0 -1
  68. package/cjs/config/index.js +0 -19
  69. package/cjs/config/index.js.map +0 -1
  70. package/cjs/config/server.d.ts +0 -6
  71. package/cjs/config/server.d.ts.map +0 -1
  72. package/cjs/config/server.js +0 -42
  73. package/cjs/config/server.js.map +0 -1
  74. package/cjs/entry/client-entry.d.ts.map +0 -1
  75. package/cjs/entry/client-entry.js.map +0 -1
  76. package/cjs/tools/vite.d.ts +0 -9
  77. package/cjs/tools/vite.d.ts.map +0 -1
  78. package/cjs/tools/vite.js +0 -151
  79. package/cjs/tools/vite.js.map +0 -1
  80. package/cjs/tools/webpack.d.ts +0 -3
  81. package/cjs/tools/webpack.d.ts.map +0 -1
  82. package/cjs/tools/webpack.js +0 -23
  83. package/cjs/tools/webpack.js.map +0 -1
  84. package/esm/config/base.d.ts +0 -4
  85. package/esm/config/base.d.ts.map +0 -1
  86. package/esm/config/base.js +0 -53
  87. package/esm/config/base.js.map +0 -1
  88. package/esm/config/client.d.ts +0 -5
  89. package/esm/config/client.d.ts.map +0 -1
  90. package/esm/config/client.js +0 -54
  91. package/esm/config/client.js.map +0 -1
  92. package/esm/config/index.d.ts +0 -3
  93. package/esm/config/index.d.ts.map +0 -1
  94. package/esm/config/index.js +0 -3
  95. package/esm/config/index.js.map +0 -1
  96. package/esm/config/server.d.ts +0 -6
  97. package/esm/config/server.d.ts.map +0 -1
  98. package/esm/config/server.js +0 -39
  99. package/esm/config/server.js.map +0 -1
  100. package/esm/entry/client-entry.d.ts.map +0 -1
  101. package/esm/entry/client-entry.js.map +0 -1
  102. package/esm/tools/vite.d.ts +0 -9
  103. package/esm/tools/vite.d.ts.map +0 -1
  104. package/esm/tools/vite.js +0 -143
  105. package/esm/tools/vite.js.map +0 -1
  106. package/esm/tools/webpack.d.ts +0 -3
  107. package/esm/tools/webpack.d.ts.map +0 -1
  108. package/esm/tools/webpack.js +0 -18
  109. package/esm/tools/webpack.js.map +0 -1
  110. package/src/config/base.ts +0 -65
  111. package/src/config/client.ts +0 -65
  112. package/src/config/index.ts +0 -2
  113. package/src/config/server.ts +0 -48
  114. package/src/entry/client-entry.tsx +0 -55
  115. package/src/tools/vite.ts +0 -153
  116. package/src/tools/webpack.ts +0 -20
@@ -0,0 +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'
9
+
10
+ const { FeRoutes, layoutFetch, App } = Routes
11
+
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(window.__VALTIO_DATA__)
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
+
64
+ ReactDOM[window.__USE_SSR__ ? 'hydrate' : 'render'](ele, container)
65
+
66
+ if (!window.__USE_VITE__) {
67
+ ;(module as any)?.hot?.accept?.()
68
+ }
69
+ }
70
+
71
+ clientRender()
72
+
73
+ export { clientRender }
@@ -0,0 +1,80 @@
1
+ import type { hydrateRoot as HydrateRoot, createRoot as CreateRoot } from 'react-dom18/client'
2
+ import type { LayoutProps } from 'ssr-types'
3
+
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
+
13
+ const { FeRoutes, layoutFetch, App } = Routes
14
+
15
+ const clientRender = async (): Promise<void> => {
16
+ const IApp =
17
+ App ??
18
+ function (props: LayoutProps) {
19
+ return props.children!
20
+ }
21
+ const context = ssrCreateContext() as any
22
+ setStoreContext(context)
23
+ const store = createStore(window.__VALTIO_DATA__)
24
+ setStore(store ?? {})
25
+ const baseName = isMicro() ? window.clientPrefix : window.prefix
26
+ const routes = await preloadComponent(FeRoutes, baseName)
27
+ const container = document.querySelector(window.ssrDevInfo.rootId ?? '#app')!
28
+ const ele = createElement(
29
+ //@ts-ignore
30
+ BrowserRouter,
31
+ {
32
+ basename: baseName
33
+ },
34
+ createElement(AppContext, {
35
+ context,
36
+ children: createElement(
37
+ Switch,
38
+ null,
39
+ createElement(
40
+ IApp as any,
41
+ null,
42
+ createElement(
43
+ Switch,
44
+ null,
45
+ routes.map((item) => {
46
+ const { fetch, component, path } = item
47
+ component.fetch = fetch
48
+ component.layoutFetch = layoutFetch
49
+ const WrappedComponent = wrapComponent(component)
50
+ return createElement(Route, {
51
+ exact: true,
52
+ key: path,
53
+ path: path,
54
+ render: (props) =>
55
+ createElement(WrappedComponent, {
56
+ ...props,
57
+ key: props.history.location.key
58
+ })
59
+ })
60
+ })
61
+ )
62
+ )
63
+ )
64
+ })
65
+ )
66
+ if (window.__USE_SSR__) {
67
+ ;(hydrateRoot as typeof HydrateRoot)(container, ele)
68
+ } else {
69
+ const root = (createRoot as typeof CreateRoot)(container)
70
+ root.render(ele)
71
+ }
72
+
73
+ if (!window.__USE_VITE__) {
74
+ ;(module as any)?.hot?.accept?.()
75
+ }
76
+ }
77
+
78
+ clientRender()
79
+
80
+ export { clientRender }
@@ -1,9 +1,10 @@
1
+ import type { renderToPipeableStream } from 'react-dom18/server'
1
2
  import { PassThrough } from 'stream'
2
3
  import * as React from 'react'
3
4
  import { createElement } from 'react'
5
+ import * as ReactDOMServer from 'react-dom/server'
4
6
  import { StaticRouter } from 'react-router-dom'
5
- import { renderToString, renderToNodeStream } from 'react-dom/server'
6
- import { findRoute, getManifest, logGreen, normalizePath, getAsyncCssChunk, getAsyncJsChunk, splitPageInfo, reactRefreshFragment, localStorageWrapper, checkRoute, useStore } from 'ssr-common-utils'
7
+ import { findRoute, getManifest, logGreen, normalizePath, getAsyncCssChunk, getAsyncJsChunk, splitPageInfo, reactRefreshFragment, localStorageWrapper, checkRoute, useStore, isReact18, getClientEntry } from 'ssr-common-utils'
7
8
  import { ISSRContext, IConfig, ReactESMPreloadFeRouteItem, DynamicFC, StaticFC } from 'ssr-types'
8
9
  import { serialize } from 'ssr-serialize-javascript'
9
10
  import { AppContext } from './context'
@@ -11,97 +12,144 @@ import { Routes, ssrCreateContext, createStore } from './create'
11
12
 
12
13
  const { FeRoutes, layoutFetch, state, Layout } = Routes
13
14
 
15
+ type ReactDOMServerType = typeof ReactDOMServer & {
16
+ renderToPipeableStream: typeof renderToPipeableStream
17
+ }
14
18
  const serverRender = async (ctx: ISSRContext, config: IConfig) => {
15
- const context = ssrCreateContext()
16
- const { mode, parallelFetch, prefix, isVite, isDev, clientPrefix, stream, rootId, hashRouter, streamHighWaterMark } = config
17
- const rawPath = ctx.request.path ?? ctx.request.url
18
- const path = normalizePath(rawPath, prefix)
19
- const routeItem = findRoute<ReactESMPreloadFeRouteItem>(FeRoutes, path)
20
- checkRoute({ routeItem, path })
21
- const { fetch, webpackChunkName, component } = routeItem
19
+ const context = ssrCreateContext()
20
+ const { mode, parallelFetch, prefix, isVite, isDev, clientPrefix, onReady, onError, stream, rootId, hashRouter, streamHighWaterMark } = config
21
+ const rawPath = ctx.request.path ?? ctx.request.url
22
+ const path = normalizePath(rawPath, prefix)
23
+ const routeItem = findRoute<ReactESMPreloadFeRouteItem>(FeRoutes, path)
24
+ checkRoute({ routeItem, path })
25
+ const { fetch, webpackChunkName, component } = routeItem
22
26
 
23
- const fn = async () => {
24
- const dynamicCssOrder = await getAsyncCssChunk(ctx, webpackChunkName, config)
25
- const dynamicJsOrder = await getAsyncJsChunk(ctx, webpackChunkName, config)
26
- const manifest = await getManifest(config)
27
+ const fn = async () => {
28
+ const dynamicCssOrder = await getAsyncCssChunk(ctx, webpackChunkName, config)
29
+ const dynamicJsOrder = await getAsyncJsChunk(ctx, webpackChunkName, config)
30
+ const manifest = await getManifest(config)
27
31
 
28
- const injectCss = ((isVite && isDev) ? [
29
- <script src="/@vite/client" type="module" key="vite-client" />,
30
- <script key="vite-react-refresh" type="module" dangerouslySetInnerHTML={{
31
- __html: reactRefreshFragment
32
- }} />
33
- ] : dynamicCssOrder.map(css => manifest[css]).filter(Boolean).map(css => <link rel='stylesheet' key={css} href={css} />))
34
- .concat((isVite && isDev) ? [] : dynamicJsOrder.map(js => manifest[js]).filter(Boolean).map(js =>
35
- <link href={js} as="script" rel={isVite ? 'modulepreload' : 'preload'} key={js} />
36
- ))
32
+ const injectCss = (
33
+ isVite && isDev
34
+ ? [
35
+ <script src="/@vite/client" type="module" key="vite-client" />,
36
+ <script
37
+ key="vite-react-refresh"
38
+ type="module"
39
+ dangerouslySetInnerHTML={{
40
+ __html: reactRefreshFragment
41
+ }}
42
+ />
43
+ ]
44
+ : dynamicCssOrder
45
+ .map((css) => manifest[css])
46
+ .filter(Boolean)
47
+ .map((css) => <link rel="stylesheet" key={css} href={css} />)
48
+ ).concat(
49
+ isVite && isDev
50
+ ? []
51
+ : dynamicJsOrder
52
+ .map((js) => manifest[js])
53
+ .filter(Boolean)
54
+ .map((js) => <link href={js} as="script" rel={isVite ? 'modulepreload' : 'preload'} key={js} />)
55
+ )
37
56
 
38
- const injectScript = [
39
- ...(isVite ? [<script key="viteWindowInit" dangerouslySetInnerHTML={{
40
- __html: 'window.__USE_VITE__=true'
41
- }} />] : []),
42
- ...((isVite && isDev) ? [<script type="module" src='/node_modules/ssr-plugin-react/esm/entry/client-entry.js' key="vite-react-entry" />] : []),
43
- ...dynamicJsOrder.map(js => manifest[js]).filter(Boolean).map(item => <script key={item} src={item} type={isVite ? 'module' : 'text/javascript'} />)
44
- ]
45
- const staticList = {
46
- injectCss,
47
- injectScript
48
- }
57
+ const injectScript = [
58
+ ...(isVite
59
+ ? [
60
+ <script
61
+ key="viteWindowInit"
62
+ dangerouslySetInnerHTML={{
63
+ __html: 'window.__USE_VITE__=true'
64
+ }}
65
+ />
66
+ ]
67
+ : []),
68
+ ...(isVite && isDev ? [<script type="module" src={`/node_modules/ssr-plugin-react/esm/entry/${getClientEntry()}.js`} key="vite-react-entry" />] : []),
69
+ ...dynamicJsOrder
70
+ .map((js) => manifest[js])
71
+ .filter(Boolean)
72
+ .map((item) => <script key={item} src={item} type={isVite ? 'module' : 'text/javascript'} />)
73
+ ]
74
+ const staticList = {
75
+ injectCss,
76
+ injectScript
77
+ }
49
78
 
50
- const isCsr = !!(mode === 'csr' || ctx.request.query?.csr)
51
- const Component = isCsr ? React.Fragment : (component.name === 'dynamicComponent' ? (await (component as DynamicFC)()).default : component as StaticFC)
79
+ const isCsr = !!(mode === 'csr' || ctx.request.query?.csr)
80
+ const Component = isCsr ? React.Fragment : component.name === 'dynamicComponent' ? (await (component as DynamicFC)()).default : (component as StaticFC)
52
81
 
53
- if (isCsr) {
54
- logGreen(`Current path ${path} use csr render mode`)
55
- }
82
+ if (isCsr) {
83
+ logGreen(`Current path ${path} use csr render mode`)
84
+ }
56
85
 
57
- let [layoutFetchData, fetchData] = [{}, {}]
86
+ let [layoutFetchData, fetchData] = [{}, {}]
58
87
 
59
- if (!isCsr) {
60
- const currentFetch = fetch ? (await fetch()).default : null
61
- const lF = layoutFetch ? layoutFetch({ ctx }) : Promise.resolve({})
62
- const CF = currentFetch ? currentFetch({ ctx }) : Promise.resolve({});
63
- [layoutFetchData, fetchData] = parallelFetch ? await Promise.all([lF, CF]) : [await lF, await CF]
64
- }
88
+ if (!isCsr) {
89
+ const currentFetch = fetch ? (await fetch()).default : null
90
+ const lF = layoutFetch ? layoutFetch({ ctx }) : Promise.resolve({})
91
+ const CF = currentFetch ? currentFetch({ ctx }) : Promise.resolve({})
92
+ ;[layoutFetchData, fetchData] = parallelFetch ? await Promise.all([lF, CF]) : [await lF, await CF]
93
+ }
65
94
 
66
- const combineData = isCsr ? null : Object.assign({}, state ?? {}, layoutFetchData ?? {}, fetchData ?? {})
67
- const ssrDevInfo = { manifest: isDev ? manifest : '', rootId }
68
- const innerHTML = splitPageInfo({
69
- 'window.__USE_SSR__': !isCsr,
70
- 'window.__INITIAL_DATA__': isCsr ? {} : serialize(combineData),
71
- 'window.__USE_VITE__': isVite,
72
- 'window.prefix': `"${prefix}"`,
73
- 'window.clientPrefix': `"${clientPrefix ?? ''}"`,
74
- 'window.ssrDevInfo': JSON.stringify(ssrDevInfo),
75
- 'window.hashRouter': Boolean(hashRouter),
76
- 'window.__VALTIO_DATA__': isCsr ? {} : serialize(useStore())
77
- })
78
- const injectState = <script dangerouslySetInnerHTML={{ __html: innerHTML }} />
79
- // with jsx type error, use createElement here
80
- const ele = createElement(StaticRouter, {
81
- location: ctx.request.url,
82
- basename: prefix === '/' ? undefined : prefix
83
- }, createElement(AppContext, {
84
- context: context as any,
85
- initialState: combineData,
86
- children: createElement(Layout, {
87
- ctx: ctx,
88
- config: config,
89
- staticList: staticList,
90
- injectState: injectState
91
- }, createElement(Component, null))
92
- }))
93
- // for ctx.body will loose asynclocalstorage context, consume stream in advance like vue2/3
94
- return stream ? renderToNodeStream(ele).pipe(new PassThrough({ highWaterMark: streamHighWaterMark })) : renderToString(ele)
95
- }
95
+ const combineData = isCsr ? null : Object.assign({}, state ?? {}, layoutFetchData ?? {}, fetchData ?? {})
96
+ const ssrDevInfo = { manifest: isDev ? manifest : '', rootId }
97
+ const innerHTML = splitPageInfo({
98
+ 'window.__USE_SSR__': !isCsr,
99
+ 'window.__INITIAL_DATA__': isCsr ? {} : serialize(combineData),
100
+ 'window.__USE_VITE__': isVite,
101
+ 'window.prefix': `"${prefix}"`,
102
+ 'window.clientPrefix': `"${clientPrefix ?? ''}"`,
103
+ 'window.ssrDevInfo': JSON.stringify(ssrDevInfo),
104
+ 'window.hashRouter': Boolean(hashRouter),
105
+ 'window.__VALTIO_DATA__': isCsr ? {} : serialize(useStore())
106
+ })
107
+ const injectState = <script dangerouslySetInnerHTML={{ __html: innerHTML }} />
108
+ // with jsx type error, use createElement here
109
+ const ele = createElement(
110
+ StaticRouter,
111
+ {
112
+ location: ctx.request.url,
113
+ basename: prefix === '/' ? undefined : prefix
114
+ },
115
+ createElement(AppContext, {
116
+ context: context as any,
117
+ initialState: combineData,
118
+ children: createElement(
119
+ Layout,
120
+ {
121
+ ctx: ctx,
122
+ config: config,
123
+ staticList: staticList,
124
+ injectState: injectState
125
+ },
126
+ createElement(Component, null)
127
+ )
128
+ })
129
+ )
130
+ // for ctx.body will loose asynclocalstorage context, consume stream in advance like vue2/3
131
+ if (isReact18()) {
132
+ return stream
133
+ ? (ReactDOMServer as ReactDOMServerType)
134
+ .renderToPipeableStream(ele, {
135
+ onAllReady: onReady,
136
+ onError: onError as any
137
+ })
138
+ .pipe(new PassThrough({ highWaterMark: streamHighWaterMark }))
139
+ : ReactDOMServer.renderToString(ele)
140
+ } else {
141
+ return stream ? ReactDOMServer.renderToNodeStream(ele).pipe(new PassThrough({ highWaterMark: streamHighWaterMark })) : ReactDOMServer.renderToString(ele)
142
+ }
143
+ }
96
144
 
97
- return await localStorageWrapper.run({
98
- context: context as any,
99
- ctx,
100
- store: createStore()
101
- }, fn)
145
+ return await localStorageWrapper.run(
146
+ {
147
+ context: context as any,
148
+ ctx,
149
+ store: createStore()
150
+ },
151
+ fn
152
+ )
102
153
  }
103
154
 
104
- export {
105
- serverRender,
106
- Routes
107
- }
155
+ export { serverRender, Routes }
package/src/index.ts CHANGED
@@ -1,49 +1,45 @@
1
- import { loadConfig } from 'ssr-common-utils'
1
+ import { resolve } from 'path'
2
+ import { loadConfig, getCwd } from 'ssr-common-utils'
2
3
 
3
4
  const { isVite, optimize } = loadConfig()
4
5
  const spinner = require('ora')('Building')
5
6
 
6
- export function clientPlugin () {
7
- return {
8
- name: 'plugin-react',
9
- start: async () => {
10
- if (isVite) {
11
- const { viteStart } = await import('./tools/vite')
12
- await viteStart()
13
- } else {
14
- if (optimize) {
15
- spinner.start()
16
- const { viteBuildClient } = await import('./tools/vite')
17
- await viteBuildClient()
18
- process.env.NODE_ENV = 'development'
19
- spinner.stop()
20
- const { webpackStart } = await import('./tools/webpack')
21
- await webpackStart()
22
- } else {
23
- const { webpackStart } = await import('./tools/webpack')
24
- await webpackStart()
25
- }
26
- }
27
- },
28
- build: async () => {
29
- if (isVite) {
30
- const { viteBuild } = await import('./tools/vite')
31
- await viteBuild()
32
- } else {
33
- if (optimize) {
34
- spinner.start()
35
- const { viteBuildClient } = await import('./tools/vite')
36
- await viteBuildClient()
37
- spinner.stop()
38
- const { webpackBuild } = await import('./tools/webpack')
39
- await webpackBuild()
40
- } else {
41
- const { webpackBuild } = await import('./tools/webpack')
42
- await webpackBuild()
43
- }
44
- }
45
- }
46
- }
7
+ export function clientPlugin() {
8
+ const cwd = getCwd()
9
+ const webpackPath = resolve(cwd, './node_modules/ssr-webpack')
10
+ const vitePath = resolve(cwd, './node_modules/ssr-vite')
11
+ return {
12
+ name: 'plugin-react',
13
+ start: async () => {
14
+ if (isVite) {
15
+ const { start } = await import(vitePath)
16
+ await start()
17
+ } else {
18
+ if (optimize) {
19
+ spinner.start()
20
+ const { viteBuildClient } = await import(vitePath)
21
+ await viteBuildClient()
22
+ process.env.NODE_ENV = 'development'
23
+ spinner.stop()
24
+ }
25
+ const { start } = await import(webpackPath)
26
+ await start()
27
+ }
28
+ },
29
+ build: async () => {
30
+ if (isVite) {
31
+ const { build } = await import(vitePath)
32
+ await build()
33
+ } else {
34
+ if (optimize) {
35
+ spinner.start()
36
+ const { build } = await import(vitePath)
37
+ await build()
38
+ spinner.stop()
39
+ }
40
+ const { build } = await import(webpackPath)
41
+ await build()
42
+ }
43
+ }
44
+ }
47
45
  }
48
-
49
- export * from './tools/vite'
@@ -1,7 +0,0 @@
1
- ssr-plugin-react:build: cache hit, replaying output 8059836ede6aedca
2
- ssr-plugin-react:build:
3
- ssr-plugin-react:build: > ssr-plugin-react@6.2.84 build /home/runner/work/ssr/ssr/packages/plugin-react
4
- ssr-plugin-react:build: > concurrently "tsc -p ./tsconfig.cjs.json " " tsc -p ./tsconfig.esm.json"
5
- ssr-plugin-react:build:
6
- ssr-plugin-react:build: [0] tsc -p ./tsconfig.cjs.json exited with code 0
7
- ssr-plugin-react:build: [1] tsc -p ./tsconfig.esm.json exited with code 0
@@ -1,4 +0,0 @@
1
- import * as WebpackChain from 'webpack-chain';
2
- declare const getBaseConfig: (chain: WebpackChain, isServer: boolean) => import("ssr-types").IConfig;
3
- export { getBaseConfig };
4
- //# sourceMappingURL=base.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/config/base.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,YAAY,MAAM,eAAe,CAAA;AAM7C,QAAA,MAAM,aAAa,UAAW,YAAY,YAAY,OAAO,gCAkD5D,CAAA;AAED,OAAO,EACL,aAAa,EACd,CAAA"}
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBaseConfig = void 0;
4
- const path_1 = require("path");
5
- const ssr_common_utils_1 = require("ssr-common-utils");
6
- const webpack = require("ssr-webpack4");
7
- const MiniCssExtractPlugin = require((0, ssr_common_utils_1.loadModuleFromFramework)('ssr-mini-css-extract-plugin'));
8
- const WebpackBar = require('webpackbar');
9
- const getBaseConfig = (chain, isServer) => {
10
- const config = (0, ssr_common_utils_1.loadConfig)();
11
- const { moduleFileExtensions, chainBaseConfig, alias, define } = config;
12
- const mode = process.env.NODE_ENV;
13
- chain.mode(mode);
14
- chain.module.strictExportPresence(true);
15
- chain
16
- .resolve
17
- .modules
18
- .add('node_modules')
19
- .add((0, path_1.join)((0, ssr_common_utils_1.getCwd)(), './node_modules'))
20
- .end()
21
- .extensions.merge(moduleFileExtensions)
22
- .end()
23
- .alias
24
- .end();
25
- alias && Object.keys(alias).forEach(item => {
26
- chain.resolve.alias
27
- .set(item, alias[item]);
28
- });
29
- (0, ssr_common_utils_1.addCommonChain)(chain, isServer);
30
- (0, ssr_common_utils_1.setStyle)(chain, /\.css$/, {
31
- rule: 'css',
32
- isServer,
33
- importLoaders: 1
34
- });
35
- (0, ssr_common_utils_1.setStyle)(chain, /\.less$/, {
36
- rule: 'less',
37
- loader: 'less-loader',
38
- isServer,
39
- importLoaders: 2
40
- });
41
- chain.plugin('minify-css').use(MiniCssExtractPlugin, (0, ssr_common_utils_1.getBuildConfig)().cssBuildConfig);
42
- chain.plugin('webpackBar').use(new WebpackBar({
43
- name: isServer ? 'server' : 'client',
44
- color: isServer ? '#f173ac' : '#45b97c'
45
- }));
46
- chain.plugin('ssrDefine').use(webpack.DefinePlugin, [{
47
- ...process.env,
48
- __isBrowser__: !isServer,
49
- ...(isServer ? define === null || define === void 0 ? void 0 : define.server : define === null || define === void 0 ? void 0 : define.client),
50
- ...define === null || define === void 0 ? void 0 : define.base
51
- }]);
52
- chainBaseConfig(chain, isServer);
53
- return config;
54
- };
55
- exports.getBaseConfig = getBaseConfig;
56
- //# sourceMappingURL=base.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/config/base.ts"],"names":[],"mappings":";;;AACA,+BAA2B;AAE3B,uDAAwH;AAExH,wCAAuC;AAEvC,MAAM,oBAAoB,GAAG,OAAO,CAAC,IAAA,0CAAuB,EAAC,6BAA6B,CAAC,CAAC,CAAA;AAC5F,MAAM,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;AAExC,MAAM,aAAa,GAAG,CAAC,KAAmB,EAAE,QAAiB,EAAE,EAAE;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AC,CAAA;IAErF,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC;QAC5C,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ;QACpC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS;KACxC,CAAC,CAAC,CAAA;IACH,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC;YACnD,GAAG,OAAO,CAAC,GAAG;YACd,aAAa,EAAE,CAAC,QAAQ;YACxB,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CAAC,CAAC,CAAC,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CAAC;YAC/C,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI;SAChB,CAAC,CAAC,CAAA;IACH,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;IAChC,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAGC,sCAAa"}
@@ -1,5 +0,0 @@
1
- /// <reference types="webpack" />
2
- import * as WebpackChain from 'webpack-chain';
3
- declare const getClientWebpack: (chain: WebpackChain) => import("webpack").Configuration;
4
- export { getClientWebpack };
5
- //# sourceMappingURL=client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/config/client.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,YAAY,MAAM,eAAe,CAAA;AAQ7C,QAAA,MAAM,gBAAgB,UAAW,YAAY,oCAmD5C,CAAA;AAED,OAAO,EACL,gBAAgB,EACjB,CAAA"}
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getClientWebpack = void 0;
4
- const ssr_common_utils_1 = require("ssr-common-utils");
5
- const base_1 = require("./base");
6
- const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
7
- const safePostCssParser = require('postcss-safe-parser');
8
- const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
9
- const loadModule = ssr_common_utils_1.loadModuleFromFramework;
10
- const getClientWebpack = (chain) => {
11
- const { isDev, chunkName, getOutput, cwd, chainClientConfig, host, fePort } = (0, ssr_common_utils_1.loadConfig)();
12
- const buildConfig = (0, ssr_common_utils_1.getBuildConfig)();
13
- const shouldUseSourceMap = isDev || Boolean(process.env.GENERATE_SOURCEMAP);
14
- const publicPath = (0, ssr_common_utils_1.getOutputPublicPath)();
15
- (0, base_1.getBaseConfig)(chain, false);
16
- chain.entry(chunkName)
17
- .add(require.resolve('../entry/client-entry'))
18
- .end()
19
- .output
20
- .path(getOutput().clientOutPut)
21
- .filename(buildConfig.jsBuldConfig.fileName)
22
- .chunkFilename(buildConfig.jsBuldConfig.chunkFileName)
23
- .publicPath(publicPath)
24
- .end();
25
- chain.optimization
26
- .runtimeChunk(true)
27
- .splitChunks((0, ssr_common_utils_1.getSplitChunksOptions)(ssr_common_utils_1.asyncChunkMap))
28
- .when(!isDev, optimization => {
29
- optimization.minimizer('terser')
30
- .use(loadModule('terser-webpack-plugin'), [(0, ssr_common_utils_1.terserConfig)(false)]);
31
- optimization.minimizer('optimize-css').use(loadModule('optimize-css-assets-webpack-plugin'), [{
32
- cssProcessorOptions: {
33
- parser: safePostCssParser,
34
- map: shouldUseSourceMap ? {
35
- inline: false,
36
- annotation: true
37
- } : false
38
- }
39
- }]);
40
- });
41
- chain.plugin('moduleNotFound').use(ModuleNotFoundPlugin, [cwd]);
42
- chain.plugin('manifest').use(loadModule('webpack-manifest-plugin'), [{
43
- fileName: 'asset-manifest.json'
44
- }]);
45
- chain.when(isDev, chain => {
46
- chain.plugin('fast-refresh').use(new ReactRefreshWebpackPlugin({
47
- overlay: {
48
- sockHost: host,
49
- sockPort: fePort
50
- }
51
- }));
52
- });
53
- chainClientConfig(chain); // 合并用户自定义配置
54
- return chain.toConfig();
55
- };
56
- exports.getClientWebpack = getClientWebpack;
57
- //# sourceMappingURL=client.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/config/client.ts"],"names":[],"mappings":";;;AAAA,uDAA+J;AAE/J,iCAAsC;AAEtC,MAAM,oBAAoB,GAAG,OAAO,CAAC,sCAAsC,CAAC,CAAA;AAC5E,MAAM,iBAAiB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAA;AACxD,MAAM,yBAAyB,GAAG,OAAO,CAAC,sCAAsC,CAAC,CAAA;AACjF,MAAM,UAAU,GAAG,0CAAuB,CAAA;AAE1C,MAAM,gBAAgB,GAAG,CAAC,KAAmB,EAAE,EAAE;IAC/C,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAA,6BAAU,GAAE,CAAA;IAC1F,MAAM,WAAW,GAAG,IAAA,iCAAc,GAAE,CAAA;IAEpC,MAAM,kBAAkB,GAAG,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;IAC3E,MAAM,UAAU,GAAG,IAAA,sCAAmB,GAAE,CAAA;IACxC,IAAA,oBAAa,EAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IAC3B,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC;SACnB,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC;SAC7C,GAAG,EAAE;SACL,MAAM;SACN,IAAI,CAAC,SAAS,EAAE,CAAC,YAAY,CAAC;SAC9B,QAAQ,CAAC,WAAW,CAAC,YAAY,CAAC,QAAQ,CAAC;SAC3C,aAAa,CAAC,WAAW,CAAC,YAAY,CAAC,aAAa,CAAC;SACrD,UAAU,CAAC,UAAU,CA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AAC,YAAY;IAErC,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAA;AACzB,CAAC,CAAA;AAGC,4CAAgB"}
@@ -1,3 +0,0 @@
1
- export * from './client';
2
- export * from './server';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}