ssr-plugin-react 6.2.84 → 6.2.85

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 (69) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/CHANGELOG.md +19 -0
  3. package/cjs/config/base.d.ts +1 -1
  4. package/cjs/config/base.d.ts.map +1 -1
  5. package/cjs/config/base.js +9 -16
  6. package/cjs/config/base.js.map +1 -1
  7. package/cjs/config/client.d.ts +1 -1
  8. package/cjs/config/client.d.ts.map +1 -1
  9. package/cjs/config/client.js +18 -21
  10. package/cjs/config/client.js.map +1 -1
  11. package/cjs/config/server.d.ts +1 -1
  12. package/cjs/config/server.d.ts.map +1 -1
  13. package/cjs/config/server.js +9 -12
  14. package/cjs/config/server.js.map +1 -1
  15. package/cjs/entry/client-entry.d.ts.map +1 -1
  16. package/cjs/entry/client-entry.js +2 -1
  17. package/cjs/entry/client-entry.js.map +1 -1
  18. package/cjs/entry/context.d.ts.map +1 -1
  19. package/cjs/entry/context.js.map +1 -1
  20. package/cjs/entry/create.d.ts.map +1 -1
  21. package/cjs/entry/create.js +4 -4
  22. package/cjs/entry/create.js.map +1 -1
  23. package/cjs/entry/server-entry.d.ts.map +1 -1
  24. package/cjs/entry/server-entry.js +29 -13
  25. package/cjs/entry/server-entry.js.map +1 -1
  26. package/cjs/index.js.map +1 -1
  27. package/cjs/tools/vite.d.ts.map +1 -1
  28. package/cjs/tools/vite.js +11 -20
  29. package/cjs/tools/vite.js.map +1 -1
  30. package/cjs/tools/webpack.js.map +1 -1
  31. package/esm/config/base.d.ts +1 -1
  32. package/esm/config/base.d.ts.map +1 -1
  33. package/esm/config/base.js +10 -17
  34. package/esm/config/base.js.map +1 -1
  35. package/esm/config/client.d.ts +1 -1
  36. package/esm/config/client.d.ts.map +1 -1
  37. package/esm/config/client.js +19 -22
  38. package/esm/config/client.js.map +1 -1
  39. package/esm/config/server.d.ts +1 -1
  40. package/esm/config/server.d.ts.map +1 -1
  41. package/esm/config/server.js +9 -12
  42. package/esm/config/server.js.map +1 -1
  43. package/esm/entry/client-entry.d.ts.map +1 -1
  44. package/esm/entry/client-entry.js +2 -1
  45. package/esm/entry/client-entry.js.map +1 -1
  46. package/esm/entry/context.d.ts.map +1 -1
  47. package/esm/entry/context.js.map +1 -1
  48. package/esm/entry/create.d.ts.map +1 -1
  49. package/esm/entry/create.js +4 -4
  50. package/esm/entry/create.js.map +1 -1
  51. package/esm/entry/server-entry.d.ts.map +1 -1
  52. package/esm/entry/server-entry.js +29 -13
  53. package/esm/entry/server-entry.js.map +1 -1
  54. package/esm/index.js.map +1 -1
  55. package/esm/tools/vite.d.ts.map +1 -1
  56. package/esm/tools/vite.js +12 -21
  57. package/esm/tools/vite.js.map +1 -1
  58. package/esm/tools/webpack.js.map +1 -1
  59. package/package.json +1 -1
  60. package/src/config/base.ts +47 -55
  61. package/src/config/client.ts +52 -55
  62. package/src/config/server.ts +31 -36
  63. package/src/entry/client-entry.tsx +54 -38
  64. package/src/entry/context.tsx +22 -19
  65. package/src/entry/create.ts +19 -19
  66. package/src/entry/server-entry.tsx +115 -82
  67. package/src/index.ts +41 -41
  68. package/src/tools/vite.ts +103 -122
  69. package/src/tools/webpack.ts +12 -12
@@ -1,65 +1,57 @@
1
-
2
1
  import { join } from 'path'
2
+ import { addCommonChain, getBuildConfig, getCwd, loadConfig, loadModuleFromFramework, setStyle } from 'ssr-common-utils'
3
3
  import { Mode } from 'ssr-types'
4
- import { getCwd, loadConfig, setStyle, loadModuleFromFramework, getBuildConfig, addCommonChain } from 'ssr-common-utils'
5
- import * as WebpackChain from 'webpack-chain'
6
4
  import * as webpack from 'ssr-webpack4'
