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