@bleedingdev/modern-js-app-tools 3.2.0-ultramodern.12 → 3.2.0-ultramodern.121

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 (159) hide show
  1. package/bin/modern.js +0 -0
  2. package/dist/cjs/baseline.js +12 -50
  3. package/dist/cjs/builder/builder-rspack/index.js +9 -5
  4. package/dist/cjs/builder/generator/adapterCopy.js +9 -5
  5. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +9 -5
  6. package/dist/cjs/builder/generator/createCopyPattern.js +9 -5
  7. package/dist/cjs/builder/generator/getBuilderEnvironments.js +199 -12
  8. package/dist/cjs/builder/generator/index.js +9 -5
  9. package/dist/cjs/builder/index.js +9 -5
  10. package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +50 -10
  11. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +10 -6
  12. package/dist/cjs/builder/shared/builderPlugins/adapterPrecompress.js +9 -5
  13. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +12 -6
  14. package/dist/cjs/builder/shared/builderPlugins/builderHooks.js +12 -8
  15. package/dist/cjs/builder/shared/builderPlugins/index.js +9 -5
  16. package/dist/cjs/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +9 -5
  17. package/dist/cjs/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +12 -8
  18. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +9 -5
  19. package/dist/cjs/builder/shared/bundlerPlugins/index.js +9 -5
  20. package/dist/cjs/builder/shared/createCopyInfo.js +9 -5
  21. package/dist/cjs/builder/shared/index.js +9 -5
  22. package/dist/cjs/builder/shared/loaders/serverModuleLoader.js +12 -8
  23. package/dist/cjs/commands/build.js +9 -5
  24. package/dist/cjs/commands/deploy.js +9 -5
  25. package/dist/cjs/commands/dev.js +9 -5
  26. package/dist/cjs/commands/index.js +12 -11
  27. package/dist/cjs/commands/info.js +9 -5
  28. package/dist/cjs/commands/inspect.js +12 -8
  29. package/dist/cjs/commands/runtime.js +50 -13
  30. package/dist/cjs/commands/serve.js +9 -5
  31. package/dist/cjs/compat/hooks.js +9 -5
  32. package/dist/cjs/compat/index.js +9 -5
  33. package/dist/cjs/compat/utils.js +9 -5
  34. package/dist/cjs/config/default.js +9 -5
  35. package/dist/cjs/config/index.js +9 -5
  36. package/dist/cjs/config/initialize/index.js +9 -5
  37. package/dist/cjs/config/initialize/inits.js +9 -5
  38. package/dist/cjs/constants.js +13 -9
  39. package/dist/cjs/defineConfig.js +12 -8
  40. package/dist/cjs/esm/register-esm.js +12 -8
  41. package/dist/cjs/esm/ts-paths-loader.js +9 -5
  42. package/dist/cjs/index.js +22 -17
  43. package/dist/cjs/locale/en.js +12 -8
  44. package/dist/cjs/locale/index.js +9 -5
  45. package/dist/cjs/locale/zh.js +12 -8
  46. package/dist/cjs/plugins/analyze/constants.js +14 -10
  47. package/dist/cjs/plugins/analyze/getBundleEntry.js +9 -5
  48. package/dist/cjs/plugins/analyze/getFileSystemEntry.js +9 -5
  49. package/dist/cjs/plugins/analyze/getHtmlTemplate.js +9 -5
  50. package/dist/cjs/plugins/analyze/getServerRoutes.js +9 -5
  51. package/dist/cjs/plugins/analyze/index.js +9 -5
  52. package/dist/cjs/plugins/analyze/isDefaultExportFunction.js +9 -5
  53. package/dist/cjs/plugins/analyze/templates.js +12 -8
  54. package/dist/cjs/plugins/analyze/utils.js +9 -5
  55. package/dist/cjs/plugins/deploy/index.js +26 -11
  56. package/dist/cjs/plugins/deploy/platforms/cloudflare.js +400 -0
  57. package/dist/cjs/plugins/deploy/platforms/gh-pages.js +9 -5
  58. package/dist/cjs/plugins/deploy/platforms/netlify.js +9 -5
  59. package/dist/cjs/plugins/deploy/platforms/node.js +9 -5
  60. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +563 -0
  61. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-worker-fs-promises.mjs +7 -0
  62. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-worker-loadable-server.mjs +185 -0
  63. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-worker-path.mjs +59 -0
  64. package/dist/cjs/plugins/deploy/platforms/vercel.js +9 -5
  65. package/dist/cjs/plugins/deploy/utils/generator.js +9 -5
  66. package/dist/cjs/plugins/deploy/utils/index.js +19 -40
  67. package/dist/cjs/plugins/initialize/index.js +9 -5
  68. package/dist/cjs/plugins/serverBuild.js +9 -5
  69. package/dist/cjs/plugins/serverRuntime.js +12 -8
  70. package/dist/cjs/presetUltramodern.js +95 -7
  71. package/dist/cjs/rsbuild.js +55 -64
  72. package/dist/cjs/run/index.js +9 -5
  73. package/dist/cjs/types/config/cloudflareDeploy.js +18 -0
  74. package/dist/cjs/types/config/index.js +9 -5
  75. package/dist/cjs/types/config/precompress.js +18 -0
  76. package/dist/cjs/types/index.js +9 -5
  77. package/dist/cjs/ultramodern/designSystem.js +16 -12
  78. package/dist/cjs/utils/config.js +9 -5
  79. package/dist/cjs/utils/createServer.js +14 -10
  80. package/dist/cjs/utils/env.js +9 -5
  81. package/dist/cjs/utils/generateWatchFiles.js +9 -5
  82. package/dist/cjs/utils/getConfigFile.js +9 -5
  83. package/dist/cjs/utils/getSelectedEntries.js +9 -5
  84. package/dist/cjs/utils/initAppContext.js +9 -5
  85. package/dist/cjs/utils/loadPlugins.js +9 -5
  86. package/dist/cjs/utils/printInstructions.js +9 -5
  87. package/dist/cjs/utils/register.js +9 -5
  88. package/dist/cjs/utils/restart.js +9 -5
  89. package/dist/cjs/utils/routes.js +9 -5
  90. package/dist/esm/baseline.mjs +3 -45
  91. package/dist/esm/builder/generator/getBuilderEnvironments.mjs +180 -8
  92. package/dist/esm/builder/shared/builderPlugins/adapterBasic.mjs +41 -5
  93. package/dist/esm/builder/shared/builderPlugins/adapterHtml.mjs +1 -1
  94. package/dist/esm/builder/shared/builderPlugins/adapterSSR.mjs +3 -1
  95. package/dist/esm/commands/index.mjs +2 -5
  96. package/dist/esm/commands/runtime.mjs +33 -3
  97. package/dist/esm/index.mjs +2 -2
  98. package/dist/esm/plugins/deploy/index.mjs +10 -4
  99. package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +352 -0
  100. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +563 -0
  101. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-worker-fs-promises.mjs +7 -0
  102. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-worker-loadable-server.mjs +185 -0
  103. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-worker-path.mjs +59 -0
  104. package/dist/esm/plugins/deploy/utils/index.mjs +11 -36
  105. package/dist/esm/presetUltramodern.mjs +77 -3
  106. package/dist/esm/rsbuild.mjs +4 -8
  107. package/dist/esm/types/config/cloudflareDeploy.mjs +0 -0
  108. package/dist/esm/types/config/precompress.mjs +0 -0
  109. package/dist/esm-node/baseline.mjs +3 -45
  110. package/dist/esm-node/builder/generator/getBuilderEnvironments.mjs +185 -9
  111. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.mjs +41 -5
  112. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.mjs +1 -1
  113. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.mjs +3 -1
  114. package/dist/esm-node/commands/index.mjs +2 -5
  115. package/dist/esm-node/commands/runtime.mjs +33 -3
  116. package/dist/esm-node/index.mjs +2 -2
  117. package/dist/esm-node/plugins/deploy/index.mjs +10 -4
  118. package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +353 -0
  119. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +563 -0
  120. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-worker-fs-promises.mjs +7 -0
  121. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-worker-loadable-server.mjs +185 -0
  122. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-worker-path.mjs +59 -0
  123. package/dist/esm-node/plugins/deploy/utils/index.mjs +12 -36
  124. package/dist/esm-node/presetUltramodern.mjs +77 -3
  125. package/dist/esm-node/rsbuild.mjs +4 -8
  126. package/dist/esm-node/types/config/cloudflareDeploy.mjs +1 -0
  127. package/dist/esm-node/types/config/precompress.mjs +1 -0
  128. package/dist/types/baseline.d.ts +16 -46
  129. package/dist/types/builder/builder-rspack/index.d.ts +1 -1
  130. package/dist/types/builder/generator/index.d.ts +1 -1
  131. package/dist/types/builder/shared/createCopyInfo.d.ts +1 -1
  132. package/dist/types/commands/index.d.ts +1 -1
  133. package/dist/types/commands/inspect.d.ts +1 -1
  134. package/dist/types/commands/runtime.d.ts +1 -0
  135. package/dist/types/locale/en.d.ts +1 -1
  136. package/dist/types/locale/index.d.ts +89 -2
  137. package/dist/types/locale/zh.d.ts +1 -1
  138. package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +2 -2
  139. package/dist/types/plugins/analyze/utils.d.ts +1 -1
  140. package/dist/types/plugins/deploy/index.d.ts +4 -1
  141. package/dist/types/plugins/deploy/platforms/cloudflare.d.ts +2 -0
  142. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.d.mts +4 -0
  143. package/dist/types/plugins/deploy/platforms/templates/cloudflare-worker-fs-promises.d.mts +5 -0
  144. package/dist/types/plugins/deploy/platforms/templates/cloudflare-worker-loadable-server.d.mts +48 -0
  145. package/dist/types/plugins/deploy/platforms/templates/cloudflare-worker-path.d.mts +21 -0
  146. package/dist/types/plugins/deploy/utils/generator.d.ts +2 -2
  147. package/dist/types/plugins/deploy/utils/index.d.ts +1 -1
  148. package/dist/types/presetUltramodern.d.ts +52 -2
  149. package/dist/types/rsbuild.d.ts +1 -1
  150. package/dist/types/run/index.d.ts +1 -1
  151. package/dist/types/types/config/cloudflareDeploy.d.ts +79 -0
  152. package/dist/types/types/config/deploy.d.ts +16 -14
  153. package/dist/types/types/config/output.d.ts +4 -20
  154. package/dist/types/types/config/precompress.d.ts +20 -0
  155. package/dist/types/utils/getConfigFile.d.ts +1 -1
  156. package/dist/types/utils/loadPlugins.d.ts +2 -2
  157. package/package.json +21 -22
  158. package/dist/esm/rslib-runtime.mjs +0 -18
  159. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -0,0 +1,353 @@