5
+ import WebpackChain from 'webpack-chain'
7
6
 
8
7
  const MiniCssExtractPlugin = require(loadModuleFromFramework('ssr-mini-css-extract-plugin'))
9
8
  const WebpackBar = require('webpackbar')
10
9
 
11
10
  const getBaseConfig = (chain: WebpackChain, isServer: boolean) => {
12
- const config = loadConfig()
13
- const { moduleFileExtensions, chainBaseConfig, alias, define } = config
14
- const mode = process.env.NODE_ENV as Mode
15
-
16
- chain.mode(mode)
17
- chain.module.strictExportPresence(true)
18
- chain
19
- .resolve
20
- .modules
21
- .add('node_modules')
22
- .add(join(getCwd(), './node_modules'))
23
- .end()
24
- .extensions.merge(moduleFileExtensions)
25
- .end()
26
- .alias
27
- .end()
28
-
29
- alias && Object.keys(alias).forEach(item => {
30
- chain.resolve.alias
31
- .set(item, alias[item])
32
- })
33
- addCommonChain(chain, isServer)
34
- setStyle(chain, /\.css$/, {
35
- rule: 'css',
36
- isServer,
37
- importLoaders: 1
38
- })
39
-
40
- setStyle(chain, /\.less$/, {
41
- rule: 'less',
42
- loader: 'less-loader',
43
- isServer,
44
- importLoaders: 2
45
- })
46
-
47
- chain.plugin('minify-css').use(MiniCssExtractPlugin, getBuildConfig().cssBuildConfig)
48
-
49
- chain.plugin('webpackBar').use(new WebpackBar({
50
- name: isServer ? 'server' : 'client',
51
- color: isServer ? '#f173ac' : '#45b97c'
52
- }))
53
- chain.plugin('ssrDefine').use(webpack.DefinePlugin, [{
54
- ...process.env,
55
- __isBrowser__: !isServer,
56
- ...(isServer ? define?.server : define?.client),
57
- ...define?.base
58
- }])
59
- chainBaseConfig(chain, isServer)
60
- return config
11
+ const config = loadConfig()
12
+ const { moduleFileExtensions, chainBaseConfig, alias, define } = config
13
+ const mode = process.env.NODE_ENV as Mode
14
+
15
+ chain.mode(mode)
16
+ chain.module.strictExportPresence(true)
17
+ chain.resolve.modules.add('node_modules').add(join(getCwd(), './node_modules')).end().extensions.merge(moduleFileExtensions).end().alias.end()
18
+
19
+ alias &&
20
+ Object.keys(alias).forEach((item) => {
21
+ chain.resolve.alias.set(item, alias[item])
22
+ })
23
+ addCommonChain(chain, isServer)
24
+ setStyle(chain, /\.css$/, {
25
+ rule: 'css',
26
+ isServer,
27
+ importLoaders: 1
28
+ })
29
+
30
+ setStyle(chain, /\.less$/, {
31
+ rule: 'less',
32
+ loader: 'less-loader',
33
+ isServer,
34
+ importLoaders: 2
35
+ })
36
+
37
+ chain.plugin('minify-css').use(MiniCssExtractPlugin, getBuildConfig().cssBuildConfig)
38
+
39
+ chain.plugin('webpackBar').use(
40
+ new WebpackBar({
41
+ name: isServer ? 'server' : 'client',
42
+ color: isServer ? '#f173ac' : '#45b97c'
43
+ })
44
+ )
45
+ chain.plugin('ssrDefine').use(webpack.DefinePlugin, [
46
+ {
47
+ ...process.env,
48
+ __isBrowser__: !isServer,
49
+ ...(isServer ? define?.server : define?.client),
50
+ ...define?.base
51
+ }
52
+ ])
53
+ chainBaseConfig(chain, isServer)
54
+ return config
61
55
  }
62
56
 
63
- export {
64
- getBaseConfig
65
- }
57
+ export { getBaseConfig }
@@ -1,5 +1,5 @@
1
- import { loadConfig, getSplitChunksOptions, getOutputPublicPath, loadModuleFromFramework, getBuildConfig, terserConfig, asyncChunkMap } from 'ssr-common-utils'
2
- import * as WebpackChain from 'webpack-chain'
1
+ import { asyncChunkMap, getBuildConfig, getOutputPublicPath, getSplitChunksOptions, loadConfig, loadModuleFromFramework, terserConfig } from 'ssr-common-utils'
2
+ import WebpackChain from 'webpack-chain'
3
3
  import { getBaseConfig } from './base'
