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

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