1
+ import "node:module";
2
+ import node_path from "node:path";
3
+ import { fs } from "@modern-js/utils";
4
+ import { readTemplate } from "../utils/index.mjs";
5
+ const WORKER_ENTRY = 'server/index.mjs';
6
+ const WORKER_MANIFEST = 'server/modern-worker-manifest.json';
7
+ const ASSETS_BINDING = 'ASSETS';
8
+ const ROUTE_SPEC_FILE = 'route.json';
9
+ const ROUTE_SPEC_OUTPUT = `server/${ROUTE_SPEC_FILE}`;
10
+ const LOADABLE_STATS_FILE = 'loadable-stats.json';
11
+ const ROUTE_MANIFEST_FILE = 'routes-manifest.json';
12
+ const PUBLIC_ASSETS_DIRECTORY = 'public';
13
+ const WORKER_BUNDLE_DIRECTORY = 'worker';
14
+ const SERVER_BUNDLE_DIRECTORY = 'bundles';
15
+ const BFF_EFFECT_WORKER_ENTRY = `${WORKER_BUNDLE_DIRECTORY}/__modern_bff_effect.js`;
16
+ const DEFAULT_COMPATIBILITY_DATE = '2026-06-02';
17
+ const COMPATIBILITY_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/u;
18
+ const DEFAULT_SECURITY_HEADERS = {
19
+ referrerPolicy: 'strict-origin-when-cross-origin',
20
+ contentTypeOptions: 'nosniff',
21
+ permissionsPolicy: 'camera=(), geolocation=(), microphone=(), payment=(), usb=()'
22
+ };
23
+ const DEFAULT_CORS_ALLOWED_METHODS = [
24
+ 'GET',
25
+ 'HEAD',
26
+ 'POST',
27
+ 'PUT',
28
+ 'PATCH',
29
+ 'DELETE',
30
+ 'OPTIONS'
31
+ ];
32
+ const DEFAULT_CSP_DIRECTIVES = {
33
+ 'base-uri': [
34
+ "'self'"
35
+ ],
36
+ 'connect-src': [
37
+ "'self'",
38
+ 'https:',
39
+ 'http:',
40
+ 'wss:',
41
+ 'ws:'
42
+ ],
43
+ 'default-src': [
44
+ "'self'"
45
+ ],
46
+ 'font-src': [
47
+ "'self'",
48
+ 'data:',
49
+ 'https:',
50
+ 'http:'
51
+ ],
52
+ 'form-action': [
53
+ "'self'"
54
+ ],
55
+ 'frame-ancestors': [
56
+ "'self'"
57
+ ],
58
+ 'img-src': [
59
+ "'self'",
60
+ 'data:',
61
+ 'blob:',
62
+ 'https:',
63
+ 'http:'
64
+ ],
65
+ 'manifest-src': [
66
+ "'self'",
67
+ 'https:',
68
+ 'http:'
69
+ ],
70
+ 'object-src': [
71
+ "'none'"
72
+ ],
73
+ "script-src": [
74
+ "'self'",
75
+ "'unsafe-inline'",
76
+ "'unsafe-eval'",
77
+ 'https:',
78
+ 'http:',
79
+ 'blob:'
80
+ ],
81
+ 'style-src': [
82
+ "'self'",
83
+ "'unsafe-inline'",
84
+ 'https:',
85
+ 'http:'
86
+ ],
87
+ 'worker-src': [
88
+ "'self'",
89
+ 'blob:'
90
+ ]
91
+ };
92
+ const getCompatibilityDate = (modernConfig)=>{
93
+ const configuredDate = modernConfig.deploy?.worker?.compatibilityDate?.trim();
94
+ const compatibilityDate = configuredDate || DEFAULT_COMPATIBILITY_DATE;
95
+ if (!COMPATIBILITY_DATE_PATTERN.test(compatibilityDate)) throw new Error(`deploy.worker.compatibilityDate must use YYYY-MM-DD, received ${JSON.stringify(compatibilityDate)}.`);
96
+ return compatibilityDate;
97
+ };
98
+ const getWorkerName = (appDirectory)=>{
99
+ const basename = node_path.basename(appDirectory);
100
+ return basename.replace(/[^a-zA-Z0-9-_]/g, '-') || 'modern-cloudflare-worker';
101
+ };
102
+ const getConfiguredWorkerName = (appDirectory, modernConfig)=>{
103
+ const configuredName = modernConfig.deploy?.worker?.name?.trim();
104
+ return configuredName || getWorkerName(appDirectory);
105
+ };
106
+ const normalizeDirectiveValues = (value)=>{
107
+ const values = Array.isArray(value) ? value : [
108
+ value
109
+ ];
110
+ return [
111
+ ...new Set(values.map((entry)=>entry.trim()).filter(Boolean))
112
+ ];
113
+ };
114
+ const appendDirectiveValues = (directives, name, values)=>{
115
+ if (!values?.length) return;
116
+ directives[name] = normalizeDirectiveValues([
117
+ ...directives[name] ?? [],
118
+ ...values
119
+ ]);
120
+ };
121
+ const createContentSecurityPolicy = (config)=>{
122
+ const mode = config?.mode ?? 'report-only';
123
+ if ('off' === mode) return {
124
+ mode,
125
+ directives: {},
126
+ reason: config?.reason
127
+ };
128
+ const directives = Object.fromEntries(Object.entries(DEFAULT_CSP_DIRECTIVES).map(([name, values])=>[
129
+ name,
130
+ [
131
+ ...values
132
+ ]
133
+ ]));
134
+ for (const [name, value] of Object.entries(config?.directives ?? {}))if (false === value) delete directives[name];
135
+ else directives[name] = normalizeDirectiveValues(value);
136
+ if (config?.frameAncestors === false) delete directives['frame-ancestors'];
137
+ else if (config?.frameAncestors) directives['frame-ancestors'] = normalizeDirectiveValues(config.frameAncestors);
138
+ appendDirectiveValues(directives, "script-src", config?.additionalScriptSrc);
139
+ appendDirectiveValues(directives, 'style-src', config?.additionalStyleSrc);
140
+ appendDirectiveValues(directives, 'connect-src', config?.additionalConnectSrc);
141
+ appendDirectiveValues(directives, 'img-src', config?.additionalImgSrc);
142
+ if (config?.reportUri) directives['report-uri'] = [
143
+ config.reportUri
144
+ ];
145
+ return {
146
+ mode,
147
+ directives,
148
+ reason: config?.reason
149
+ };
150
+ };
151
+ const createNoindexPolicy = (noindex)=>{
152
+ if (false === noindex) return {
153
+ workersDev: false,
154
+ localhost: false,
155
+ previewHostnames: []
156
+ };
157
+ if (true === noindex || void 0 === noindex) return {
158
+ workersDev: true,
159
+ localhost: true,
160
+ previewHostnames: []
161
+ };
162
+ return {
163
+ workersDev: noindex.workersDev ?? true,
164
+ localhost: noindex.localhost ?? true,
165
+ previewHostnames: noindex.previewHostnames ?? [],
166
+ reason: noindex.reason
167
+ };
168
+ };
169
+ const createCloudflareWorkerCorsPolicy = (cors)=>({
170
+ assets: cors?.assets ?? true,
171
+ allowedOrigins: normalizeDirectiveValues(cors?.allowedOrigins ?? []),
172
+ allowedMethods: cors?.allowedMethods?.length ? normalizeDirectiveValues(cors.allowedMethods.map((method)=>method.toUpperCase())) : DEFAULT_CORS_ALLOWED_METHODS,
173
+ allowedHeaders: cors?.allowedHeaders?.length ? normalizeDirectiveValues(cors.allowedHeaders) : [
174
+ '*'
175
+ ],
176
+ reason: cors?.reason
177
+ });
178
+ const createCloudflareWorkerSecurityPolicy = (modernConfig)=>{
179
+ const security = modernConfig.deploy?.worker?.security;
180
+ if (security?.enabled === false) return {
181
+ enabled: false,
182
+ cors: createCloudflareWorkerCorsPolicy(security.cors),
183
+ reason: security.reason
184
+ };
185
+ return {
186
+ enabled: true,
187
+ headers: {
188
+ referrerPolicy: security?.headers?.referrerPolicy ?? DEFAULT_SECURITY_HEADERS.referrerPolicy,
189
+ contentTypeOptions: security?.headers?.contentTypeOptions ?? DEFAULT_SECURITY_HEADERS.contentTypeOptions,
190
+ permissionsPolicy: security?.headers?.permissionsPolicy ?? DEFAULT_SECURITY_HEADERS.permissionsPolicy
191
+ },
192
+ contentSecurityPolicy: createContentSecurityPolicy(security?.contentSecurityPolicy),
193
+ noindex: createNoindexPolicy(security?.noindex),
194
+ cors: createCloudflareWorkerCorsPolicy(security?.cors),
195
+ reason: security?.reason
196
+ };
197
+ };
198
+ const readRouteSpec = async (outputDirectory)=>{
199
+ const routeSpecPath = node_path.join(outputDirectory, ROUTE_SPEC_OUTPUT);
200
+ if (!await fs.pathExists(routeSpecPath)) return {
201
+ routes: []
202
+ };
203
+ const routeSpec = await fs.readJSON(routeSpecPath);
204
+ return {
205
+ ...routeSpec,
206
+ routes: Array.isArray(routeSpec.routes) ? routeSpec.routes : []
207
+ };
208
+ };
209
+ const createWorkerManifest = async (outputDirectory, modernConfig)=>{
210
+ const routeSpec = await readRouteSpec(outputDirectory);
211
+ const routes = await Promise.all(routeSpec.routes.map(async (route)=>{
212
+ const worker = 'string' == typeof route.worker ? route.worker : void 0;
213
+ return {
214
+ urlPath: route.urlPath,
215
+ entryName: route.entryName,
216
+ entryPath: route.entryPath,
217
+ isSSR: Boolean(route.isSSR),
218
+ worker,
219
+ workerExists: worker ? await fs.pathExists(node_path.join(outputDirectory, worker)) : false
220
+ };
221
+ }));
222
+ const bffPrefix = modernConfig.bff?.prefix;
223
+ const primaryBffPrefix = Array.isArray(bffPrefix) ? bffPrefix[0] : bffPrefix;
224
+ const isEffectBff = Boolean(modernConfig.bff) && modernConfig.bff?.runtimeFramework !== 'hono';
225
+ const effectBffWorkerExists = await fs.pathExists(node_path.join(outputDirectory, BFF_EFFECT_WORKER_ENTRY));
226
+ return {
227
+ version: 1,
228
+ runtime: {
229
+ type: 'cloudflare-module-worker',
230
+ entry: WORKER_ENTRY,
231
+ fetchExport: true,
232
+ nodeListen: false
233
+ },
234
+ assets: {
235
+ binding: ASSETS_BINDING,
236
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
237
+ runWorkerFirst: true
238
+ },
239
+ routeSpec: {
240
+ file: ROUTE_SPEC_OUTPUT,
241
+ routes
242
+ },
243
+ workerBundles: {
244
+ directory: WORKER_BUNDLE_DIRECTORY,
245
+ format: 'commonjs',
246
+ importableFromModuleWorker: true,
247
+ requestHandlerExport: 'requestHandler'
248
+ },
249
+ resources: {
250
+ loadableStats: LOADABLE_STATS_FILE,
251
+ routeManifest: ROUTE_MANIFEST_FILE
252
+ },
253
+ security: createCloudflareWorkerSecurityPolicy(modernConfig),
254
+ bff: isEffectBff && primaryBffPrefix && effectBffWorkerExists ? {
255
+ runtimeFramework: 'effect',
256
+ prefix: primaryBffPrefix,
257
+ worker: BFF_EFFECT_WORKER_ENTRY
258
+ } : void 0
259
+ };
260
+ };
261
+ const createWorkerModuleLoaders = (manifest)=>{
262
+ const imports = new Map();
263
+ for (const route of manifest.routeSpec.routes)if (route.worker && route.workerExists) {
264
+ const importPath = `../${String(route.worker).replace(/^\/+/u, '')}`;
265
+ imports.set(route.worker, `() => import(${JSON.stringify(importPath)})`);
266
+ }
267
+ if (manifest.bff?.worker) {
268
+ const importPath = `../${String(manifest.bff.worker).replace(/^\/+/u, '')}`;
269
+ imports.set(manifest.bff.worker, `() => import(${JSON.stringify(importPath)})`);
270
+ }
271
+ if (0 === imports.size) return '{}';
272
+ const loaders = [
273
+ ...imports.entries()
274
+ ].map(([worker, loader])=>`${JSON.stringify(worker)}: ${loader}`);
275
+ return `{\n${loaders.join(',\n')}\n}`;
276
+ };
277
+ const shouldCopyToPublicAssets = (src, distDirectory)=>{
278
+ const relativePath = node_path.relative(distDirectory, src);
279
+ if (!relativePath) return true;
280
+ const normalizedRelativePath = relativePath.replace(/\\/g, '/');
281
+ const [topLevelDirectory] = normalizedRelativePath.split('/');
282
+ return normalizedRelativePath !== ROUTE_SPEC_FILE && topLevelDirectory !== WORKER_BUNDLE_DIRECTORY && topLevelDirectory !== SERVER_BUNDLE_DIRECTORY;
283
+ };
284
+ const shouldCopyToWorkerBundle = (src, workerBundleDirectory)=>{
285
+ const relativePath = node_path.relative(workerBundleDirectory, src);
286
+ if (!relativePath) return true;
287
+ const normalizedRelativePath = relativePath.replace(/\\/g, '/');
288
+ const basename = node_path.basename(normalizedRelativePath);
289
+ if (basename.startsWith('.') || normalizedRelativePath.includes('/.')) return false;
290
+ if (fs.statSync(src).isDirectory()) return true;
291
+ return [
292
+ '.cjs',
293
+ '.js',
294
+ '.mjs'
295
+ ].includes(node_path.extname(normalizedRelativePath));
296
+ };
297
+ const createCloudflarePreset = ({ appContext, modernConfig })=>{
298
+ const { appDirectory, distDirectory } = appContext;
299
+ const outputDirectory = node_path.join(appDirectory, '.output');
300
+ const publicDirectory = node_path.join(outputDirectory, PUBLIC_ASSETS_DIRECTORY);
301
+ const workerEntryPath = node_path.join(outputDirectory, WORKER_ENTRY);
302
+ const workerManifestPath = node_path.join(outputDirectory, WORKER_MANIFEST);
303
+ const routeSpecOutputPath = node_path.join(outputDirectory, ROUTE_SPEC_OUTPUT);
304
+ const wranglerConfigPath = node_path.join(outputDirectory, 'wrangler.json');
305
+ const workerName = getConfiguredWorkerName(appDirectory, modernConfig);
306
+ return {
307
+ async prepare () {
308
+ await fs.remove(outputDirectory);
309
+ },
310
+ async writeOutput () {
311
+ await fs.copy(distDirectory, publicDirectory, {
312
+ filter: (src)=>shouldCopyToPublicAssets(src, distDirectory)
313
+ });
314
+ await fs.ensureDir(node_path.dirname(workerEntryPath));
315
+ await fs.ensureDir(node_path.dirname(workerManifestPath));
316
+ const routeSpecSourcePath = node_path.join(distDirectory, ROUTE_SPEC_FILE);
317
+ if (await fs.pathExists(routeSpecSourcePath)) await fs.copy(routeSpecSourcePath, routeSpecOutputPath);
318
+ const workerBundleSourceDirectory = node_path.join(distDirectory, WORKER_BUNDLE_DIRECTORY);
319
+ if (await fs.pathExists(workerBundleSourceDirectory)) await fs.copy(workerBundleSourceDirectory, node_path.join(outputDirectory, WORKER_BUNDLE_DIRECTORY), {
320
+ filter: (src)=>shouldCopyToWorkerBundle(src, workerBundleSourceDirectory)
321
+ });
322
+ await fs.writeJSON(wranglerConfigPath, {
323
+ $schema: 'node_modules/wrangler/config-schema.json',
324
+ name: workerName,
325
+ main: WORKER_ENTRY,
326
+ compatibility_date: getCompatibilityDate(modernConfig),
327
+ compatibility_flags: [
328
+ 'nodejs_compat',
329
+ 'global_fetch_strictly_public'
330
+ ],
331
+ assets: {
332
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
333
+ binding: ASSETS_BINDING,
334
+ run_worker_first: true
335
+ }
336
+ }, {
337
+ spaces: 2
338
+ });
339
+ await fs.writeJSON(workerManifestPath, await createWorkerManifest(outputDirectory, modernConfig), {
340
+ spaces: 2
341
+ });
342
+ await fs.writeJSON(node_path.join(outputDirectory, 'package.json'), {
343
+ type: 'commonjs'
344
+ });
345
+ },
346
+ async genEntry () {
347
+ const template = await readTemplate('cloudflare-entry.mjs');
348
+ const manifest = await fs.readJSON(workerManifestPath);
349
+ await fs.writeFile(workerEntryPath, template.replace('p_workerManifest', JSON.stringify(manifest, null, 2)).replace('p_workerModuleLoaders', createWorkerModuleLoaders(manifest)));
350
+ }
351
+ };
352
+ };
353
+ export { createCloudflarePreset };