4
4
 
5
5
  const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin')
@@ -8,58 +8,55 @@ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'
8
8
  const loadModule = loadModuleFromFramework
9
9
 
10
10
  const getClientWebpack = (chain: WebpackChain) => {
11
- const { isDev, chunkName, getOutput, cwd, chainClientConfig, host, fePort } = loadConfig()
12
- const buildConfig = getBuildConfig()
13
-
14
- const shouldUseSourceMap = isDev || Boolean(process.env.GENERATE_SOURCEMAP)
15
- const publicPath = getOutputPublicPath()
16
- getBaseConfig(chain, false)
17
- chain.entry(chunkName)
18
- .add(require.resolve('../entry/client-entry'))
19
- .end()
20
- .output
21
- .path(getOutput().clientOutPut)
22
- .filename(buildConfig.jsBuldConfig.fileName)
23
- .chunkFilename(buildConfig.jsBuldConfig.chunkFileName)
24
- .publicPath(publicPath)
25
- .end()
26
- chain.optimization
27
- .runtimeChunk(true)
28
- .splitChunks(getSplitChunksOptions(asyncChunkMap))
29
- .when(!isDev, optimization => {
30
- optimization.minimizer('terser')
31
- .use(loadModule('terser-webpack-plugin'), [terserConfig(false)])
32
- optimization.minimizer('optimize-css').use(loadModule('optimize-css-assets-webpack-plugin'), [{
33
- cssProcessorOptions: {
34
- parser: safePostCssParser,
35
- map: shouldUseSourceMap ? {
36
- inline: false,
37
- annotation: true
38
- } : false
39
- }
40
- }])
41
- })
42
-
43
- chain.plugin('moduleNotFound').use(ModuleNotFoundPlugin, [cwd])
44
-
45
- chain.plugin('manifest').use(loadModule('webpack-manifest-plugin'), [{
46
- fileName: 'asset-manifest.json'
47
- }])
48
-
49
- chain.when(isDev, chain => {
50
- chain.plugin('fast-refresh').use(new ReactRefreshWebpackPlugin({
51
- overlay: {
52
- sockHost: host,
53
- sockPort: fePort
54
- }
55
- }))
56
- })
57
-
58
- chainClientConfig(chain) // 合并用户自定义配置
59
-
60
- return chain.toConfig()
11
+ const { isDev, chunkName, getOutput, cwd, chainClientConfig, host, fePort } = loadConfig()
12
+ const buildConfig = getBuildConfig()
13
+
14
+ const shouldUseSourceMap = isDev || Boolean(process.env.GENERATE_SOURCEMAP)
15
+ const publicPath = getOutputPublicPath()
16
+ getBaseConfig(chain, false)
17
+ chain.entry(chunkName).add(require.resolve('../entry/client-entry')).end().output.path(getOutput().clientOutPut).filename(buildConfig.jsBuldConfig.fileName).chunkFilename(buildConfig.jsBuldConfig.chunkFileName).publicPath(publicPath).end()
18
+ chain.optimization
19
+ .runtimeChunk(true)
20
+ .splitChunks(getSplitChunksOptions(asyncChunkMap))
21
+ .when(!isDev, (optimization) => {
22
+ optimization.minimizer('terser').use(loadModule('terser-webpack-plugin'), [terserConfig(false)])
23
+ optimization.minimizer('optimize-css').use(loadModule('optimize-css-assets-webpack-plugin'), [
24
+ {
25
+ cssProcessorOptions: {
26
+ parser: safePostCssParser,
27
+ map: shouldUseSourceMap
28
+ ? {
29
+ inline: false,
30
+ annotation: true
31
+ }
32
+ : false
33
+ }
34
+ }
35
+ ])
36
+ })
37
+
38
+ chain.plugin('moduleNotFound').use(ModuleNotFoundPlugin, [cwd])
39
+
40
+ chain.plugin('manifest').use(loadModule('webpack-manifest-plugin'), [
41
+ {
42
+ fileName: 'asset-manifest.json'
43
+ }
44
+ ])
45
+
46
+ chain.when(isDev, (chain) => {
47
+ chain.plugin('fast-refresh').use(
48
+ new ReactRefreshWebpackPlugin({
49
+ overlay: {
50
+ sockHost: host,
51
+ sockPort: fePort
52
+ }
53
+ })
54
+ )
55
+ })
56
+
57
+ chainClientConfig(chain) // 合并用户自定义配置
58
+
59
+ return chain.toConfig()
61
60
  }
