@bleedingdev/modern-js-ultramodern-app-tools 3.9.0-ultramodern.11 → 3.9.0-ultramodern.13

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.
@@ -46,10 +46,12 @@ __webpack_require__.d(__webpack_exports__, {
46
46
  ultramodernReleaseEnvelopePlugin: ()=>external_release_envelope_plugin_js_namespaceObject.ultramodernReleaseEnvelopePlugin
47
47
  });
48
48
  const app_tools_namespaceObject = require("@modern-js/app-tools");
49
+ const builder_namespaceObject = require("@modern-js/app-tools/builder");
49
50
  const backend_federation_build_namespaceObject = require("@modern-js/app-tools-extensions/backend-federation-build");
50
51
  var backend_federation_build_default = /*#__PURE__*/ __webpack_require__.n(backend_federation_build_namespaceObject);
51
52
  const cloudflare_builder_namespaceObject = require("@modern-js/app-tools-extensions/cloudflare-builder");
52
53
  const plugin_namespaceObject = require("@modern-js/app-tools-extensions/deploy-output/plugin");
54
+ const target_namespaceObject = require("@modern-js/app-tools-extensions/deploy-output/target");
53
55
  const policy_defaults_namespaceObject = require("@modern-js/app-tools-extensions/policy-defaults");
54
56
  const runtime_package_resolution_namespaceObject = require("@modern-js/app-tools-extensions/runtime-package-resolution");
55
57
  const i18n_integration_namespaceObject = require("@modern-js/i18n-integration");
