-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathindex.ts
More file actions
198 lines (177 loc) · 5.69 KB
/
Copy pathindex.ts
File metadata and controls
198 lines (177 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import path from 'path';
import type {
AppTools,
AppToolsNormalizedConfig,
CliPlugin,
ServerUserConfig,
} from '@modern-js/app-tools';
import type { CLIPluginAPI } from '@modern-js/plugin';
import type { Entrypoint } from '@modern-js/types';
import { LOADABLE_STATS_FILE, isUseSSRBundle } from '@modern-js/utils';
import { SERVER_BUNDLE_NAME } from '@modern-js/utils/universal/constants';
import type { RsbuildPlugin } from '@rsbuild/core';
import LoadableBundlerPlugin from './loadable-bundler-plugin';
import { resolveSSRMode } from './mode';
const hasStringSSREntry = (userConfig: AppToolsNormalizedConfig): boolean => {
const isStreaming = (ssr: ServerUserConfig['ssr']) =>
ssr && typeof ssr === 'object' && ssr.mode === 'stream';
const { server, output } = userConfig;
// ssg need use stringSSR.
if (output?.ssg) {
return true;
}
if (output?.ssgByEntries && Object.keys(output.ssgByEntries).length > 0) {
return true;
}
if (server?.ssr && !isStreaming(server.ssr)) {
return true;
}
if (server?.ssrByEntries && typeof server.ssrByEntries === 'object') {
for (const name of Object.keys(server.ssrByEntries)) {
if (
server.ssrByEntries[name] &&
!isStreaming(server.ssrByEntries[name])
) {
return true;
}
}
}
return false;
};
/**
* Check if any entry uses string SSR mode.
* Returns true if at least one entry uses 'string' SSR mode.
*/
const checkUseStringSSR = (
config: AppToolsNormalizedConfig,
appDirectory?: string,
entrypoints?: Entrypoint[],
): boolean => {
// If entrypoints are provided, check each entry
if (entrypoints && entrypoints.length > 0) {
for (const entrypoint of entrypoints) {
const ssrMode = resolveSSRMode({
entry: entrypoint.entryName,
config,
appDirectory,
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
});
if (ssrMode === 'string') {
return true;
}
}
return false;
}
return true;
};
const ssrBuilderPlugin = (
modernAPI: CLIPluginAPI<AppTools>,
outputModule: boolean,
exportLoadablePath: string,
): RsbuildPlugin => ({
name: '@modern-js/builder-plugin-ssr',
setup(api) {
api.modifyEnvironmentConfig((config, { name, mergeEnvironmentConfig }) => {
const isServerEnvironment =
config.output.target === 'node' ||
name === 'workerSSR' ||
name === SERVER_BUNDLE_NAME;
const userConfig = modernAPI.getNormalizedConfig();
// Maybe we can enable it for node 18 and above, but we can't ensure it in the compilation.
const ssrEnv =
userConfig.deploy?.worker?.ssr || userConfig.server?.rsc
? 'edge'
: 'node';
const appContext = modernAPI.getAppContext();
const { appDirectory, entrypoints } = appContext;
const useLoadablePlugin =
isUseSSRBundle(userConfig) &&
!isServerEnvironment &&
checkUseStringSSR(userConfig, appDirectory, entrypoints);
const useLoadableComponents =
isUseSSRBundle(userConfig) &&
checkUseStringSSR(userConfig, appDirectory, entrypoints);
return mergeEnvironmentConfig(config, {
source: {
define: {
'process.env.MODERN_TARGET': isServerEnvironment
? JSON.stringify('node')
: JSON.stringify('browser'),
'process.env.MODERN_SSR_ENV': JSON.stringify(ssrEnv),
},
},
output: {
module: isServerEnvironment && outputModule,
},
tools: {
bundlerChain: useLoadablePlugin
? chain => {
chain
.plugin('loadable')
.use(LoadableBundlerPlugin, [
{ filename: LOADABLE_STATS_FILE },
]);
}
: undefined,
swc: useLoadableComponents
? {
jsc: {
experimental: {
plugins: [
[
require.resolve('@swc/plugin-loadable-components'),
{
signatures: [
{ name: 'default', from: '@loadable/component' },
{ name: 'lazy', from: '@loadable/component' },
{
name: 'default',
from: exportLoadablePath,
},
{
name: 'lazy',
from: exportLoadablePath,
},
],
},
],
],
},
},
}
: undefined,
},
});
});
},
});
export const ssrPlugin = (): CliPlugin<AppTools> => ({
name: '@modern-js/plugin-ssr',
required: ['@modern-js/runtime'],
setup: api => {
const appContext = api.getAppContext();
const exportLoadablePath = `@${appContext.metaName}/runtime/loadable`;
const runtimeUtilsPath = require.resolve('@modern-js/runtime-utils/node');
const aliasPath = runtimeUtilsPath
.replace(`${path.sep}cjs${path.sep}`, `${path.sep}esm${path.sep}`)
.replace(/\.js$/, '.mjs');
api.config(() => {
return {
builderPlugins: [
ssrBuilderPlugin(
api,
appContext.moduleType === 'module',
exportLoadablePath,
),
],
resolve: {
alias: {
// ensure that all packages use the same storage in @modern-js/runtime-utils/node
'@modern-js/runtime-utils/node$': aliasPath,
},
},
};
});
},
});
export default ssrPlugin;