62
61
 
63
- export {
64
- getClientWebpack
65
- }
62
+ export { getClientWebpack }
@@ -1,48 +1,43 @@
1
1
  import { loadConfig, loadModuleFromFramework } from 'ssr-common-utils'
2
- import * as WebpackChain from 'webpack-chain'
3
2
  import * as webpack from 'ssr-webpack4'
3
+ import WebpackChain from 'webpack-chain'
4
4
 
5
5
  import { getBaseConfig } from './base'
6
6
 
7
7
  const getServerWebpack = (chain: WebpackChain) => {
8
- const config = loadConfig()
9
- const { isDev, getOutput, chainServerConfig, chunkName } = config
10
- const shouldUseSourceMap = isDev || Boolean(process.env.GENERATE_SOURCEMAP)
11
- getBaseConfig(chain, true)
12
- chain.target('node')
13
- chain.entry(chunkName)
14
- .add(require.resolve('../entry/server-entry'))
15
- .end()
16
- .output
17
- .path(getOutput().serverOutPut)
18
- .filename('[name].server.js')
19
- .libraryTarget('commonjs')
8
+ const config = loadConfig()
9
+ const { isDev, getOutput, chainServerConfig, chunkName } = config
10
+ const shouldUseSourceMap = isDev || Boolean(process.env.GENERATE_SOURCEMAP)
11
+ getBaseConfig(chain, true)
12
+ chain.target('node')
13
+ chain.entry(chunkName).add(require.resolve('../entry/server-entry')).end().output.path(getOutput().serverOutPut).filename('[name].server.js').libraryTarget('commonjs')
20
14
 
21
- if (!isDev) {
22
- chain.optimization.minimizer('terser')
23
- .use(loadModuleFromFramework('terser-webpack-plugin'), [{
24
- terserOptions: {
25
- keep_fnames: true
26
- },
27
- extractComments: false,
28
- parallel: true,
29
- cache: true,
30
- sourceMap: shouldUseSourceMap
31
- }])
32
- }
33
- chain.when(isDev, () => {
34
- chain.watch(true)
35
- })
15
+ if (!isDev) {
16
+ chain.optimization.minimizer('terser').use(loadModuleFromFramework('terser-webpack-plugin'), [
17
+ {
18
+ terserOptions: {
19
+ keep_fnames: true
20
+ },
21
+ extractComments: false,
22
+ parallel: true,
23
+ cache: true,
24
+ sourceMap: shouldUseSourceMap
25
+ }
26
+ ])
27
+ }
28
+ chain.when(isDev, () => {
29
+ chain.watch(true)
30
+ })
36
31
 
37
- chain.plugin('serverLimit').use(webpack.optimize.LimitChunkCountPlugin, [{
38
- maxChunks: 1
39
- }])
32
+ chain.plugin('serverLimit').use(webpack.optimize.LimitChunkCountPlugin, [
33
+ {
34
+ maxChunks: 1
35
+ }
36
+ ])
40
37
 
41
- chainServerConfig(chain) // 合并用户自定义配置
38
+ chainServerConfig(chain) // 合并用户自定义配置
42
39
 
43
- return chain.toConfig()
40
+ return chain.toConfig()
44
41
  }
45
42
 
46
- export {
47
- getServerWebpack
48
- }
43
+ export { getServerWebpack }
@@ -11,45 +11,61 @@ import { AppContext } from './context'
11
11
  const { FeRoutes, layoutFetch, App } = Routes
12
12
 
