@bleedingdev/modern-js-app-tools 3.2.0-ultramodern.5 → 3.2.0-ultramodern.51
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/dist/cjs/builder/generator/getBuilderEnvironments.js +143 -8
- package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +41 -5
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +3 -1
- package/dist/cjs/plugins/deploy/index.js +17 -6
- package/dist/cjs/plugins/deploy/platforms/cloudflare.js +220 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +258 -0
- package/dist/cjs/rsbuild.js +3 -0
- package/dist/esm/builder/generator/getBuilderEnvironments.mjs +132 -8
- package/dist/esm/builder/shared/builderPlugins/adapterBasic.mjs +41 -5
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.mjs +3 -1
- package/dist/esm/plugins/deploy/index.mjs +10 -4
- package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +176 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +258 -0
- package/dist/esm/rsbuild.mjs +5 -2
- package/dist/esm-node/builder/generator/getBuilderEnvironments.mjs +134 -9
- package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.mjs +41 -5
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.mjs +3 -1
- package/dist/esm-node/plugins/deploy/index.mjs +10 -4
- package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +177 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +258 -0
- package/dist/esm-node/rsbuild.mjs +5 -2
- package/dist/types/baseline.d.ts +46 -0
- package/dist/types/builder/builder-rspack/index.d.ts +2 -0
- package/dist/types/builder/generator/adapterCopy.d.ts +3 -0
- package/dist/types/builder/generator/createBuilderProviderConfig.d.ts +3 -0
- package/dist/types/builder/generator/createCopyPattern.d.ts +16 -0
- package/dist/types/builder/generator/getBuilderEnvironments.d.ts +6 -0
- package/dist/types/builder/generator/index.d.ts +8 -0
- package/dist/types/builder/index.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/adapterBasic.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/adapterHtml.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/adapterPrecompress.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/adapterSSR.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/builderHooks.d.ts +3 -0
- package/dist/types/builder/shared/builderPlugins/index.d.ts +5 -0
- package/dist/types/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.d.ts +7 -0
- package/dist/types/builder/shared/bundlerPlugins/HtmlBottomTemplate.d.ts +9 -0
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +32 -0
- package/dist/types/builder/shared/bundlerPlugins/index.d.ts +3 -0
- package/dist/types/builder/shared/createCopyInfo.d.ts +8 -0
- package/dist/types/builder/shared/index.d.ts +3 -0
- package/dist/types/builder/shared/loaders/serverModuleLoader.d.ts +3 -0
- package/dist/types/builder/shared/types.d.ts +6 -0
- package/dist/types/commands/build.d.ts +4 -0
- package/dist/types/commands/deploy.d.ts +3 -0
- package/dist/types/commands/dev.d.ts +9 -0
- package/dist/types/commands/index.d.ts +10 -0
- package/dist/types/commands/info.d.ts +18 -0
- package/dist/types/commands/inspect.d.ts +4 -0
- package/dist/types/commands/runtime.d.ts +11 -0
- package/dist/types/commands/serve.d.ts +8 -0
- package/dist/types/compat/hooks.d.ts +7 -0
- package/dist/types/compat/index.d.ts +2 -0
- package/dist/types/compat/utils.d.ts +13 -0
- package/dist/types/config/default.d.ts +3 -0
- package/dist/types/config/index.d.ts +2 -0
- package/dist/types/config/initialize/index.d.ts +3 -0
- package/dist/types/config/initialize/inits.d.ts +4 -0
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/defineConfig.d.ts +11 -0
- package/dist/types/esm/register-esm.d.mts +5 -0
- package/dist/types/esm/ts-paths-loader.d.mts +6 -0
- package/dist/types/exports/server.d.ts +1 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/locale/en.d.ts +44 -0
- package/dist/types/locale/index.d.ts +3 -0
- package/dist/types/locale/zh.d.ts +44 -0
- package/dist/types/plugins/analyze/constants.d.ts +3 -0
- package/dist/types/plugins/analyze/getBundleEntry.d.ts +4 -0
- package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +7 -0
- package/dist/types/plugins/analyze/getHtmlTemplate.d.ts +27 -0
- package/dist/types/plugins/analyze/getServerRoutes.d.ts +8 -0
- package/dist/types/plugins/analyze/index.d.ts +3 -0
- package/dist/types/plugins/analyze/isDefaultExportFunction.d.ts +1 -0
- package/dist/types/plugins/analyze/templates.d.ts +5 -0
- package/dist/types/plugins/analyze/utils.d.ts +10 -0
- package/dist/types/plugins/deploy/index.d.ts +6 -0
- package/dist/types/plugins/deploy/platforms/cloudflare.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/gh-pages.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/netlify.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/node.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/platform.d.ts +17 -0
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.d.mts +4 -0
- package/dist/types/plugins/deploy/platforms/templates/netlify-entry.d.cts +2 -0
- package/dist/types/plugins/deploy/platforms/templates/netlify-entry.d.mts +2 -0
- package/dist/types/plugins/deploy/platforms/templates/node-entry.d.cts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/node-entry.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/vercel-entry.d.cts +2 -0
- package/dist/types/plugins/deploy/platforms/templates/vercel-entry.d.mts +2 -0
- package/dist/types/plugins/deploy/platforms/vercel.d.ts +2 -0
- package/dist/types/plugins/deploy/types.d.ts +3 -0
- package/dist/types/plugins/deploy/utils/generator.d.ts +24 -0
- package/dist/types/plugins/deploy/utils/index.d.ts +16 -0
- package/dist/types/plugins/initialize/index.d.ts +3 -0
- package/dist/types/plugins/serverBuild.d.ts +3 -0
- package/dist/types/plugins/serverRuntime.d.ts +3 -0
- package/dist/types/presetUltramodern.d.ts +6 -0
- package/dist/types/rsbuild.d.ts +12 -0
- package/dist/types/run/index.d.ts +20 -0
- package/dist/types/types/config/deploy.d.ts +46 -0
- package/dist/types/types/config/dev.d.ts +55 -0
- package/dist/types/types/config/experiments.d.ts +3 -0
- package/dist/types/types/config/html.d.ts +3 -0
- package/dist/types/types/config/index.d.ts +48 -0
- package/dist/types/types/config/output.d.ts +61 -0
- package/dist/types/types/config/performance.d.ts +3 -0
- package/dist/types/types/config/resolve.d.ts +2 -0
- package/dist/types/types/config/security.d.ts +2 -0
- package/dist/types/types/config/source.d.ts +61 -0
- package/dist/types/types/config/testing.d.ts +8 -0
- package/dist/types/types/config/tools.d.ts +21 -0
- package/dist/types/types/index.d.ts +10 -0
- package/dist/types/types/plugin.d.ts +114 -0
- package/dist/types/types/utils.d.ts +1 -0
- package/dist/types/ultramodern/designSystem.d.ts +54 -0
- package/dist/types/utils/config.d.ts +2 -0
- package/dist/types/utils/createServer.d.ts +5 -0
- package/dist/types/utils/env.d.ts +2 -0
- package/dist/types/utils/generateWatchFiles.d.ts +2 -0
- package/dist/types/utils/getConfigFile.d.ts +1 -0
- package/dist/types/utils/getSelectedEntries.d.ts +5 -0
- package/dist/types/utils/initAppContext.d.ts +31 -0
- package/dist/types/utils/loadPlugins.d.ts +13 -0
- package/dist/types/utils/printInstructions.d.ts +3 -0
- package/dist/types/utils/register.d.ts +19 -0
- package/dist/types/utils/restart.d.ts +2 -0
- package/dist/types/utils/routes.d.ts +4 -0
- package/dist/types/utils/types.d.ts +49 -0
- package/package.json +15 -15
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
const ASSETS_BINDING = 'ASSETS';
|
|
2
|
+
const MODERN_WORKER_MANIFEST = p_workerManifest;
|
|
3
|
+
const WORKER_MODULE_LOADERS = p_workerModuleLoaders;
|
|
4
|
+
const workerModulePromises = new Map();
|
|
5
|
+
const CORS_HEADERS = {
|
|
6
|
+
'access-control-allow-headers': '*',
|
|
7
|
+
'access-control-allow-methods': 'GET, HEAD, OPTIONS',
|
|
8
|
+
'access-control-allow-origin': '*'
|
|
9
|
+
};
|
|
10
|
+
globalThis.__dirname ??= '/';
|
|
11
|
+
globalThis.__filename ??= '/index.js';
|
|
12
|
+
function withCorsHeaders(response) {
|
|
13
|
+
const headers = new Headers(response.headers);
|
|
14
|
+
for (const [name, value] of Object.entries(CORS_HEADERS))if (!headers.has(name)) headers.set(name, value);
|
|
15
|
+
return new Response(response.body, {
|
|
16
|
+
headers,
|
|
17
|
+
status: response.status,
|
|
18
|
+
statusText: response.statusText
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function createCorsPreflightResponse(request) {
|
|
22
|
+
if ('OPTIONS' !== request.method) return null;
|
|
23
|
+
return new Response(null, {
|
|
24
|
+
headers: CORS_HEADERS,
|
|
25
|
+
status: 204
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async function fetchAsset(request, env) {
|
|
29
|
+
const assets = env?.[ASSETS_BINDING];
|
|
30
|
+
if (!assets || 'function' != typeof assets.fetch) return null;
|
|
31
|
+
const response = await assets.fetch(request);
|
|
32
|
+
if (404 === response.status) return null;
|
|
33
|
+
return withCorsHeaders(response);
|
|
34
|
+
}
|
|
35
|
+
async function fetchAssetByPath(pathname, request, env) {
|
|
36
|
+
const url = new URL(request.url);
|
|
37
|
+
url.pathname = `/${pathname.replace(/^\/+/u, '')}`;
|
|
38
|
+
return fetchAsset(new Request(url, request), env);
|
|
39
|
+
}
|
|
40
|
+
async function fetchAssetByPathFollowingRedirects(pathname, request, env, visited = new Set()) {
|
|
41
|
+
const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
42
|
+
if (visited.has(normalizedPathname)) return null;
|
|
43
|
+
visited.add(normalizedPathname);
|
|
44
|
+
const response = await fetchAssetByPath(normalizedPathname, request, env);
|
|
45
|
+
if (response && response.status >= 300 && response.status < 400 && response.headers.has('location')) {
|
|
46
|
+
const location = response.headers.get('location');
|
|
47
|
+
if (location) {
|
|
48
|
+
const nextUrl = new URL(location, request.url);
|
|
49
|
+
const currentUrl = new URL(request.url);
|
|
50
|
+
if (nextUrl.origin === currentUrl.origin) return fetchAssetByPathFollowingRedirects(nextUrl.pathname, request, env, visited);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
async function readAssetText(pathname, request, env) {
|
|
56
|
+
const response = await fetchAssetByPathFollowingRedirects(pathname, request, env);
|
|
57
|
+
if (!response || !response.ok) return;
|
|
58
|
+
return response.text();
|
|
59
|
+
}
|
|
60
|
+
async function readAssetJson(pathname, request, env) {
|
|
61
|
+
const text = await readAssetText(pathname, request, env);
|
|
62
|
+
if (!text) return {};
|
|
63
|
+
return JSON.parse(text);
|
|
64
|
+
}
|
|
65
|
+
function normalizeRoutePath(pathname) {
|
|
66
|
+
if ('/' === pathname) return pathname;
|
|
67
|
+
return pathname.replace(/\/+$/u, '');
|
|
68
|
+
}
|
|
69
|
+
function routeMatches(route, pathname) {
|
|
70
|
+
if ('string' != typeof route.urlPath) return false;
|
|
71
|
+
const routePath = normalizeRoutePath(route.urlPath);
|
|
72
|
+
const requestPath = normalizeRoutePath(pathname);
|
|
73
|
+
return routePath === requestPath || '/' === routePath && route.isSSR || requestPath.startsWith(`${routePath}/`);
|
|
74
|
+
}
|
|
75
|
+
function findRoute(request) {
|
|
76
|
+
const { pathname } = new URL(request.url);
|
|
77
|
+
const routes = MODERN_WORKER_MANIFEST.routeSpec.routes;
|
|
78
|
+
return [
|
|
79
|
+
...routes
|
|
80
|
+
].sort((left, right)=>{
|
|
81
|
+
const leftLength = left.urlPath?.length || 0;
|
|
82
|
+
const rightLength = right.urlPath?.length || 0;
|
|
83
|
+
return rightLength - leftLength;
|
|
84
|
+
}).find((route)=>routeMatches(route, pathname));
|
|
85
|
+
}
|
|
86
|
+
async function fetchRouteHtml(route, request, env) {
|
|
87
|
+
if (!route?.entryPath) return null;
|
|
88
|
+
return fetchAssetByPath(route.entryPath, request, env);
|
|
89
|
+
}
|
|
90
|
+
function createNoopMonitors() {
|
|
91
|
+
const noop = ()=>{};
|
|
92
|
+
return {
|
|
93
|
+
debug: noop,
|
|
94
|
+
error: noop,
|
|
95
|
+
info: noop,
|
|
96
|
+
warn: noop
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function createRequestHandlerOptions({ route, htmlTemplate, routeManifest, loadableStats }) {
|
|
100
|
+
const monitors = createNoopMonitors();
|
|
101
|
+
return {
|
|
102
|
+
resource: {
|
|
103
|
+
route,
|
|
104
|
+
routeManifest,
|
|
105
|
+
loadableStats,
|
|
106
|
+
htmlTemplate,
|
|
107
|
+
entryName: route.entryName
|
|
108
|
+
},
|
|
109
|
+
params: {},
|
|
110
|
+
loaderContext: {},
|
|
111
|
+
config: {},
|
|
112
|
+
locals: {},
|
|
113
|
+
staticGenerate: false,
|
|
114
|
+
monitors,
|
|
115
|
+
onError (error) {
|
|
116
|
+
monitors.error(error);
|
|
117
|
+
},
|
|
118
|
+
onTiming () {},
|
|
119
|
+
reporter: {
|
|
120
|
+
reportTiming: ()=>{}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async function getRequestHandlerOptions(route, request, env) {
|
|
125
|
+
const [htmlTemplate, routeManifest, loadableStats] = await Promise.all([
|
|
126
|
+
readAssetText(route.entryPath, request, env),
|
|
127
|
+
readAssetJson(MODERN_WORKER_MANIFEST.resources.routeManifest, request, env),
|
|
128
|
+
readAssetJson(MODERN_WORKER_MANIFEST.resources.loadableStats, request, env)
|
|
129
|
+
]);
|
|
130
|
+
return createRequestHandlerOptions({
|
|
131
|
+
route,
|
|
132
|
+
htmlTemplate: htmlTemplate || '',
|
|
133
|
+
routeManifest,
|
|
134
|
+
loadableStats
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async function loadWorkerModule(workerPath) {
|
|
138
|
+
const loader = WORKER_MODULE_LOADERS[workerPath];
|
|
139
|
+
if (!loader) return;
|
|
140
|
+
if (!workerModulePromises.has(workerPath)) workerModulePromises.set(workerPath, loader());
|
|
141
|
+
return workerModulePromises.get(workerPath);
|
|
142
|
+
}
|
|
143
|
+
function getFetchHandler(workerModule) {
|
|
144
|
+
const defaultExport = workerModule.default;
|
|
145
|
+
return defaultExport && 'object' == typeof defaultExport && 'function' == typeof defaultExport.fetch && defaultExport.fetch.bind(defaultExport) || 'function' == typeof workerModule.fetch && workerModule.fetch;
|
|
146
|
+
}
|
|
147
|
+
async function getRequestHandler(workerModule) {
|
|
148
|
+
const defaultExport = workerModule.default;
|
|
149
|
+
const defaultRequestHandler = defaultExport && 'object' == typeof defaultExport && 'requestHandler' in defaultExport ? defaultExport.requestHandler : void 0;
|
|
150
|
+
return await workerModule.requestHandler || await defaultRequestHandler || ('function' == typeof defaultExport ? defaultExport : void 0);
|
|
151
|
+
}
|
|
152
|
+
async function dispatchRouteWorker(route, request, env, ctx) {
|
|
153
|
+
const workerPath = route.worker;
|
|
154
|
+
if (!workerPath) return new Response('Worker bundle not configured for SSR route', {
|
|
155
|
+
status: 500,
|
|
156
|
+
headers: {
|
|
157
|
+
'content-type': 'text/plain; charset=utf-8'
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
const workerModule = await loadWorkerModule(workerPath);
|
|
161
|
+
if (!workerModule) return new Response(`Worker bundle not found: ${workerPath}`, {
|
|
162
|
+
status: 500,
|
|
163
|
+
headers: {
|
|
164
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
165
|
+
'x-modern-js-route-worker': workerPath
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const fetchHandler = getFetchHandler(workerModule);
|
|
169
|
+
if (fetchHandler) return fetchHandler(request, env, ctx);
|
|
170
|
+
const requestHandler = await getRequestHandler(workerModule);
|
|
171
|
+
if ('function' == typeof requestHandler) {
|
|
172
|
+
const requestHandlerOptions = await getRequestHandlerOptions(route, request, env);
|
|
173
|
+
return requestHandler(request, requestHandlerOptions);
|
|
174
|
+
}
|
|
175
|
+
return new Response(`Worker bundle has no fetch or requestHandler export: ${workerPath}`, {
|
|
176
|
+
status: 500,
|
|
177
|
+
headers: {
|
|
178
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
179
|
+
'x-modern-js-route-worker': workerPath
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function matchesPrefix(pathname, prefix) {
|
|
184
|
+
if (!prefix || '/' === prefix) return true;
|
|
185
|
+
const normalized = prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
186
|
+
return pathname === normalized || pathname.startsWith(`${normalized}/`);
|
|
187
|
+
}
|
|
188
|
+
function createRequestForMountedPrefix(request, prefix) {
|
|
189
|
+
if (!prefix || '/' === prefix) return request;
|
|
190
|
+
const url = new URL(request.url);
|
|
191
|
+
const normalized = prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
192
|
+
if (!matchesPrefix(url.pathname, normalized)) return request;
|
|
193
|
+
const nextPath = url.pathname.slice(normalized.length) || '/';
|
|
194
|
+
url.pathname = nextPath.startsWith('/') ? nextPath : `/${nextPath}`;
|
|
195
|
+
return new Request(url, request);
|
|
196
|
+
}
|
|
197
|
+
function createEffectContext(originalRequest, mountedRequest, env) {
|
|
198
|
+
const url = new URL(originalRequest.url);
|
|
199
|
+
return {
|
|
200
|
+
request: mountedRequest,
|
|
201
|
+
env: env || {},
|
|
202
|
+
path: url.pathname,
|
|
203
|
+
method: originalRequest.method,
|
|
204
|
+
operationContext: {
|
|
205
|
+
request: mountedRequest,
|
|
206
|
+
env: env || {},
|
|
207
|
+
path: url.pathname,
|
|
208
|
+
method: originalRequest.method
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
async function dispatchBffRequest(request, env) {
|
|
213
|
+
const bff = MODERN_WORKER_MANIFEST.bff;
|
|
214
|
+
if (!bff?.worker || !matchesPrefix(new URL(request.url).pathname, bff.prefix)) return null;
|
|
215
|
+
const workerModule = await loadWorkerModule(bff.worker);
|
|
216
|
+
if (!workerModule) return new Response(`BFF worker bundle not found: ${bff.worker}`, {
|
|
217
|
+
status: 500,
|
|
218
|
+
headers: {
|
|
219
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
220
|
+
'x-modern-js-bff-worker': bff.worker
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
const mountedRequest = createRequestForMountedPrefix(request, bff.prefix);
|
|
224
|
+
const effectContext = createEffectContext(request, mountedRequest, env);
|
|
225
|
+
const defaultExport = workerModule.default;
|
|
226
|
+
const runtime = defaultExport && 'object' == typeof defaultExport ? {
|
|
227
|
+
...workerModule,
|
|
228
|
+
...defaultExport
|
|
229
|
+
} : workerModule;
|
|
230
|
+
const directHandler = 'function' == typeof runtime.handler && runtime.handler || 'function' == typeof defaultExport && defaultExport;
|
|
231
|
+
const createdHandler = 'function' == typeof runtime.createHandler ? runtime.createHandler().handler : void 0;
|
|
232
|
+
const handler = directHandler || createdHandler;
|
|
233
|
+
if ('function' != typeof handler) return new Response(`BFF worker bundle has no handler export: ${bff.worker}`, {
|
|
234
|
+
status: 500,
|
|
235
|
+
headers: {
|
|
236
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
237
|
+
'x-modern-js-bff-worker': bff.worker
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
return handler.length > 1 ? handler(mountedRequest, effectContext) : handler(mountedRequest);
|
|
241
|
+
}
|
|
242
|
+
export default {
|
|
243
|
+
async fetch (request, env, ctx) {
|
|
244
|
+
const corsPreflightResponse = createCorsPreflightResponse(request);
|
|
245
|
+
if (corsPreflightResponse) return corsPreflightResponse;
|
|
246
|
+
const assetResponse = await fetchAsset(request, env);
|
|
247
|
+
if (assetResponse) return assetResponse;
|
|
248
|
+
const bffResponse = await dispatchBffRequest(request, env);
|
|
249
|
+
if (bffResponse) return withCorsHeaders(bffResponse);
|
|
250
|
+
const route = findRoute(request);
|
|
251
|
+
if (route?.worker) return withCorsHeaders(await dispatchRouteWorker(route, request, env, ctx));
|
|
252
|
+
const htmlResponse = await fetchRouteHtml(route, request, env);
|
|
253
|
+
if (htmlResponse) return htmlResponse;
|
|
254
|
+
return withCorsHeaders(new Response('Not found', {
|
|
255
|
+
status: 404
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
};
|
package/dist/cjs/rsbuild.js
CHANGED
|
@@ -42,9 +42,11 @@ var __webpack_exports__ = {};
|
|
|
42
42
|
resolveModernRsbuildConfig: ()=>resolveModernRsbuildConfig
|
|
43
43
|
});
|
|
44
44
|
const builder_namespaceObject = require("@modern-js/builder");
|
|
45
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
45
46
|
const index_js_namespaceObject = require("./builder/shared/builderPlugins/index.js");
|
|
46
47
|
const external_constants_js_namespaceObject = require("./constants.js");
|
|
47
48
|
const getConfigFile_js_namespaceObject = require("./utils/getConfigFile.js");
|
|
49
|
+
const loadPlugins_js_namespaceObject = require("./utils/loadPlugins.js");
|
|
48
50
|
const MODERN_META_NAME = 'modern-js';
|
|
49
51
|
const { createConfigOptions } = __webpack_require__("@modern-js/plugin/cli");
|
|
50
52
|
async function resolveModernRsbuildConfig(options) {
|
|
@@ -55,6 +57,7 @@ var __webpack_exports__ = {};
|
|
|
55
57
|
command: options.command,
|
|
56
58
|
cwd,
|
|
57
59
|
configFile,
|
|
60
|
+
internalPlugins: await (0, loadPlugins_js_namespaceObject.loadInternalPlugins)(cwd, utils_namespaceObject.INTERNAL_RUNTIME_PLUGINS),
|
|
58
61
|
metaName,
|
|
59
62
|
modifyModernConfig: options.modifyModernConfig
|
|
60
63
|
});
|
|
@@ -1,5 +1,40 @@
|
|
|
1
|
+
import node_fs from "node:fs";
|
|
2
|
+
import node_path from "node:path";
|
|
1
3
|
import { SERVICE_WORKER_ENVIRONMENT_NAME } from "@modern-js/builder";
|
|
2
4
|
import { isProd, isSSR, isServiceWorker, isUseRsc, isUseSSRBundle } from "@modern-js/utils";
|
|
5
|
+
const BFF_EFFECT_WORKER_ENTRY_NAME = '__modern_bff_effect';
|
|
6
|
+
const BFF_EFFECT_WORKER_RUNTIME_QUERY = 'modern-bff-runtime';
|
|
7
|
+
const JS_OR_TS_EXTS = [
|
|
8
|
+
'.ts',
|
|
9
|
+
'.tsx',
|
|
10
|
+
'.js',
|
|
11
|
+
'.jsx',
|
|
12
|
+
'.mjs',
|
|
13
|
+
'.cjs'
|
|
14
|
+
];
|
|
15
|
+
function findExistingFile(candidates) {
|
|
16
|
+
return candidates.find((candidate)=>node_fs.existsSync(candidate));
|
|
17
|
+
}
|
|
18
|
+
function resolvePackageFile(packageName, filePath, paths) {
|
|
19
|
+
try {
|
|
20
|
+
const packageJsonPath = require.resolve(`${packageName}/package.json`, {
|
|
21
|
+
paths
|
|
22
|
+
});
|
|
23
|
+
const packageFile = node_path.join(node_path.dirname(packageJsonPath), filePath);
|
|
24
|
+
return node_fs.existsSync(packageFile) ? node_fs.realpathSync(packageFile) : void 0;
|
|
25
|
+
} catch {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function setAliasIfPresent(alias, name, value) {
|
|
30
|
+
if (value) alias.set(name, value);
|
|
31
|
+
}
|
|
32
|
+
function getEffectBffEntry(normalizedConfig, appContext) {
|
|
33
|
+
if (!normalizedConfig.bff || 'hono' === normalizedConfig.bff.runtimeFramework) return;
|
|
34
|
+
const configuredEntry = normalizedConfig.bff.effect?.entry;
|
|
35
|
+
const entryWithoutExtension = configuredEntry ? node_path.isAbsolute(configuredEntry) ? configuredEntry : node_path.resolve(appContext.appDirectory, configuredEntry) : node_path.resolve(appContext.apiDirectory, 'effect', 'index');
|
|
36
|
+
return findExistingFile(JS_OR_TS_EXTS.map((extension)=>`${entryWithoutExtension}${extension}`));
|
|
37
|
+
}
|
|
3
38
|
function getBuilderEnvironments(normalizedConfig, appContext, tempBuilderConfig) {
|
|
4
39
|
const entries = {};
|
|
5
40
|
const { entrypoints = [], checkedEntries } = appContext;
|
|
@@ -16,6 +51,11 @@ function getBuilderEnvironments(normalizedConfig, appContext, tempBuilderConfig)
|
|
|
16
51
|
const v = entries[entry];
|
|
17
52
|
serverEntries[entry] = v.map((entry)=>entry.replace('index.jsx', 'index.server.jsx')).map((entry)=>entry.replace('bootstrap.jsx', 'bootstrap.server.jsx'));
|
|
18
53
|
}
|
|
54
|
+
const cloudflareWorkerServerEntries = {};
|
|
55
|
+
for(const entry in entries){
|
|
56
|
+
const v = entries[entry];
|
|
57
|
+
cloudflareWorkerServerEntries[entry] = v.map((entry)=>entry.replace('index.jsx', 'index.server.jsx')).map((entry)=>entry.replace('bootstrap.jsx', 'index.server.jsx'));
|
|
58
|
+
}
|
|
19
59
|
const environments = {
|
|
20
60
|
client: {
|
|
21
61
|
output: {
|
|
@@ -40,14 +80,98 @@ function getBuilderEnvironments(normalizedConfig, appContext, tempBuilderConfig)
|
|
|
40
80
|
}
|
|
41
81
|
};
|
|
42
82
|
const useWorkerTarget = isServiceWorker(normalizedConfig);
|
|
43
|
-
if (useWorkerTarget)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
83
|
+
if (useWorkerTarget) {
|
|
84
|
+
const useCloudflareModuleWorker = normalizedConfig.deploy?.target === 'cloudflare';
|
|
85
|
+
const effectBffEntry = useCloudflareModuleWorker ? getEffectBffEntry(normalizedConfig, appContext) : void 0;
|
|
86
|
+
const tanstackRouterSsrServerFile = useCloudflareModuleWorker ? resolvePackageFile('@tanstack/router-core', 'dist/esm/ssr/ssr-server.js', [
|
|
87
|
+
appContext.appDirectory,
|
|
88
|
+
process.cwd()
|
|
89
|
+
]) : void 0;
|
|
90
|
+
const runtimeRscWorkerFile = useCloudflareModuleWorker ? resolvePackageFile('@modern-js/runtime', 'dist/esm/rsc/server.worker.mjs', [
|
|
91
|
+
appContext.appDirectory,
|
|
92
|
+
process.cwd()
|
|
93
|
+
]) : void 0;
|
|
94
|
+
const renderRscWorkerFile = useCloudflareModuleWorker ? resolvePackageFile('@modern-js/render', 'dist/esm/rscWorker.mjs', [
|
|
95
|
+
appContext.appDirectory,
|
|
96
|
+
process.cwd()
|
|
97
|
+
]) : void 0;
|
|
98
|
+
const reactFile = useCloudflareModuleWorker ? resolvePackageFile('react', 'index.js', [
|
|
99
|
+
appContext.appDirectory,
|
|
100
|
+
process.cwd()
|
|
101
|
+
]) : void 0;
|
|
102
|
+
const reactJsxRuntimeFile = useCloudflareModuleWorker ? resolvePackageFile('react', 'jsx-runtime.js', [
|
|
103
|
+
appContext.appDirectory,
|
|
104
|
+
process.cwd()
|
|
105
|
+
]) : void 0;
|
|
106
|
+
const reactJsxDevRuntimeFile = useCloudflareModuleWorker ? resolvePackageFile('react', 'jsx-dev-runtime.js', [
|
|
107
|
+
appContext.appDirectory,
|
|
108
|
+
process.cwd()
|
|
109
|
+
]) : void 0;
|
|
110
|
+
const reactDomFile = useCloudflareModuleWorker ? resolvePackageFile('react-dom', 'index.js', [
|
|
111
|
+
appContext.appDirectory,
|
|
112
|
+
process.cwd()
|
|
113
|
+
]) : void 0;
|
|
114
|
+
const reactDomServerEdgeFile = useCloudflareModuleWorker ? resolvePackageFile('react-dom', 'server.edge.js', [
|
|
115
|
+
appContext.appDirectory,
|
|
116
|
+
process.cwd()
|
|
117
|
+
]) : void 0;
|
|
118
|
+
const baseWorkerEntries = useCloudflareModuleWorker ? cloudflareWorkerServerEntries : serverEntries;
|
|
119
|
+
const workerEntries = effectBffEntry ? {
|
|
120
|
+
...baseWorkerEntries,
|
|
121
|
+
[BFF_EFFECT_WORKER_ENTRY_NAME]: [
|
|
122
|
+
`${effectBffEntry}?${BFF_EFFECT_WORKER_RUNTIME_QUERY}`
|
|
123
|
+
]
|
|
124
|
+
} : baseWorkerEntries;
|
|
125
|
+
environments[SERVICE_WORKER_ENVIRONMENT_NAME] = {
|
|
126
|
+
output: {
|
|
127
|
+
target: useCloudflareModuleWorker ? 'web' : 'web-worker',
|
|
128
|
+
...useCloudflareModuleWorker ? {
|
|
129
|
+
module: true
|
|
130
|
+
} : {}
|
|
131
|
+
},
|
|
132
|
+
source: {
|
|
133
|
+
entry: workerEntries
|
|
134
|
+
},
|
|
135
|
+
tools: {
|
|
136
|
+
htmlPlugin: false,
|
|
137
|
+
...useCloudflareModuleWorker ? {
|
|
138
|
+
bundlerChain: (chain)=>{
|
|
139
|
+
chain.merge({
|
|
140
|
+
experiments: {
|
|
141
|
+
outputModule: true
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
chain.target('webworker');
|
|
145
|
+
chain.plugins.delete('plugin-module-federation');
|
|
146
|
+
if (tanstackRouterSsrServerFile) {
|
|
147
|
+
chain.resolve.alias.set('@tanstack/router-core/ssr/server$', tanstackRouterSsrServerFile);
|
|
148
|
+
chain.resolve.alias.set('@tanstack/router-core/ssr/server', tanstackRouterSsrServerFile);
|
|
149
|
+
}
|
|
150
|
+
if (runtimeRscWorkerFile) {
|
|
151
|
+
chain.resolve.alias.set('@modern-js/runtime/rsc/server$', runtimeRscWorkerFile);
|
|
152
|
+
chain.resolve.alias.set('@modern-js/runtime/rsc/server', runtimeRscWorkerFile);
|
|
153
|
+
}
|
|
154
|
+
if (renderRscWorkerFile) {
|
|
155
|
+
chain.resolve.alias.set('@modern-js/render/rsc$', renderRscWorkerFile);
|
|
156
|
+
chain.resolve.alias.set('@modern-js/render/rsc', renderRscWorkerFile);
|
|
157
|
+
chain.resolve.alias.set('@modern-js/render/rsc-worker$', renderRscWorkerFile);
|
|
158
|
+
}
|
|
159
|
+
setAliasIfPresent(chain.resolve.alias, 'react$', reactFile);
|
|
160
|
+
setAliasIfPresent(chain.resolve.alias, 'react/jsx-runtime$', reactJsxRuntimeFile);
|
|
161
|
+
setAliasIfPresent(chain.resolve.alias, 'react/jsx-dev-runtime$', reactJsxDevRuntimeFile);
|
|
162
|
+
setAliasIfPresent(chain.resolve.alias, 'react-dom$', reactDomFile);
|
|
163
|
+
setAliasIfPresent(chain.resolve.alias, 'react-dom/server.edge$', reactDomServerEdgeFile);
|
|
164
|
+
chain.resolve.alias.set('react-server-dom-rspack/server.node$', 'react-server-dom-rspack/server.edge');
|
|
165
|
+
chain.resolve.alias.set('react-server-dom-rspack/server.node', 'react-server-dom-rspack/server.edge');
|
|
166
|
+
chain.resolve.alias.set('react-server-dom-rspack/client.node$', 'react-server-dom-rspack/client.edge');
|
|
167
|
+
chain.resolve.alias.set('react-server-dom-rspack/client.node', 'react-server-dom-rspack/client.edge');
|
|
168
|
+
chain.resolve.fallback.set('async_hooks', false);
|
|
169
|
+
chain.resolve.fallback.set('node:async_hooks', false);
|
|
170
|
+
}
|
|
171
|
+
} : {}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
51
175
|
return {
|
|
52
176
|
environments,
|
|
53
177
|
builderConfig: tempBuilderConfig
|
|
@@ -5,8 +5,11 @@ const builderPluginAdapterBasic = (options)=>({
|
|
|
5
5
|
setup (api) {
|
|
6
6
|
api.modifyBundlerChain((chain, { target, CHAIN_ID, environment })=>{
|
|
7
7
|
const isServiceWorker = environment.name === SERVICE_WORKER_ENVIRONMENT_NAME;
|
|
8
|
-
|
|
9
|
-
if ('
|
|
8
|
+
const isWebTargetServiceWorker = isServiceWorker && 'web' === target;
|
|
9
|
+
if ('node' === target || isServiceWorker) applyNodeCompat(isServiceWorker, chain, {
|
|
10
|
+
includeNodeExtensions: !isWebTargetServiceWorker
|
|
11
|
+
});
|
|
12
|
+
if ('web' === target && !isServiceWorker) {
|
|
10
13
|
const bareServerModuleReg = /\.(server|node)\.[tj]sx?$/;
|
|
11
14
|
const depExt = 'mjs';
|
|
12
15
|
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(bareServerModuleReg);
|
|
@@ -22,8 +25,40 @@ const builderPluginAdapterBasic = (options)=>({
|
|
|
22
25
|
});
|
|
23
26
|
api.modifyRspackConfig((config, { target, environment })=>{
|
|
24
27
|
const isServiceWorker = environment.name === SERVICE_WORKER_ENVIRONMENT_NAME;
|
|
28
|
+
const isWebTargetServiceWorker = isServiceWorker && 'web' === target;
|
|
25
29
|
if ('node' === target || isServiceWorker) {
|
|
26
|
-
const extensionAlias = {
|
|
30
|
+
const extensionAlias = isWebTargetServiceWorker ? {
|
|
31
|
+
'.js': [
|
|
32
|
+
'.worker.js',
|
|
33
|
+
'.server.js',
|
|
34
|
+
'.js'
|
|
35
|
+
],
|
|
36
|
+
'.jsx': [
|
|
37
|
+
'.worker.jsx',
|
|
38
|
+
'.server.jsx',
|
|
39
|
+
'.jsx'
|
|
40
|
+
],
|
|
41
|
+
'.ts': [
|
|
42
|
+
'.worker.ts',
|
|
43
|
+
'.server.ts',
|
|
44
|
+
'.ts'
|
|
45
|
+
],
|
|
46
|
+
'.tsx': [
|
|
47
|
+
'.worker.tsx',
|
|
48
|
+
'.server.tsx',
|
|
49
|
+
'.tsx'
|
|
50
|
+
],
|
|
51
|
+
'.mjs': [
|
|
52
|
+
'.worker.mjs',
|
|
53
|
+
'.server.mjs',
|
|
54
|
+
'.mjs'
|
|
55
|
+
],
|
|
56
|
+
'.json': [
|
|
57
|
+
'.worker.json',
|
|
58
|
+
'.server.json',
|
|
59
|
+
'.json'
|
|
60
|
+
]
|
|
61
|
+
} : {
|
|
27
62
|
'.js': [
|
|
28
63
|
'.node.js',
|
|
29
64
|
'.server.js',
|
|
@@ -64,7 +99,8 @@ const builderPluginAdapterBasic = (options)=>({
|
|
|
64
99
|
});
|
|
65
100
|
}
|
|
66
101
|
});
|
|
67
|
-
function applyNodeCompat(isServiceWorker, chain) {
|
|
102
|
+
function applyNodeCompat(isServiceWorker, chain, options = {}) {
|
|
103
|
+
const { includeNodeExtensions = true } = options;
|
|
68
104
|
const nodeExts = [
|
|
69
105
|
'.node.js',
|
|
70
106
|
'.node.jsx',
|
|
@@ -83,7 +119,7 @@ function applyNodeCompat(isServiceWorker, chain) {
|
|
|
83
119
|
'.worker.ts',
|
|
84
120
|
'.worker.tsx'
|
|
85
121
|
];
|
|
86
|
-
for (const ext of nodeExts)chain.resolve.extensions.prepend(ext);
|
|
122
|
+
if (includeNodeExtensions) for (const ext of nodeExts)chain.resolve.extensions.prepend(ext);
|
|
87
123
|
if (isServiceWorker) for (const ext of webWorkerExts)chain.resolve.extensions.prepend(ext);
|
|
88
124
|
}
|
|
89
125
|
export { builderPluginAdapterBasic };
|
|
@@ -94,9 +94,10 @@ function applyFilterEntriesBySSRConfig({ isProd, chain, appNormalizedConfig }) {
|
|
|
94
94
|
});
|
|
95
95
|
}
|
|
96
96
|
async function applySSRLoaderEntry(chain, optinos, isServer) {
|
|
97
|
-
const { appContext } = optinos;
|
|
97
|
+
const { appContext, normalizedConfig } = optinos;
|
|
98
98
|
const { internalDirectory } = appContext;
|
|
99
99
|
const { entrypoints } = appContext;
|
|
100
|
+
const isRsc = isUseRsc(normalizedConfig);
|
|
100
101
|
await Promise.all(entrypoints.map(async (entrypoint)=>{
|
|
101
102
|
const { entryName } = entrypoint;
|
|
102
103
|
const serverLoadersFile = getServerCombinedModuleFile(internalDirectory, entryName);
|
|
@@ -104,6 +105,7 @@ async function applySSRLoaderEntry(chain, optinos, isServer) {
|
|
|
104
105
|
await fs.access(serverLoadersFile, fs.constants.F_OK);
|
|
105
106
|
chain.entry(`${entryName}-server-loaders`).add(serverLoadersFile);
|
|
106
107
|
} catch (err) {}
|
|
108
|
+
else if (isRsc) chain.entry(`${entryName}-server-loaders`).add("data:text/javascript,export%20{};");
|
|
107
109
|
}));
|
|
108
110
|
}
|
|
109
111
|
function applySSRDataLoader(chain, options) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { provider } from "std-env";
|
|
2
|
+
import { createCloudflarePreset } from "./platforms/cloudflare.mjs";
|
|
2
3
|
import { createGhPagesPreset } from "./platforms/gh-pages.mjs";
|
|
3
4
|
import { createNetlifyPreset } from "./platforms/netlify.mjs";
|
|
4
5
|
import { createNodePreset } from "./platforms/node.mjs";
|
|
@@ -8,14 +9,18 @@ const deployPresets = {
|
|
|
8
9
|
node: createNodePreset,
|
|
9
10
|
vercel: createVercelPreset,
|
|
10
11
|
netlify: createNetlifyPreset,
|
|
11
|
-
ghPages: createGhPagesPreset
|
|
12
|
+
ghPages: createGhPagesPreset,
|
|
13
|
+
cloudflare: createCloudflarePreset
|
|
12
14
|
};
|
|
15
|
+
const getSupportedDeployTargets = ()=>Object.keys(deployPresets);
|
|
16
|
+
const isDeployTarget = (target)=>Object.prototype.hasOwnProperty.call(deployPresets, target);
|
|
17
|
+
const resolveDeployTarget = (modernConfig, envDeployTarget = process.env.MODERNJS_DEPLOY, detectedProvider = provider)=>modernConfig.deploy?.target || envDeployTarget || detectedProvider || 'node';
|
|
13
18
|
async function getDeployPreset(appContext, modernConfig, deployTarget, api) {
|
|
14
19
|
const { appDirectory, distDirectory, metaName } = appContext;
|
|
15
20
|
const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory, metaName);
|
|
16
21
|
const needModernServer = useSSR || useAPI || useWebServer;
|
|
22
|
+
if (!isDeployTarget(deployTarget)) throw new Error(`Unknown deploy target: '${deployTarget}'. deploy.target or MODERNJS_DEPLOY should be one of: ${getSupportedDeployTargets().join(', ')}.`);
|
|
17
23
|
const createPreset = deployPresets[deployTarget];
|
|
18
|
-
if (!createPreset) throw new Error(`Unknown deploy target: '${deployTarget}'. MODERNJS_DEPLOY should be 'node', 'vercel', or 'netlify'.`);
|
|
19
24
|
return createPreset({
|
|
20
25
|
appContext,
|
|
21
26
|
modernConfig,
|
|
@@ -26,12 +31,12 @@ async function getDeployPreset(appContext, modernConfig, deployTarget, api) {
|
|
|
26
31
|
const deploy = ()=>({
|
|
27
32
|
name: '@modern-js/plugin-deploy',
|
|
28
33
|
setup: (api)=>{
|
|
29
|
-
const deployTarget = process.env.MODERNJS_DEPLOY || provider || 'node';
|
|
30
34
|
api.deploy(async ()=>{
|
|
31
35
|
const appContext = api.getAppContext();
|
|
32
36
|
const { metaName } = appContext;
|
|
33
|
-
if ('modern-js' !== metaName && !process.env.MODERNJS_DEPLOY) return;
|
|
34
37
|
const modernConfig = api.getNormalizedConfig();
|
|
38
|
+
const deployTarget = resolveDeployTarget(modernConfig);
|
|
39
|
+
if ('modern-js' !== metaName && !modernConfig.deploy?.target && !process.env.MODERNJS_DEPLOY) return;
|
|
35
40
|
const deployPreset = await getDeployPreset(appContext, modernConfig, deployTarget, api);
|
|
36
41
|
deployPreset?.prepare && await deployPreset?.prepare();
|
|
37
42
|
deployPreset?.writeOutput && await deployPreset?.writeOutput();
|
|
@@ -41,3 +46,4 @@ const deploy = ()=>({
|
|
|
41
46
|
}
|
|
42
47
|
});
|
|
43
48
|
export default deploy;
|
|
49
|
+
export { getSupportedDeployTargets, resolveDeployTarget };
|