@@ -58,6 +60,28 @@ const external_router_integration_plugin_js_namespaceObject = require("./router-
58
60
  const external_rsc_disabled_plugin_js_namespaceObject = require("./rsc-disabled-plugin.js");
59
61
  const external_ssr_integration_plugin_js_namespaceObject = require("./ssr-integration-plugin.js");
60
62
  const external_preset_js_namespaceObject = require("./preset.js");
63
+ const headlessCloudflareWorkerPlugin = ()=>({
64
+ name: '@modern-js/headless-cloudflare-worker',
65
+ post: [
66
+ '@modern-js/ultramodern-release-envelope'
67
+ ],
68
+ setup (api) {
69
+ api.onAfterBuild(async ()=>{
70
+ const appContext = api.getAppContext();
71
+ const normalizedConfig = api.getNormalizedConfig();
72
+ if (!appContext.apiOnly || 'cloudflare' !== (0, target_namespaceObject.resolveDeployTarget)(normalizedConfig)) return;
73
+ const createBuilderForModern = await (0, builder_namespaceObject.createBuilderGenerator)();
74
+ const builder = await createBuilderForModern({
75
+ appContext,
76
+ normalizedConfig
77
+ });
78
+ builder.removePlugins([
79
+ 'builder-plugin-adapter-modern-ssr'
80
+ ]);
81
+ await builder.build();
82
+ });
83
+ }
84
+ });
61
85
  const ultramodernAppTools = ()=>({
62
86
  name: '@modern-js/ultramodern-app-tools',
63
87
  usePlugins: [
@@ -67,6 +91,7 @@ const ultramodernAppTools = ()=>({
67
91
  (0, external_ssr_integration_plugin_js_namespaceObject.ultramodernSSRIntegrationPlugin)(),
68
92
  backend_federation_build_default()(),
69
93
  (0, cloudflare_builder_namespaceObject.createCloudflareBuilderPlugin)(),
94
+ headlessCloudflareWorkerPlugin(),
70
95
  (0, plugin_namespaceObject.createDeployOutputAliasesPlugin)(),
71
96
  (0, external_release_envelope_plugin_js_namespaceObject.ultramodernReleaseEnvelopePlugin)()
72
97
  ],
@@ -48,27 +48,20 @@ const plugin_namespaceObject = require("@modern-js/app-tools-extensions/build-co
48
48
  const config_namespaceObject = require("@modern-js/app-tools-extensions/config");
49
49
  const native_type_checker_namespaceObject = require("@modern-js/app-tools-extensions/native-type-checker");
50
50
  const release_identity_namespaceObject = require("@modern-js/app-tools-extensions/release-identity");
51
+ const runtime_package_resolution_namespaceObject = require("@modern-js/app-tools-extensions/runtime-package-resolution");
51
52
  const cli_namespaceObject = require("@modern-js/plugin/cli");
52
53
  const core_namespaceObject = require("@rsbuild/core");
53
54
  const DEFAULT_OTLP_ENDPOINT = 'http://127.0.0.1:4318/v1/logs';
54
55
  const DEFAULT_VICTORIA_METRICS_ENDPOINT = 'http://127.0.0.1:8428/api/v1/import/prometheus';
55
56
  const resolveReactRouterPackageDir = (appDirectory)=>{
56
- const resolveNodeModulePackageJson = (packageName, fromDirectory)=>{
57
- let currentDirectory = external_node_path_default().resolve(fromDirectory);
58
- while(true){
59
- const packageJson = external_node_path_default().join(currentDirectory, 'node_modules', packageName, 'package.json');
60
- if (external_node_fs_default().existsSync(packageJson)) return external_node_fs_default().realpathSync(packageJson);
61
- const parentDirectory = external_node_path_default().dirname(currentDirectory);
62
- if (parentDirectory === currentDirectory) return;
63
- currentDirectory = parentDirectory;
64
- }
57
+ const findPackage = (name, from)=>{
58
+ const modules = (0, runtime_package_resolution_namespaceObject.findHostingModuleDirectory)(name, from);
59
+ return modules ? external_node_fs_default().realpathSync(external_node_path_default().join(modules, name)) : void 0;
65
60
  };
66
- const reactRouterPackageJson = resolveNodeModulePackageJson('react-router', appDirectory);
67
- if (reactRouterPackageJson) return external_node_path_default().dirname(reactRouterPackageJson);
68
- const reactRouterDomPackageJson = resolveNodeModulePackageJson('react-router-dom', appDirectory);
69
- if (!reactRouterDomPackageJson) return;
70
- const nestedReactRouterPackageJson = resolveNodeModulePackageJson('react-router', external_node_path_default().dirname(reactRouterDomPackageJson));
71
- if (nestedReactRouterPackageJson) return external_node_path_default().dirname(nestedReactRouterPackageJson);
61
+ const direct = findPackage('react-router', appDirectory);
62
+ if (direct) return direct;
63
+ const dom = findPackage('react-router-dom', appDirectory);
64
+ return dom ? findPackage('react-router', dom) : void 0;
72
65
  };
73
66
  const setReactRouterBridgeSafeAliases = (chain, { isProd })=>{
74
67
  const chainContext = chain.get('context');
@@ -27,95 +27,66 @@ var __webpack_require__ = {};
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
- const getRspackPlugins = (rspackConfig)=>{
31
- if (!rspackConfig) return [];
32
- const rspackEntries = Array.isArray(rspackConfig) ? rspackConfig : [
33
- rspackConfig
34
- ];
35
- const plugins = [];
36
- for (const entry of rspackEntries){
37
- if (!entry || 'function' == typeof entry || 'object' != typeof entry) continue;
38
- const maybePlugins = entry.plugins;
39
- if (Array.isArray(maybePlugins)) {
40
- for (const plugin of maybePlugins)if (plugin) plugins.push(plugin);
41
- }
42
- }
43
- return plugins;
44
- };
45
- const getRspackPluginName = (plugin)=>{
46
- if ('string' == typeof plugin) return plugin;
47
- if ('function' == typeof plugin) return plugin.name;
48
- if (Array.isArray(plugin)) {
49
- const [first] = plugin;
50
- if (!first) return;
51
- if ('string' == typeof first) return first;
52
- if ('function' == typeof first) return first.name;
53
- if ('object' == typeof first) return first.name || first.constructor?.name;
54
- return;
55
- }
56
- return plugin.name || plugin.constructor?.name;
57
- };
58
- const hasServerRenderingConfig = (userConfig)=>{
59
- const { output, server } = userConfig;
60
- if (output?.ssg) return true;
61
- if (output?.ssgByEntries && Object.keys(output.ssgByEntries).length > 0) return true;
62
- if (server?.ssr) return true;
63
- if (server?.ssrByEntries && Object.keys(server.ssrByEntries).length > 0) return true;
64
- return false;
65
- };
66
- const isModuleFederationAppSSREnabledInConfig = (ssr)=>{
67
- if (!ssr || 'object' != typeof ssr) return false;
68
- return true === ssr.moduleFederationAppSSR;
69
- };
70
- const isModuleFederationAppSSREnabled = (userConfig)=>{
71
- if (isModuleFederationAppSSREnabledInConfig(userConfig.server?.ssr)) return true;
72
- if (userConfig.server?.ssrByEntries && 'object' == typeof userConfig.server.ssrByEntries) return Object.values(userConfig.server.ssrByEntries).some(isModuleFederationAppSSREnabledInConfig);
73
- return false;
74
- };
75
- const isModuleFederationRspackPlugin = (plugin)=>{
76
- const candidate = getRspackPluginName(plugin);
77
- return 'string' == typeof candidate && /modulefederation/i.test(candidate);
78
- };
79
- const hasModuleFederationMarker = (config)=>{
80
- if ('true' === process.env.MF_SSR_PRJ) return true;
81
- const define = config.source?.define || {};
30
+ const hasModuleFederationMarker = (config, projectMarker)=>{
31
+ if (projectMarker) return true;
32
+ const define = config.source?.define ?? {};
82
33
  if ('REMOTE_IP_STRATEGY' in define || 'FEDERATION_IPV4' in define) return true;
83
- const plugins = getRspackPlugins(config.tools?.rspack);
84
- return plugins.some(isModuleFederationRspackPlugin);
34
+ const rspack = config.tools?.rspack;
35
+ return (Array.isArray(rspack) ? rspack : [
36
+ rspack
37
+ ]).some((entry)=>{
38
+ if (!entry || 'object' != typeof entry || !('plugins' in entry) || !Array.isArray(entry.plugins)) return false;
39
+ return entry.plugins.some((input)=>{
40
+ const plugin = Array.isArray(input) ? input[0] : input;
41
+ if (!plugin) return false;
42
+ const name = 'string' == typeof plugin ? plugin : 'function' == typeof plugin ? plugin.name : 'object' == typeof plugin ? plugin.name || plugin.constructor?.name : void 0;
43
+ return 'string' == typeof name && /modulefederation/i.test(name);
44
+ });
45
+ });
46
+ };
47
+ const normalizeSsrCapabilities = (config)=>{
48
+ const ssr = [
49
+ config.server?.ssr,
50
+ ...Object.values(config.server?.ssrByEntries ?? {})
51
+ ];
52
+ return {
53
+ rendering: Boolean(config.output?.ssg || Object.keys(config.output?.ssgByEntries ?? {}).length || config.server?.ssr || Object.keys(config.server?.ssrByEntries ?? {}).length),
54
+ federation: ssr.some((value)=>value && 'object' == typeof value && true === value.moduleFederationAppSSR),
55
+ worker: config.deploy?.target === 'cloudflare',
56
+ projectMarker: 'true' === process.env.MF_SSR_PRJ,
57
+ requireExplicit: 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT
58
+ };
85
59
  };
86
60
  const isNodeEnvironmentTarget = (target)=>'string' == typeof target && ('node' === target || 'async-node' === target || target.startsWith('node'));
87
- const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config);
61
+ const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config, 'true' === process.env.MF_SSR_PRJ);
88
62
  const ssrIntegrationBuilderPlugin = (modernAPI)=>({
89
63
  name: '@modern-js/ultramodern-builder-plugin-ssr',
90
64
  pre: [
91
65
  '@modern-js/builder-plugin-ssr'
92
66
  ],
93
67
  setup (api) {
68
+ const capabilities = normalizeSsrCapabilities(modernAPI.getNormalizedConfig());
94
69
  api.modifyEnvironmentConfig((config, { name, mergeEnvironmentConfig })=>{
95
70
  const isServerEnvironment = isNodeEnvironmentTarget(config.output.target) || 'workerSSR' === name;
96
- const userConfig = modernAPI.getNormalizedConfig();
97
- const hasServerRendering = hasServerRenderingConfig(userConfig);
98
- const hasModuleFederationRuntimeMarker = hasServerRendering && shouldUseModuleFederationNodeOutput(config);
99
- const hasExplicitMfSsrFlag = isModuleFederationAppSSREnabled(userConfig);
100
- const requireExplicitMfSsrFlag = 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT;
101
- if (hasServerRendering && hasModuleFederationRuntimeMarker && !hasExplicitMfSsrFlag) {
71
+ const hasModuleFederationRuntimeMarker = capabilities.rendering && isNodeEnvironmentTarget(config.output.target) && hasModuleFederationMarker(config, capabilities.projectMarker);
72
+ if (capabilities.rendering && hasModuleFederationRuntimeMarker && !capabilities.federation) {
102
73
  const warningMessage = '[modernjs][mf-ssr] Module Federation SSR was auto-detected from runtime markers. Set server.ssr.moduleFederationAppSSR=true explicitly in host and remotes to avoid heuristic drift.';
103
- if (requireExplicitMfSsrFlag) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
74
+ if (capabilities.requireExplicit) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
104
75
  console.warn(warningMessage);
105
76
  }
106
- const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag;
77
+ const isModuleFederationAppSSR = capabilities.rendering && capabilities.federation;
107
78
  return mergeEnvironmentConfig(config, {
108
79
  source: {
109
80
  define: {
110
81
  'process.env.MODERN_MF_APP_SSR': JSON.stringify(String(isModuleFederationAppSSR))
111
82
  }
112
83
  },
113
- ...'workerSSR' === name && userConfig.deploy?.target === 'cloudflare' ? {
84
+ ...'workerSSR' === name && capabilities.worker ? {
114
85
  output: {
115
86
  module: true
116
87
  }
117
88
  } : {},
118
- splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || hasExplicitMfSsrFlag) ? false : void 0
89
+ splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || capabilities.federation) ? false : void 0
119
90
  });
120
91
  });
121
92
  }
@@ -1,7 +1,9 @@
1
1
  import { appTools } from "@modern-js/app-tools";
2
+ import { createBuilderGenerator } from "@modern-js/app-tools/builder";
2
3
  import backend_federation_build from "@modern-js/app-tools-extensions/backend-federation-build";
3
4
  import { createCloudflareBuilderPlugin } from "@modern-js/app-tools-extensions/cloudflare-builder";
4
5
  import { createDeployOutputAliasesPlugin } from "@modern-js/app-tools-extensions/deploy-output/plugin";
6
+ import { resolveDeployTarget } from "@modern-js/app-tools-extensions/deploy-output/target";
5
7
  import { RENDERER_EXTENSIONS_PACKAGE, SERVER_EXTENSIONS_PLUGIN_NAME } from "@modern-js/app-tools-extensions/policy-defaults";
6
8
  import { collectRuntimePackageModuleDirectories, createRuntimePackageResolutionPlugin } from "@modern-js/app-tools-extensions/runtime-package-resolution";
7
9
  import { ultramodernI18nIntegrationPlugin } from "@modern-js/i18n-integration";
@@ -9,6 +11,28 @@ import { ultramodernReleaseEnvelopePlugin } from "./release-envelope-plugin.mjs"
9
11
  import { ultramodernRouterIntegrationPlugin } from "./router-integration-plugin.mjs";
10
12
  import { rscDisabledRuntimePlugin } from "./rsc-disabled-plugin.mjs";
11
13
  import { ultramodernSSRIntegrationPlugin } from "./ssr-integration-plugin.mjs";
14
+ const headlessCloudflareWorkerPlugin = ()=>({
15
+ name: '@modern-js/headless-cloudflare-worker',
16
+ post: [
17
+ '@modern-js/ultramodern-release-envelope'
18
+ ],
19
+ setup (api) {
20
+ api.onAfterBuild(async ()=>{
21
+ const appContext = api.getAppContext();
22
+ const normalizedConfig = api.getNormalizedConfig();
23
+ if (!appContext.apiOnly || 'cloudflare' !== resolveDeployTarget(normalizedConfig)) return;
24
+ const createBuilderForModern = await createBuilderGenerator();
25
+ const builder = await createBuilderForModern({
26
+ appContext,
27
+ normalizedConfig
28
+ });
29
+ builder.removePlugins([
30
+ 'builder-plugin-adapter-modern-ssr'
31
+ ]);
32
+ await builder.build();
33
+ });
34
+ }
35
+ });
12
36
  const ultramodernAppTools = ()=>({
13
37
  name: '@modern-js/ultramodern-app-tools',
14
38
  usePlugins: [
@@ -18,6 +42,7 @@ const ultramodernAppTools = ()=>({
18
42
  ultramodernSSRIntegrationPlugin(),
19
43
  backend_federation_build(),
20
44
  createCloudflareBuilderPlugin(),
45
+ headlessCloudflareWorkerPlugin(),
21
46
  createDeployOutputAliasesPlugin(),
22
47
  ultramodernReleaseEnvelopePlugin()
23
48
  ],
@@ -4,27 +4,20 @@ import { builderPluginAdapterPrecompress } from "@modern-js/app-tools-extensions
4
4
  import { resolveEffectTsgoCompiler } from "@modern-js/app-tools-extensions/config";
5
5
  import { configureUltramodernTypeChecker } from "@modern-js/app-tools-extensions/native-type-checker";
6
6
  import { resolveUltramodernReleaseIdentity } from "@modern-js/app-tools-extensions/release-identity";
7
+ import { findHostingModuleDirectory } from "@modern-js/app-tools-extensions/runtime-package-resolution";
7
8
  import { mergeConfig } from "@modern-js/plugin/cli";
8
9
  import { rspack } from "@rsbuild/core";
9
10
  const DEFAULT_OTLP_ENDPOINT = 'http://127.0.0.1:4318/v1/logs';
10
11
  const DEFAULT_VICTORIA_METRICS_ENDPOINT = 'http://127.0.0.1:8428/api/v1/import/prometheus';
11
12
  const resolveReactRouterPackageDir = (appDirectory)=>{
12
- const resolveNodeModulePackageJson = (packageName, fromDirectory)=>{
13
- let currentDirectory = node_path.resolve(fromDirectory);
14
- while(true){
15
- const packageJson = node_path.join(currentDirectory, 'node_modules', packageName, 'package.json');
16
- if (node_fs.existsSync(packageJson)) return node_fs.realpathSync(packageJson);
17
- const parentDirectory = node_path.dirname(currentDirectory);
18
- if (parentDirectory === currentDirectory) return;
19
- currentDirectory = parentDirectory;
20
- }
13
+ const findPackage = (name, from)=>{
14
+ const modules = findHostingModuleDirectory(name, from);
15
+ return modules ? node_fs.realpathSync(node_path.join(modules, name)) : void 0;
21
16
  };
22
- const reactRouterPackageJson = resolveNodeModulePackageJson('react-router', appDirectory);
23
- if (reactRouterPackageJson) return node_path.dirname(reactRouterPackageJson);
24
- const reactRouterDomPackageJson = resolveNodeModulePackageJson('react-router-dom', appDirectory);
25
- if (!reactRouterDomPackageJson) return;
26
- const nestedReactRouterPackageJson = resolveNodeModulePackageJson('react-router', node_path.dirname(reactRouterDomPackageJson));
27
- if (nestedReactRouterPackageJson) return node_path.dirname(nestedReactRouterPackageJson);
17
+ const direct = findPackage('react-router', appDirectory);
18
+ if (direct) return direct;
19
+ const dom = findPackage('react-router-dom', appDirectory);
20
+ return dom ? findPackage('react-router', dom) : void 0;
28
21
  };
29
22
  const setReactRouterBridgeSafeAliases = (chain, { isProd })=>{
30
23
  const chainContext = chain.get('context');
@@ -1,92 +1,63 @@
1
- const getRspackPlugins = (rspackConfig)=>{
2
- if (!rspackConfig) return [];
3
- const rspackEntries = Array.isArray(rspackConfig) ? rspackConfig : [
4
- rspackConfig
5
- ];
6
- const plugins = [];
7
- for (const entry of rspackEntries){
8
- if (!entry || 'function' == typeof entry || 'object' != typeof entry) continue;
9
- const maybePlugins = entry.plugins;
10
- if (Array.isArray(maybePlugins)) {
11
- for (const plugin of maybePlugins)if (plugin) plugins.push(plugin);
12
- }
13
- }
14
- return plugins;
15
- };
16
- const getRspackPluginName = (plugin)=>{
17
- if ('string' == typeof plugin) return plugin;
18
- if ('function' == typeof plugin) return plugin.name;
19
- if (Array.isArray(plugin)) {
20
- const [first] = plugin;
21
- if (!first) return;
22
- if ('string' == typeof first) return first;
23
- if ('function' == typeof first) return first.name;
24
- if ('object' == typeof first) return first.name || first.constructor?.name;
25
- return;
26
- }
27
- return plugin.name || plugin.constructor?.name;
28
- };
29
- const hasServerRenderingConfig = (userConfig)=>{
30
- const { output, server } = userConfig;
31
- if (output?.ssg) return true;
32
- if (output?.ssgByEntries && Object.keys(output.ssgByEntries).length > 0) return true;
33
- if (server?.ssr) return true;
34
- if (server?.ssrByEntries && Object.keys(server.ssrByEntries).length > 0) return true;
35
- return false;
36
- };
37
- const isModuleFederationAppSSREnabledInConfig = (ssr)=>{
38
- if (!ssr || 'object' != typeof ssr) return false;
39
- return true === ssr.moduleFederationAppSSR;
40
- };
41
- const isModuleFederationAppSSREnabled = (userConfig)=>{
42
- if (isModuleFederationAppSSREnabledInConfig(userConfig.server?.ssr)) return true;
43
- if (userConfig.server?.ssrByEntries && 'object' == typeof userConfig.server.ssrByEntries) return Object.values(userConfig.server.ssrByEntries).some(isModuleFederationAppSSREnabledInConfig);
44
- return false;
45
- };
46
- const isModuleFederationRspackPlugin = (plugin)=>{
47
- const candidate = getRspackPluginName(plugin);
48
- return 'string' == typeof candidate && /modulefederation/i.test(candidate);
49
- };
50
- const hasModuleFederationMarker = (config)=>{
51
- if ('true' === process.env.MF_SSR_PRJ) return true;
52
- const define = config.source?.define || {};
1
+ const hasModuleFederationMarker = (config, projectMarker)=>{
2
+ if (projectMarker) return true;
3
+ const define = config.source?.define ?? {};
53
4
  if ('REMOTE_IP_STRATEGY' in define || 'FEDERATION_IPV4' in define) return true;
54
- const plugins = getRspackPlugins(config.tools?.rspack);
55
- return plugins.some(isModuleFederationRspackPlugin);
5
+ const rspack = config.tools?.rspack;
6
+ return (Array.isArray(rspack) ? rspack : [
7
+ rspack
8
+ ]).some((entry)=>{
9
+ if (!entry || 'object' != typeof entry || !('plugins' in entry) || !Array.isArray(entry.plugins)) return false;
10
+ return entry.plugins.some((input)=>{
11
+ const plugin = Array.isArray(input) ? input[0] : input;
12
+ if (!plugin) return false;
13
+ const name = 'string' == typeof plugin ? plugin : 'function' == typeof plugin ? plugin.name : 'object' == typeof plugin ? plugin.name || plugin.constructor?.name : void 0;
14
+ return 'string' == typeof name && /modulefederation/i.test(name);
15
+ });
16
+ });
17
+ };
18
+ const normalizeSsrCapabilities = (config)=>{
19
+ const ssr = [
20
+ config.server?.ssr,
21
+ ...Object.values(config.server?.ssrByEntries ?? {})
22
+ ];
23
+ return {
24
+ rendering: Boolean(config.output?.ssg || Object.keys(config.output?.ssgByEntries ?? {}).length || config.server?.ssr || Object.keys(config.server?.ssrByEntries ?? {}).length),
25
+ federation: ssr.some((value)=>value && 'object' == typeof value && true === value.moduleFederationAppSSR),
26
+ worker: config.deploy?.target === 'cloudflare',
27
+ projectMarker: 'true' === process.env.MF_SSR_PRJ,
28
+ requireExplicit: 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT
29
+ };
56
30
  };
57
31
  const isNodeEnvironmentTarget = (target)=>'string' == typeof target && ('node' === target || 'async-node' === target || target.startsWith('node'));
58
- const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config);
32
+ const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config, 'true' === process.env.MF_SSR_PRJ);
59
33
  const ssrIntegrationBuilderPlugin = (modernAPI)=>({
60
34
  name: '@modern-js/ultramodern-builder-plugin-ssr',
61
35
  pre: [
62
36
  '@modern-js/builder-plugin-ssr'
63
37
  ],
64
38
  setup (api) {
39
+ const capabilities = normalizeSsrCapabilities(modernAPI.getNormalizedConfig());
65
40
  api.modifyEnvironmentConfig((config, { name, mergeEnvironmentConfig })=>{
66
41
  const isServerEnvironment = isNodeEnvironmentTarget(config.output.target) || 'workerSSR' === name;
67
- const userConfig = modernAPI.getNormalizedConfig();
68
- const hasServerRendering = hasServerRenderingConfig(userConfig);
69
- const hasModuleFederationRuntimeMarker = hasServerRendering && shouldUseModuleFederationNodeOutput(config);
70
- const hasExplicitMfSsrFlag = isModuleFederationAppSSREnabled(userConfig);
71
- const requireExplicitMfSsrFlag = 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT;
72
- if (hasServerRendering && hasModuleFederationRuntimeMarker && !hasExplicitMfSsrFlag) {
42
+ const hasModuleFederationRuntimeMarker = capabilities.rendering && isNodeEnvironmentTarget(config.output.target) && hasModuleFederationMarker(config, capabilities.projectMarker);
43
+ if (capabilities.rendering && hasModuleFederationRuntimeMarker && !capabilities.federation) {
73
44
  const warningMessage = '[modernjs][mf-ssr] Module Federation SSR was auto-detected from runtime markers. Set server.ssr.moduleFederationAppSSR=true explicitly in host and remotes to avoid heuristic drift.';
74
- if (requireExplicitMfSsrFlag) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
45
+ if (capabilities.requireExplicit) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
75
46
  console.warn(warningMessage);
76
47
  }
77
- const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag;
48
+ const isModuleFederationAppSSR = capabilities.rendering && capabilities.federation;
78
49
  return mergeEnvironmentConfig(config, {
79
50
  source: {
80
51
  define: {
81
52
  'process.env.MODERN_MF_APP_SSR': JSON.stringify(String(isModuleFederationAppSSR))
82
53
  }
83
54
  },
84
- ...'workerSSR' === name && userConfig.deploy?.target === 'cloudflare' ? {
55
+ ...'workerSSR' === name && capabilities.worker ? {
85
56
  output: {
86
57
  module: true
87
58
  }
88
59
  } : {},
89
- splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || hasExplicitMfSsrFlag) ? false : void 0
60
+ splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || capabilities.federation) ? false : void 0
90
61
  });
91
62
  });
92
63
  }
@@ -1,8 +1,10 @@
1
1
  import "node:module";
2
2
  import { appTools } from "@modern-js/app-tools";
3
+ import { createBuilderGenerator } from "@modern-js/app-tools/builder";
3
4
  import backend_federation_build from "@modern-js/app-tools-extensions/backend-federation-build";
4
5
  import { createCloudflareBuilderPlugin } from "@modern-js/app-tools-extensions/cloudflare-builder";
5
6
  import { createDeployOutputAliasesPlugin } from "@modern-js/app-tools-extensions/deploy-output/plugin";
7
+ import { resolveDeployTarget } from "@modern-js/app-tools-extensions/deploy-output/target";
6
8
  import { RENDERER_EXTENSIONS_PACKAGE, SERVER_EXTENSIONS_PLUGIN_NAME } from "@modern-js/app-tools-extensions/policy-defaults";
7
9
  import { collectRuntimePackageModuleDirectories, createRuntimePackageResolutionPlugin } from "@modern-js/app-tools-extensions/runtime-package-resolution";
8
10
  import { ultramodernI18nIntegrationPlugin } from "@modern-js/i18n-integration";
@@ -10,6 +12,28 @@ import { ultramodernReleaseEnvelopePlugin } from "./release-envelope-plugin.mjs"
10
12
  import { ultramodernRouterIntegrationPlugin } from "./router-integration-plugin.mjs";
11
13
  import { rscDisabledRuntimePlugin } from "./rsc-disabled-plugin.mjs";
12
14
  import { ultramodernSSRIntegrationPlugin } from "./ssr-integration-plugin.mjs";
15
+ const headlessCloudflareWorkerPlugin = ()=>({
16
+ name: '@modern-js/headless-cloudflare-worker',
17
+ post: [
18
+ '@modern-js/ultramodern-release-envelope'
19
+ ],
20
+ setup (api) {
21
+ api.onAfterBuild(async ()=>{
22
+ const appContext = api.getAppContext();
23
+ const normalizedConfig = api.getNormalizedConfig();
24
+ if (!appContext.apiOnly || 'cloudflare' !== resolveDeployTarget(normalizedConfig)) return;
25
+ const createBuilderForModern = await createBuilderGenerator();
26
+ const builder = await createBuilderForModern({
27
+ appContext,
28
+ normalizedConfig
29
+ });
30
+ builder.removePlugins([
31
+ 'builder-plugin-adapter-modern-ssr'
32
+ ]);
33
+ await builder.build();
34
+ });
35
+ }
36
+ });
13
37
  const ultramodernAppTools = ()=>({
14
38
  name: '@modern-js/ultramodern-app-tools',
15
39
  usePlugins: [
@@ -19,6 +43,7 @@ const ultramodernAppTools = ()=>({
19
43
  ultramodernSSRIntegrationPlugin(),
20
44
  backend_federation_build(),
21
45
  createCloudflareBuilderPlugin(),
46
+ headlessCloudflareWorkerPlugin(),
22
47
  createDeployOutputAliasesPlugin(),
23
48
  ultramodernReleaseEnvelopePlugin()
24
49
  ],
@@ -5,27 +5,20 @@ import { builderPluginAdapterPrecompress } from "@modern-js/app-tools-extensions
5
5
  import { resolveEffectTsgoCompiler } from "@modern-js/app-tools-extensions/config";
6
6
  import { configureUltramodernTypeChecker } from "@modern-js/app-tools-extensions/native-type-checker";
7
7
  import { resolveUltramodernReleaseIdentity } from "@modern-js/app-tools-extensions/release-identity";
8
+ import { findHostingModuleDirectory } from "@modern-js/app-tools-extensions/runtime-package-resolution";
8
9
  import { mergeConfig } from "@modern-js/plugin/cli";
9
10
  import { rspack } from "@rsbuild/core";
10
11
  const DEFAULT_OTLP_ENDPOINT = 'http://127.0.0.1:4318/v1/logs';
11
12
  const DEFAULT_VICTORIA_METRICS_ENDPOINT = 'http://127.0.0.1:8428/api/v1/import/prometheus';
12
13
  const resolveReactRouterPackageDir = (appDirectory)=>{
13
- const resolveNodeModulePackageJson = (packageName, fromDirectory)=>{
14
- let currentDirectory = node_path.resolve(fromDirectory);
15
- while(true){
16
- const packageJson = node_path.join(currentDirectory, 'node_modules', packageName, 'package.json');
17
- if (node_fs.existsSync(packageJson)) return node_fs.realpathSync(packageJson);
18
- const parentDirectory = node_path.dirname(currentDirectory);
19
- if (parentDirectory === currentDirectory) return;
20
- currentDirectory = parentDirectory;
21
- }
14
+ const findPackage = (name, from)=>{
15
+ const modules = findHostingModuleDirectory(name, from);
16
+ return modules ? node_fs.realpathSync(node_path.join(modules, name)) : void 0;
22
17
  };
23
- const reactRouterPackageJson = resolveNodeModulePackageJson('react-router', appDirectory);
24
- if (reactRouterPackageJson) return node_path.dirname(reactRouterPackageJson);
25
- const reactRouterDomPackageJson = resolveNodeModulePackageJson('react-router-dom', appDirectory);
26
- if (!reactRouterDomPackageJson) return;
27
- const nestedReactRouterPackageJson = resolveNodeModulePackageJson('react-router', node_path.dirname(reactRouterDomPackageJson));
28
- if (nestedReactRouterPackageJson) return node_path.dirname(nestedReactRouterPackageJson);
18
+ const direct = findPackage('react-router', appDirectory);
19
+ if (direct) return direct;
20
+ const dom = findPackage('react-router-dom', appDirectory);
21
+ return dom ? findPackage('react-router', dom) : void 0;
29
22
  };
30
23
  const setReactRouterBridgeSafeAliases = (chain, { isProd })=>{
31
24
  const chainContext = chain.get('context');
@@ -1,93 +1,64 @@
1
1
  import "node:module";
2
- const getRspackPlugins = (rspackConfig)=>{
3
- if (!rspackConfig) return [];
4
- const rspackEntries = Array.isArray(rspackConfig) ? rspackConfig : [
5
- rspackConfig
6
- ];
7
- const plugins = [];
8
- for (const entry of rspackEntries){
9
- if (!entry || 'function' == typeof entry || 'object' != typeof entry) continue;
10
- const maybePlugins = entry.plugins;
11
- if (Array.isArray(maybePlugins)) {
12
- for (const plugin of maybePlugins)if (plugin) plugins.push(plugin);
13
- }
14
- }
15
- return plugins;
16
- };
17
- const getRspackPluginName = (plugin)=>{
18
- if ('string' == typeof plugin) return plugin;
19
- if ('function' == typeof plugin) return plugin.name;
20
- if (Array.isArray(plugin)) {
21
- const [first] = plugin;
22
- if (!first) return;
23
- if ('string' == typeof first) return first;
24
- if ('function' == typeof first) return first.name;
25
- if ('object' == typeof first) return first.name || first.constructor?.name;
26
- return;
27
- }
28
- return plugin.name || plugin.constructor?.name;
29
- };
30
- const hasServerRenderingConfig = (userConfig)=>{
31
- const { output, server } = userConfig;
32
- if (output?.ssg) return true;
33
- if (output?.ssgByEntries && Object.keys(output.ssgByEntries).length > 0) return true;
34
- if (server?.ssr) return true;
35
- if (server?.ssrByEntries && Object.keys(server.ssrByEntries).length > 0) return true;
36
- return false;
37
- };
38
- const isModuleFederationAppSSREnabledInConfig = (ssr)=>{
39
- if (!ssr || 'object' != typeof ssr) return false;
40
- return true === ssr.moduleFederationAppSSR;
41
- };
42
- const isModuleFederationAppSSREnabled = (userConfig)=>{
43
- if (isModuleFederationAppSSREnabledInConfig(userConfig.server?.ssr)) return true;
44
- if (userConfig.server?.ssrByEntries && 'object' == typeof userConfig.server.ssrByEntries) return Object.values(userConfig.server.ssrByEntries).some(isModuleFederationAppSSREnabledInConfig);
45
- return false;
46
- };
47
- const isModuleFederationRspackPlugin = (plugin)=>{
48
- const candidate = getRspackPluginName(plugin);
49
- return 'string' == typeof candidate && /modulefederation/i.test(candidate);
50
- };
51
- const hasModuleFederationMarker = (config)=>{
52
- if ('true' === process.env.MF_SSR_PRJ) return true;
53
- const define = config.source?.define || {};
2
+ const hasModuleFederationMarker = (config, projectMarker)=>{
3
+ if (projectMarker) return true;
4
+ const define = config.source?.define ?? {};
54
5
  if ('REMOTE_IP_STRATEGY' in define || 'FEDERATION_IPV4' in define) return true;
55
- const plugins = getRspackPlugins(config.tools?.rspack);
56
- return plugins.some(isModuleFederationRspackPlugin);
6
+ const rspack = config.tools?.rspack;
7
+ return (Array.isArray(rspack) ? rspack : [
8
+ rspack
9
+ ]).some((entry)=>{
10
+ if (!entry || 'object' != typeof entry || !('plugins' in entry) || !Array.isArray(entry.plugins)) return false;
11
+ return entry.plugins.some((input)=>{
12
+ const plugin = Array.isArray(input) ? input[0] : input;
13
+ if (!plugin) return false;
14
+ const name = 'string' == typeof plugin ? plugin : 'function' == typeof plugin ? plugin.name : 'object' == typeof plugin ? plugin.name || plugin.constructor?.name : void 0;
15
+ return 'string' == typeof name && /modulefederation/i.test(name);
16
+ });
17
+ });
18
+ };
19
+ const normalizeSsrCapabilities = (config)=>{
20
+ const ssr = [
21
+ config.server?.ssr,
22
+ ...Object.values(config.server?.ssrByEntries ?? {})
23
+ ];
24
+ return {
25
+ rendering: Boolean(config.output?.ssg || Object.keys(config.output?.ssgByEntries ?? {}).length || config.server?.ssr || Object.keys(config.server?.ssrByEntries ?? {}).length),
26
+ federation: ssr.some((value)=>value && 'object' == typeof value && true === value.moduleFederationAppSSR),
27
+ worker: config.deploy?.target === 'cloudflare',
28
+ projectMarker: 'true' === process.env.MF_SSR_PRJ,
29
+ requireExplicit: 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT
30
+ };
57
31
  };
58
32
  const isNodeEnvironmentTarget = (target)=>'string' == typeof target && ('node' === target || 'async-node' === target || target.startsWith('node'));
59
- const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config);
33
+ const shouldUseModuleFederationNodeOutput = (config)=>isNodeEnvironmentTarget(config.output?.target) && hasModuleFederationMarker(config, 'true' === process.env.MF_SSR_PRJ);
60
34
  const ssrIntegrationBuilderPlugin = (modernAPI)=>({
61
35
  name: '@modern-js/ultramodern-builder-plugin-ssr',
62
36
  pre: [
63
37
  '@modern-js/builder-plugin-ssr'
64
38
  ],
65
39
  setup (api) {
40
+ const capabilities = normalizeSsrCapabilities(modernAPI.getNormalizedConfig());
66
41
  api.modifyEnvironmentConfig((config, { name, mergeEnvironmentConfig })=>{
67
42
  const isServerEnvironment = isNodeEnvironmentTarget(config.output.target) || 'workerSSR' === name;
68
- const userConfig = modernAPI.getNormalizedConfig();
69
- const hasServerRendering = hasServerRenderingConfig(userConfig);
70
- const hasModuleFederationRuntimeMarker = hasServerRendering && shouldUseModuleFederationNodeOutput(config);
71
- const hasExplicitMfSsrFlag = isModuleFederationAppSSREnabled(userConfig);
72
- const requireExplicitMfSsrFlag = 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT;
73
- if (hasServerRendering && hasModuleFederationRuntimeMarker && !hasExplicitMfSsrFlag) {
43
+ const hasModuleFederationRuntimeMarker = capabilities.rendering && isNodeEnvironmentTarget(config.output.target) && hasModuleFederationMarker(config, capabilities.projectMarker);
44
+ if (capabilities.rendering && hasModuleFederationRuntimeMarker && !capabilities.federation) {
74
45
  const warningMessage = '[modernjs][mf-ssr] Module Federation SSR was auto-detected from runtime markers. Set server.ssr.moduleFederationAppSSR=true explicitly in host and remotes to avoid heuristic drift.';
75
- if (requireExplicitMfSsrFlag) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
46
+ if (capabilities.requireExplicit) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
76
47
  console.warn(warningMessage);
77
48
  }
78
- const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag;
49
+ const isModuleFederationAppSSR = capabilities.rendering && capabilities.federation;
79
50
  return mergeEnvironmentConfig(config, {
80
51
  source: {
81
52
  define: {
82
53
  'process.env.MODERN_MF_APP_SSR': JSON.stringify(String(isModuleFederationAppSSR))
83
54
  }
84
55
  },
85
- ...'workerSSR' === name && userConfig.deploy?.target === 'cloudflare' ? {
56
+ ...'workerSSR' === name && capabilities.worker ? {
86
57
  output: {
87
58
  module: true
88
59
  }
89
60
  } : {},
90
- splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || hasExplicitMfSsrFlag) ? false : void 0
61
+ splitChunks: isServerEnvironment && (hasModuleFederationRuntimeMarker || capabilities.federation) ? false : void 0
91
62
  });
92
63
  });
93
64
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "directory": "packages/solutions/ultramodern-app-tools"
12
12
  },
13
13
  "license": "MIT",
14
- "version": "3.9.0-ultramodern.11",
14
+ "version": "3.9.0-ultramodern.13",
15
15
  "files": [
16
16
  "dist",
17
17
  "src"
@@ -62,29 +62,29 @@
62
62
  "node": ">=26.7.0"
63
63
  },
64
64
  "dependencies": {
65
- "@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.9.0-ultramodern.11",
66
- "@modern-js/app-tools-extensions": "npm:@bleedingdev/modern-js-app-tools-extensions@3.9.0-ultramodern.11",
67
- "@modern-js/i18n-integration": "npm:@bleedingdev/modern-js-i18n-integration@3.9.0-ultramodern.11",
68
- "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.9.0-ultramodern.11",
69
- "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.9.0-ultramodern.11",
70
- "@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.11",
71
- "@modern-js/runtime-renderer-extensions": "npm:@bleedingdev/modern-js-runtime-renderer-extensions@3.9.0-ultramodern.11",
72
- "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.9.0-ultramodern.11",
73
- "@modern-js/server-runtime-extensions": "npm:@bleedingdev/modern-js-server-runtime-extensions@3.9.0-ultramodern.11",
74
- "@rsbuild/core": "2.2.3"
65
+ "@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.9.0-ultramodern.13",
66
+ "@modern-js/app-tools-extensions": "npm:@bleedingdev/modern-js-app-tools-extensions@3.9.0-ultramodern.13",
67
+ "@modern-js/i18n-integration": "npm:@bleedingdev/modern-js-i18n-integration@3.9.0-ultramodern.13",
68
+ "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.9.0-ultramodern.13",
69
+ "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.9.0-ultramodern.13",
70
+ "@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.13",
71
+ "@modern-js/runtime-renderer-extensions": "npm:@bleedingdev/modern-js-runtime-renderer-extensions@3.9.0-ultramodern.13",
72
+ "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.9.0-ultramodern.13",
73
+ "@modern-js/server-runtime-extensions": "npm:@bleedingdev/modern-js-server-runtime-extensions@3.9.0-ultramodern.13",
74
+ "@rsbuild/core": "2.2.9"
75
75
  },
76
76
  "devDependencies": {
77
- "@modern-js/backend-federation-contracts": "npm:@bleedingdev/modern-js-backend-federation-contracts@3.9.0-ultramodern.11",
78
- "@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.9.0-ultramodern.11",
79
- "@modern-js/plugin-bff": "npm:@bleedingdev/modern-js-plugin-bff@3.9.0-ultramodern.11",
80
- "@modern-js/plugin-tanstack": "npm:@bleedingdev/modern-js-plugin-tanstack@3.9.0-ultramodern.11",
81
- "@modern-js/prod-server": "npm:@bleedingdev/modern-js-prod-server@3.9.0-ultramodern.11",
82
- "@modern-js/server": "npm:@bleedingdev/modern-js-server@3.9.0-ultramodern.11",
83
- "@modern-js/tsconfig": "npm:@bleedingdev/modern-js-tsconfig@3.9.0-ultramodern.11",
84
- "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.9.0-ultramodern.11",
85
- "@rslib/core": "1.0.0",
77
+ "@modern-js/backend-federation-contracts": "npm:@bleedingdev/modern-js-backend-federation-contracts@3.9.0-ultramodern.13",
78
+ "@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.9.0-ultramodern.13",
79
+ "@modern-js/plugin-bff": "npm:@bleedingdev/modern-js-plugin-bff@3.9.0-ultramodern.13",
80
+ "@modern-js/plugin-tanstack": "npm:@bleedingdev/modern-js-plugin-tanstack@3.9.0-ultramodern.13",
81
+ "@modern-js/prod-server": "npm:@bleedingdev/modern-js-prod-server@3.9.0-ultramodern.13",
82
+ "@modern-js/server": "npm:@bleedingdev/modern-js-server@3.9.0-ultramodern.13",
83
+ "@modern-js/tsconfig": "npm:@bleedingdev/modern-js-tsconfig@3.9.0-ultramodern.13",
84
+ "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.9.0-ultramodern.13",
85
+ "@rslib/core": "1.0.2",
86
86
  "@scripts/rstest-config": "2.66.0",
87
- "@types/node": "^26.4.1",
87
+ "@types/node": "^26.6.2",
88
88
  "@typescript/native-preview": "7.0.0-dev.20260707.2"
89
89
  },
90
90
  "sideEffects": false,
@@ -1,7 +1,9 @@
1
1
  import { type AppTools, appTools, type CliPlugin } from '@modern-js/app-tools';
2
+ import { createBuilderGenerator } from '@modern-js/app-tools/builder';
2
3
  import backendFederationBuildPlugin from '@modern-js/app-tools-extensions/backend-federation-build';
3
4
  import { createCloudflareBuilderPlugin } from '@modern-js/app-tools-extensions/cloudflare-builder';
4
5
  import { createDeployOutputAliasesPlugin } from '@modern-js/app-tools-extensions/deploy-output/plugin';
6
+ import { resolveDeployTarget } from '@modern-js/app-tools-extensions/deploy-output/target';
5
7
  import {
6
8
  RENDERER_EXTENSIONS_PACKAGE,
7
9
  SERVER_EXTENSIONS_PLUGIN_NAME,
@@ -24,6 +26,36 @@ export {
24
26
  export { ultramodernReleaseEnvelopePlugin } from './release-envelope-plugin';
25
27
  export type { AppUserConfig, UltramodernAppUserConfig } from './types';
26
28
 
29
+ const headlessCloudflareWorkerPlugin = (): CliPlugin<AppTools> => ({
30
+ name: '@modern-js/headless-cloudflare-worker',
31
+ post: ['@modern-js/ultramodern-release-envelope'],
32
+ setup(api) {
33
+ api.onAfterBuild(async () => {
34
+ const appContext = api.getAppContext();
35
+ const normalizedConfig = api.getNormalizedConfig();
36
+ if (
37
+ !appContext.apiOnly ||
38
+ resolveDeployTarget(normalizedConfig) !== 'cloudflare'
39
+ ) {
40
+ return;
41
+ }
42
+
43
+ // Native API-only builds intentionally skip their UI builder. Reuse the
44
+ // same builder generator with the Cloudflare plugin's worker-only entry.
45
+ const createBuilderForModern = await createBuilderGenerator();
46
+ const builder = await createBuilderForModern({
47
+ appContext,
48
+ normalizedConfig,
49
+ });
50
+ // This compiler has only an Effect worker entry. The framework SSR
51
+ // adapter filters page entries and requires a UI route, so it does not
52
+ // apply to a headless API worker.
53
+ builder.removePlugins(['builder-plugin-adapter-modern-ssr']);
54
+ await builder.build();
55
+ });
56
+ },
57
+ });
58
+
27
59
  /** Compose the fork's build and release features through native CLI plugins. */
28
60
  export const ultramodernAppTools = (): CliPlugin<AppTools> => ({
29
61
  name: '@modern-js/ultramodern-app-tools',
@@ -34,6 +66,7 @@ export const ultramodernAppTools = (): CliPlugin<AppTools> => ({
34
66
  ultramodernSSRIntegrationPlugin(),
35
67
  backendFederationBuildPlugin(),
36
68
  createCloudflareBuilderPlugin(),
69
+ headlessCloudflareWorkerPlugin(),
37
70
  createDeployOutputAliasesPlugin(),
38
71
  ultramodernReleaseEnvelopePlugin(),
39
72
  ],
@@ -4,6 +4,7 @@ import { builderPluginAdapterPrecompress } from '@modern-js/app-tools-extensions
4
4
  import { resolveEffectTsgoCompiler } from '@modern-js/app-tools-extensions/config';
5
5
  import { configureUltramodernTypeChecker } from '@modern-js/app-tools-extensions/native-type-checker';
6
6
  import { resolveUltramodernReleaseIdentity } from '@modern-js/app-tools-extensions/release-identity';
7
+ import { findHostingModuleDirectory } from '@modern-js/app-tools-extensions/runtime-package-resolution';
7
8
  import { mergeConfig } from '@modern-js/plugin/cli';
8
9
  import { type RspackChain, rspack } from '@rsbuild/core';
9
10
  import type { AppUserConfig } from './types';
@@ -81,56 +82,14 @@ export interface PresetUltramodernOptions {
81
82
  }
82
83
 
83
84
  const resolveReactRouterPackageDir = (appDirectory: string) => {
84
- const resolveNodeModulePackageJson = (
85
- packageName: string,
86
- fromDirectory: string,
87
- ) => {
88
- let currentDirectory = path.resolve(fromDirectory);
89
-
90
- while (true) {
91
- const packageJson = path.join(
92
- currentDirectory,
93
- 'node_modules',
94
- packageName,
95
- 'package.json',
96
- );
97
- if (fs.existsSync(packageJson)) {
98
- return fs.realpathSync(packageJson);
99
- }
100
-
101
- const parentDirectory = path.dirname(currentDirectory);
102
- if (parentDirectory === currentDirectory) {
103
- return undefined;
104
- }
105
- currentDirectory = parentDirectory;
106
- }
85
+ const findPackage = (name: string, from: string) => {
86
+ const modules = findHostingModuleDirectory(name, from);
87
+ return modules ? fs.realpathSync(path.join(modules, name)) : undefined;
107
88
  };
108
-
109
- const reactRouterPackageJson = resolveNodeModulePackageJson(
110
- 'react-router',
111
- appDirectory,
112
- );
113
- if (reactRouterPackageJson) {
114
- return path.dirname(reactRouterPackageJson);
115
- }
116
-
117
- const reactRouterDomPackageJson = resolveNodeModulePackageJson(
118
- 'react-router-dom',
119
- appDirectory,
120
- );
121
- if (!reactRouterDomPackageJson) {
122
- return undefined;
123
- }
124
-
125
- const nestedReactRouterPackageJson = resolveNodeModulePackageJson(
126
- 'react-router',
127
- path.dirname(reactRouterDomPackageJson),
128
- );
129
- if (nestedReactRouterPackageJson) {
130
- return path.dirname(nestedReactRouterPackageJson);
131
- }
132
-
133
- return undefined;
89
+ const direct = findPackage('react-router', appDirectory);
90
+ if (direct) return direct;
91
+ const dom = findPackage('react-router-dom', appDirectory);
92
+ return dom ? findPackage('react-router', dom) : undefined;
134
93
  };
135
94
 
136
95
  // Dependency-driven opt-in surface for apps that explicitly install
@@ -3,171 +3,72 @@ import type {
3
3
  AppTools,
4
4
  AppToolsNormalizedConfig,
5
5
  CliPlugin,
6
- ServerUserConfig,
7
6
  } from '@modern-js/app-tools';
8
7
  import type { CLIPluginAPI } from '@modern-js/plugin';
9
8
  import type { MergedEnvironmentConfig, RsbuildPlugin } from '@rsbuild/core';
10
9
 
11
- type RsbuildRspackPluginLike =
12
- | string
13
- | ((...args: any[]) => any)
14
- | {
15
- name?: string;
16
- constructor?: {
17
- name?: string;
18
- };
19
- }
20
- | [unknown, ...unknown[]];
21
-
22
10
  type EnvironmentConfigLike = Partial<
23
11
  Pick<MergedEnvironmentConfig, 'output' | 'source' | 'tools'>
24
12
  >;
25
13
 
26
- const getRspackPlugins = (rspackConfig: unknown): RsbuildRspackPluginLike[] => {
27
- if (!rspackConfig) {
28
- return [];
29
- }
30
-
31
- const rspackEntries = Array.isArray(rspackConfig)
32
- ? rspackConfig
33
- : [rspackConfig];
34
- const plugins: RsbuildRspackPluginLike[] = [];
35
-
36
- for (const entry of rspackEntries) {
37
- if (!entry || typeof entry === 'function' || typeof entry !== 'object') {
38
- continue;
39
- }
40
-
41
- const maybePlugins = (entry as { plugins?: unknown }).plugins;
42
-
43
- if (!Array.isArray(maybePlugins)) {
44
- continue;
45
- }
46
-
47
- for (const plugin of maybePlugins) {
48
- if (plugin) {
49
- plugins.push(plugin as RsbuildRspackPluginLike);
50
- }
51
- }
52
- }
53
-
54
- return plugins;
55
- };
56
-
57
- const getRspackPluginName = (
58
- plugin: RsbuildRspackPluginLike,
59
- ): string | undefined => {
60
- if (typeof plugin === 'string') {
61
- return plugin;
62
- }
63
-
64
- if (typeof plugin === 'function') {
65
- return plugin.name;
66
- }
67
-
68
- if (Array.isArray(plugin)) {
69
- const [first] = plugin;
70
-
71
- if (!first) {
72
- return undefined;
73
- }
74
-
75
- if (typeof first === 'string') {
76
- return first;
77
- }
78
-
79
- if (typeof first === 'function') {
80
- return first.name;
81
- }
82
-
83
- if (typeof first === 'object') {
84
- return (
85
- (first as { name?: string }).name ||
86
- (first as { constructor?: { name?: string } }).constructor?.name
87
- );
88
- }
89
-
90
- return undefined;
91
- }
92
-
93
- return plugin.name || plugin.constructor?.name;
94
- };
95
-
96
- const hasServerRenderingConfig = (
97
- userConfig: AppToolsNormalizedConfig,
98
- ): boolean => {
99
- const { output, server } = userConfig;
100
-
101
- if (output?.ssg) {
102
- return true;
103
- }
104
-
105
- if (output?.ssgByEntries && Object.keys(output.ssgByEntries).length > 0) {
106
- return true;
107
- }
108
-
109
- if (server?.ssr) {
110
- return true;
111
- }
112
-
113
- if (server?.ssrByEntries && Object.keys(server.ssrByEntries).length > 0) {
114
- return true;
115
- }
116
-
117
- return false;
118
- };
119
-
120
- const isModuleFederationAppSSREnabledInConfig = (
121
- ssr: ServerUserConfig['ssr'],
122
- ): boolean => {
123
- if (!ssr || typeof ssr !== 'object') {
124
- return false;
125
- }
126
-
127
- return ssr.moduleFederationAppSSR === true;
128
- };
129
-
130
- const isModuleFederationAppSSREnabled = (
131
- userConfig: AppToolsNormalizedConfig,
14
+ /** Translate supported MF integration markers at the builder boundary. */
15
+ const hasModuleFederationMarker = (
16
+ config: EnvironmentConfigLike,
17
+ projectMarker: boolean,
132
18
  ): boolean => {
133
- if (isModuleFederationAppSSREnabledInConfig(userConfig.server?.ssr)) {
19
+ if (projectMarker) return true;
20
+ const define = config.source?.define ?? {};
21
+ if ('REMOTE_IP_STRATEGY' in define || 'FEDERATION_IPV4' in define)
134
22
  return true;
135
- }
136
-
137
- if (
138
- userConfig.server?.ssrByEntries &&
139
- typeof userConfig.server.ssrByEntries === 'object'
140
- ) {
141
- return Object.values(userConfig.server.ssrByEntries).some(
142
- isModuleFederationAppSSREnabledInConfig,
143
- );
144
- }
145
23
 
146
- return false;
147
- };
148
-
149
- const isModuleFederationRspackPlugin = (
150
- plugin: RsbuildRspackPluginLike,
151
- ): boolean => {
152
- const candidate = getRspackPluginName(plugin);
153
-
154
- return typeof candidate === 'string' && /modulefederation/i.test(candidate);
24
+ const rspack = config.tools?.rspack;
25
+ return (Array.isArray(rspack) ? rspack : [rspack]).some(entry => {
26
+ if (
27
+ !entry ||
28
+ typeof entry !== 'object' ||
29
+ !('plugins' in entry) ||
30
+ !Array.isArray(entry.plugins)
31
+ )
32
+ return false;
33
+ return entry.plugins.some((input: unknown) => {
34
+ const plugin = Array.isArray(input) ? input[0] : input;
35
+ if (!plugin) return false;
36
+ const name =
37
+ typeof plugin === 'string'
38
+ ? plugin
39
+ : typeof plugin === 'function'
40
+ ? plugin.name
41
+ : typeof plugin === 'object'
42
+ ? (plugin as { name?: string }).name || plugin.constructor?.name
43
+ : undefined;
44
+ return typeof name === 'string' && /modulefederation/i.test(name);
45
+ });
46
+ });
155
47
  };
156
48
 
157
- const hasModuleFederationMarker = (config: EnvironmentConfigLike): boolean => {
158
- if (process.env.MF_SSR_PRJ === 'true') {
159
- return true;
160
- }
161
-
162
- const define = config.source?.define || {};
163
-
164
- if ('REMOTE_IP_STRATEGY' in define || 'FEDERATION_IPV4' in define) {
165
- return true;
166
- }
167
-
168
- const plugins = getRspackPlugins(config.tools?.rspack);
169
-
170
- return plugins.some(isModuleFederationRspackPlugin);
49
+ /** Resolve application-wide SSR policy once; environment hooks only apply it. */
50
+ const normalizeSsrCapabilities = (config: AppToolsNormalizedConfig) => {
51
+ const ssr = [
52
+ config.server?.ssr,
53
+ ...Object.values(config.server?.ssrByEntries ?? {}),
54
+ ];
55
+ return {
56
+ rendering: Boolean(
57
+ config.output?.ssg ||
58
+ Object.keys(config.output?.ssgByEntries ?? {}).length ||
59
+ config.server?.ssr ||
60
+ Object.keys(config.server?.ssrByEntries ?? {}).length,
61
+ ),
62
+ federation: ssr.some(
63
+ value =>
64
+ value &&
65
+ typeof value === 'object' &&
66
+ value.moduleFederationAppSSR === true,
67
+ ),
68
+ worker: config.deploy?.target === 'cloudflare',
69
+ projectMarker: process.env.MF_SSR_PRJ === 'true',
70
+ requireExplicit: process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT === 'true',
71
+ };
171
72
  };
172
73
 
173
74
  const isNodeEnvironmentTarget = (target: unknown): boolean =>
@@ -178,7 +79,7 @@ export const shouldUseModuleFederationNodeOutput = (
178
79
  config: EnvironmentConfigLike,
179
80
  ): boolean =>
180
81
  isNodeEnvironmentTarget(config.output?.target) &&
181
- hasModuleFederationMarker(config);
82
+ hasModuleFederationMarker(config, process.env.MF_SSR_PRJ === 'true');
182
83
 
183
84
  const ssrIntegrationBuilderPlugin = (
184
85
  modernAPI: CLIPluginAPI<AppTools>,
@@ -186,25 +87,25 @@ const ssrIntegrationBuilderPlugin = (
186
87
  name: '@modern-js/ultramodern-builder-plugin-ssr',
187
88
  pre: ['@modern-js/builder-plugin-ssr'],
188
89
  setup(api) {
90
+ const capabilities = normalizeSsrCapabilities(
91
+ modernAPI.getNormalizedConfig(),
92
+ );
189
93
  api.modifyEnvironmentConfig((config, { name, mergeEnvironmentConfig }) => {
190
94
  const isServerEnvironment =
191
95
  isNodeEnvironmentTarget(config.output.target) || name === 'workerSSR';
192
- const userConfig = modernAPI.getNormalizedConfig();
193
- const hasServerRendering = hasServerRenderingConfig(userConfig);
194
96
  const hasModuleFederationRuntimeMarker =
195
- hasServerRendering && shouldUseModuleFederationNodeOutput(config);
196
- const hasExplicitMfSsrFlag = isModuleFederationAppSSREnabled(userConfig);
197
- const requireExplicitMfSsrFlag =
198
- process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT === 'true';
97
+ capabilities.rendering &&
98
+ isNodeEnvironmentTarget(config.output.target) &&
99
+ hasModuleFederationMarker(config, capabilities.projectMarker);
199
100
 
200
101
  if (
201
- hasServerRendering &&
102
+ capabilities.rendering &&
202
103
  hasModuleFederationRuntimeMarker &&
203
- !hasExplicitMfSsrFlag
104
+ !capabilities.federation
204
105
  ) {
205
106
  const warningMessage =
206
107
  '[modernjs][mf-ssr] Module Federation SSR was auto-detected from runtime markers. Set server.ssr.moduleFederationAppSSR=true explicitly in host and remotes to avoid heuristic drift.';
207
- if (requireExplicitMfSsrFlag) {
108
+ if (capabilities.requireExplicit) {
208
109
  throw new Error(
209
110
  `${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`,
210
111
  );
@@ -213,7 +114,7 @@ const ssrIntegrationBuilderPlugin = (
213
114
  console.warn(warningMessage);
214
115
  }
215
116
  const isModuleFederationAppSSR =
216
- hasServerRendering && hasExplicitMfSsrFlag;
117
+ capabilities.rendering && capabilities.federation;
217
118
  return mergeEnvironmentConfig(config, {
218
119
  source: {
219
120
  define: {
@@ -222,12 +123,12 @@ const ssrIntegrationBuilderPlugin = (
222
123
  ),
223
124
  },
224
125
  },
225
- ...(name === 'workerSSR' && userConfig.deploy?.target === 'cloudflare'
126
+ ...(name === 'workerSSR' && capabilities.worker
226
127
  ? { output: { module: true } }
227
128
  : {}),
228
129
  splitChunks:
229
130
  isServerEnvironment &&
230
- (hasModuleFederationRuntimeMarker || hasExplicitMfSsrFlag)
131
+ (hasModuleFederationRuntimeMarker || capabilities.federation)
231
132
  ? false
232
133
  : undefined,
233
134
  });