13
13
  const clientRender = async (): Promise<void> => {
14
- const IApp = App ?? function(props: LayoutProps) {
15
- return props.children!
16
- }
17
- const context = ssrCreateContext() as any
18
- setStoreContext(context)
19
- const store = createStore(window.__VALTIO_DATA__)
20
- setStore(store ?? {})
21
- const baseName = isMicro() ? window.clientPrefix : window.prefix
22
- const routes = await preloadComponent(FeRoutes, baseName)
23
- ReactDOM[window.__USE_SSR__ ? 'hydrate' : 'render'](
24
- createElement(BrowserRouter, {
25
- basename: baseName
26
- }, createElement(AppContext, {
27
- context,
28
- children: createElement(Switch, null,
29
- createElement(IApp as any, null, createElement(Switch, null,
30
- routes.map(item => {
31
- const { fetch, component, path } = item
32
- component.fetch = fetch
33
- component.layoutFetch = layoutFetch
34
- const WrappedComponent = wrapComponent(component)
35
- return createElement(Route, {
36
- exact: true,
37
- key: path,
38
- path: path,
39
- render: () => createElement(WrappedComponent, {
40
- key: location.pathname
41
- })
42
- })
43
- }))))
44
- }))
45
- , document.querySelector(window.ssrDevInfo.rootId ?? '#app'))
46
- if (!window.__USE_VITE__) {
47
- (module as any)?.hot?.accept?.()
48
- }
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(window.__VALTIO_DATA__)
22
+ setStore(store ?? {})
23
+ const baseName = isMicro() ? window.clientPrefix : window.prefix
24
+ const routes = await preloadComponent(FeRoutes, baseName)
25
+ ReactDOM[window.__USE_SSR__ ? 'hydrate' : 'render'](
26
+ createElement(
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: () =>
52
+ createElement(WrappedComponent, {
53
+ key: location.pathname
54
+ })
55
+ })
56
+ })
57
+ )
58
+ )
59
+ )
60
+ })
61
+ ),
62
+ document.querySelector(window.ssrDevInfo.rootId ?? '#app')
63
+ )
64
+ if (!window.__USE_VITE__) {
65
+ ;(module as any)?.hot?.accept?.()
66
+ }
49
67
  }
50
68
 
51
69
  clientRender()
52
70
 
53
- export {
54
- clientRender
55
- }
71
+ export { clientRender }
@@ -5,35 +5,38 @@ import { Routes } from './create'
5
5
  const { reducer, state } = Routes as ReactRoutesType
6
6
 
7
7
  const userState = state ?? {}
8
- const userReducer = reducer ?? function() { }
8
+ const userReducer = reducer ?? function () {}
9
9
 
10
10
  const isDev = process.env.NODE_ENV !== 'production'
11
11
 
12
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
-
26
25
  function combineReducer(state: any, action: any) {
27
- return defaultReducer(state, action) || userReducer(state, action)
26
+ return defaultReducer(state, action) || userReducer(state, action)
28
27
  }
29
28
 
30
29
  export function AppContext(props: IProps) {
31
- const initialState = Object.assign({}, userState ?? {}, __isBrowser__ ? window?.__INITIAL_DATA__ : props.initialState)
32
- const [state, dispatch] = useReducer(combineReducer, initialState)
33
- return createElement(props.context.Provider, {
34
- value: {
35
- state,
36
- dispatch
37
- }
38
- }, props.children as any)
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
+ )
39
42
  }
@@ -1,29 +1,29 @@
1
- import { createContext } from 'react'
2
- import type { IContext } from 'ssr-types'
3
- import { proxy } from 'valtio'
4
- import { deepClone } from 'ssr-deepclone'
5
- import { combineRoutes } from 'ssr-common-utils'
6
1
  import * as declareRoutes from '_build/ssr-declare-routes'
7
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 type { IContext } from 'ssr-types'
8
7
  import { ReactRoutesType } from 'ssr-types'
8
+ import { proxy } from 'valtio'
9
9
 
10
10
  export const Routes = combineRoutes(declareRoutes, ManualRoutes) as ReactRoutesType
11
11
 
12
12
  export const ssrCreateContext = () => {
13
- const STORE_CONTEXT = createContext<IContext>({
14
- state: {}
15
- })
16
- if (__isBrowser__) {
17
- window.STORE_CONTEXT = STORE_CONTEXT
18
- }
19
- return STORE_CONTEXT
13
+ const STORE_CONTEXT = createContext<IContext>({
14
+ state: {}
15
+ })
16
+ if (__isBrowser__) {
17
+ window.STORE_CONTEXT = STORE_CONTEXT
18
+ }
19
+ return STORE_CONTEXT
20
20
  }
21
21
 
22
- export function createStore (initialData?: any) {
23
- const { store } = Routes
24
- const storeInstance = initialData ? store : deepClone(store)
25
- for (const key in storeInstance) {
26
- storeInstance[key] = initialData ? proxy(initialData[key]) : proxy(storeInstance[key])
27
- }
28
- return storeInstance
22
+ export function createStore(initialData?: any) {
23
+ const { store } = Routes
24
+ const storeInstance = initialData ? store : deepClone(store)
25
+ for (const key in storeInstance) {
26
+ storeInstance[key] = initialData ? proxy(initialData[key]) : proxy(storeInstance[key])
27
+ }
28
+ return storeInstance
29
29
  }