@nx/react 24.0.5 → 24.0.7

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.
@@ -9,8 +9,11 @@ const NAME = '<%= federationName %>';
9
9
  export default defineConfig({
10
10
  server: {
11
11
  port: PORT,
12
- // Allow cross-origin fetches from a consumer on a different port.
13
- headers: { 'Access-Control-Allow-Origin': '*' },
12
+ // Local origins only. Without an explicit origin, the federation plugin
13
+ // sends `Access-Control-Allow-Origin: *`.
14
+ cors: {
15
+ origin: /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/,
16
+ },
14
17
  },
15
18
  // assetPrefix:true tells rsbuild to emit chunks with the dev-server URL as
16
19
  // their public path. Without it a consumer on a different port asks its
@@ -13,6 +13,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
13
 
14
14
  const PORT = <%= port %>;
15
15
  const NAME = '<%= federationName %>';
16
+ // localhost (and its subdomains), 127.0.0.1 or [::1], on any port.
17
+ const LOCAL_ORIGIN =
18
+ /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/;
16
19
 
17
20
  // Read mode from the rspack CLI arg (`--mode=development|production`) so the
18
21
  // config works the same on Windows + POSIX without depending on a shell
@@ -32,7 +35,13 @@ export default defineConfig((_env, argv) => {
32
35
  port: PORT,
33
36
  historyApiFallback: true,
34
37
  hot: true,
35
- headers: { 'Access-Control-Allow-Origin': '*' },
38
+ // Lets consumers on local origins fetch the manifest and hot updates.
39
+ headers: (req): Record<string, string> => {
40
+ const origin = req.headers.origin;
41
+ return origin && LOCAL_ORIGIN.test(origin)
42
+ ? { 'Access-Control-Allow-Origin': origin, Vary: 'Origin' }
43
+ : { Vary: 'Origin' };
44
+ },
36
45
  },
37
46
  resolve: { extensions: ['...', '.ts', '.tsx', '.jsx'] },
38
47
  module: {
@@ -13,13 +13,8 @@ export default defineConfig({
13
13
  strictPort: true,
14
14
  origin: `http://localhost:${PORT}`,
15
15
  host: '127.0.0.1',
16
- // Allow cross-origin fetches of mf-manifest.json + chunks from a consumer
17
- // running on a different port. Vite 8 narrows the default CORS allowlist
18
- // to specific localhost patterns; setting `cors: true` emits a wildcard
19
- // `Access-Control-Allow-Origin: *` for dev which is what federation needs.
20
- cors: true,
21
16
  },
22
- preview: { port: PORT, strictPort: true, cors: true },
17
+ preview: { port: PORT, strictPort: true },
23
18
  build: { target: 'chrome89' },
24
19
  plugins: [
25
20
  federation({
@@ -1,4 +1 @@
1
- /**
2
- * Whether `request` resolves from `directory`, without loading it.
3
- */
4
- export declare function canResolve(request: string, directory: string): boolean;
1
+ export declare function resolveOptionalModule(request: string, directory: string): string | null;
@@ -1,15 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.canResolve = canResolve;
4
- /**
5
- * Whether `request` resolves from `directory`, without loading it.
6
- */
7
- function canResolve(request, directory) {
3
+ exports.resolveOptionalModule = resolveOptionalModule;
4
+ const internal_1 = require("@nx/devkit/internal");
5
+ function resolveOptionalModule(request, directory) {
8
6
  try {
9
- require.resolve(request, { paths: [directory] });
10
- return true;
7
+ return (0, internal_1.resolveInstalledPackage)(request, [directory], directory);
11
8
  }
12
9
  catch {
13
- return false;
10
+ return null;
14
11
  }
15
12
  }
@@ -5,9 +5,16 @@ const internal_1 = require("@nx/devkit/internal");
5
5
  const devkit_1 = require("@nx/devkit");
6
6
  const path_1 = require("path");
7
7
  const fs_1 = require("fs");
8
+ const node_url_1 = require("node:url");
8
9
  const js_1 = require("@nx/js");
9
10
  const internal_2 = require("@nx/js/internal");
10
11
  const router_plugin_utils_1 = require("./router-plugin-utils");
12
+ const node_module_1 = require("node:module");
13
+ // Node passes a stand-in require, without a cache, require.extensions or
14
+ // Module._load, when an ESM loader hook supplies this module's source, as
15
+ // Yarn PnP does.
16
+ if (!require.cache)
17
+ require = (0, node_module_1.createRequire)(__filename);
11
18
  const reactRouterConfigBlob = '**/react-router.config.{ts,js,cjs,cts,mjs,mts}';
12
19
  exports.createNodes = [
13
20
  reactRouterConfigBlob,
@@ -32,14 +39,55 @@ exports.createNodes = [
32
39
  const pmCommand = (0, devkit_1.getPackageManagerCommand)(packageManager);
33
40
  const lockfile = (0, js_1.getLockFileName)(packageManager);
34
41
  const hashes = await (0, internal_1.calculateHashesForCreateNodes)(projectRoots, { ...normalizedOptions, isUsingTsSolutionSetup }, context, projectRoots.map((_) => [lockfile]));
42
+ const configFileHashes = new Map();
43
+ const getConfigFileHash = (file) => {
44
+ if (!configFileHashes.has(file)) {
45
+ configFileHashes.set(file, (0, internal_1.hashFile)(file) ?? null);
46
+ }
47
+ return configFileHashes.get(file) ?? null;
48
+ };
49
+ const getConfigDependencyHashes = (dependencies, configLoadStartedAt) => {
50
+ const hashes = {};
51
+ try {
52
+ for (const file of dependencies) {
53
+ const hash = getConfigFileHash(file);
54
+ if (hash === null)
55
+ return undefined;
56
+ const { mtimeMs, ctimeMs } = (0, fs_1.statSync)(file);
57
+ // Do not associate an earlier config result with newer file contents.
58
+ if (mtimeMs >= configLoadStartedAt ||
59
+ ctimeMs >= configLoadStartedAt) {
60
+ return undefined;
61
+ }
62
+ hashes[file] = hash;
63
+ }
64
+ }
65
+ catch {
66
+ return undefined;
67
+ }
68
+ return hashes;
69
+ };
35
70
  try {
36
71
  return await (0, devkit_1.createNodesFromFiles)(async (configFile, _, context, idx) => {
37
72
  const projectRoot = (0, path_1.dirname)(configFile);
38
73
  const hash = hashes[idx] + configFile;
39
- if (!targetsCache.has(hash)) {
40
- targetsCache.set(hash, await buildReactRouterTargets(configFile, projectRoot, normalizedOptions, context, isUsingTsSolutionSetup, pmCommand));
74
+ const cachedTargets = targetsCache.get(hash);
75
+ let result;
76
+ if (cachedTargets?.configDependencyHashes &&
77
+ Object.entries(cachedTargets.configDependencyHashes).every(([file, previousHash]) => getConfigFileHash(file) === previousHash)) {
78
+ result = cachedTargets;
79
+ }
80
+ else {
81
+ const configLoadStartedAt = Date.now();
82
+ const { configDependencies, ...inferredTargets } = await buildReactRouterTargets(configFile, projectRoot, normalizedOptions, context, isUsingTsSolutionSetup, pmCommand);
83
+ result = inferredTargets;
84
+ const configDependencyHashes = configDependencies &&
85
+ getConfigDependencyHashes(configDependencies, configLoadStartedAt);
86
+ if (configDependencyHashes) {
87
+ targetsCache.set(hash, { ...result, configDependencyHashes });
88
+ }
41
89
  }
42
- const { projectType, metadata, targets } = targetsCache.get(hash);
90
+ const { projectType, metadata, targets } = result;
43
91
  const project = {
44
92
  root: projectRoot,
45
93
  targets,
@@ -74,7 +122,8 @@ async function buildReactRouterTargets(configFilePath, projectRoot, options, con
74
122
  const { buildDirectory, serverBuildPath } = await getBuildPaths(reactRouterConfig, isLibMode);
75
123
  const targets = {};
76
124
  targets[options.buildTargetName] = await getBuildTargetConfig(options.buildTargetName, projectRoot, buildDirectory, serverBuildPath, namedInputs, isUsingTsSolutionSetup);
77
- targets[options.devTargetName] = await devTarget(projectRoot, namedInputs, isUsingTsSolutionSetup, getDevReadyWhen(context.workspaceRoot, (0, path_1.join)(context.workspaceRoot, projectRoot)));
125
+ const { readyWhen, configDependencies } = await getDevReadyWhen(context.workspaceRoot, (0, path_1.join)(context.workspaceRoot, projectRoot));
126
+ targets[options.devTargetName] = await devTarget(projectRoot, namedInputs, isUsingTsSolutionSetup, readyWhen);
78
127
  if (serverBuildPath) {
79
128
  targets[options.startTargetName] = await startTarget(projectRoot, serverBuildPath, options.buildTargetName, namedInputs, isUsingTsSolutionSetup);
80
129
  }
@@ -84,6 +133,7 @@ async function buildReactRouterTargets(configFilePath, projectRoot, options, con
84
133
  targets,
85
134
  metadata,
86
135
  projectType: isLibMode ? 'library' : 'application',
136
+ configDependencies,
87
137
  };
88
138
  }
89
139
  async function getBuildTargetConfig(buildTargetName, projectRoot, buildDirectory, serverBuildDirectory, namedInputs, isUsingTsSolutionSetup) {
@@ -153,19 +203,65 @@ const viteConfigFiles = [
153
203
  'vite.config.mts',
154
204
  'vite.config.cts',
155
205
  ];
156
- // The Vite check resolves the config when the task runs, with the task's env
157
- // and options, and waits for the server's port.
158
- function getDevReadyWhen(workspaceRoot, absoluteProjectRoot) {
206
+ async function getDevReadyWhen(workspaceRoot, absoluteProjectRoot) {
159
207
  // `react-router dev` serves with the default Vite config of its cwd.
160
208
  const configFile = viteConfigFiles.find((file) => (0, fs_1.existsSync)((0, path_1.join)(absoluteProjectRoot, file)));
161
209
  if (!configFile) {
162
- return undefined;
210
+ return { readyWhen: undefined, configDependencies: [] };
211
+ }
212
+ // The command check runs from the workspace root and must resolve its helper there.
213
+ const viteReadyCheck = (0, router_plugin_utils_1.resolveOptionalModule)('@nx/vite/ready-check', workspaceRoot);
214
+ if (!viteReadyCheck) {
215
+ return { readyWhen: undefined, configDependencies: [] };
163
216
  }
164
- // Nx runs the check from the workspace root, where @nx/vite may be missing
165
- if (!(0, router_plugin_utils_1.canResolve)('@nx/vite/ready-check', workspaceRoot)) {
166
- return undefined;
217
+ const commandReadyWhen = (0, internal_1.getReadyCheckReadyWhen)('@nx/vite/ready-check', configFile, 'react-router-dev');
218
+ try {
219
+ let vitePackageJsonPath;
220
+ try {
221
+ vitePackageJsonPath = (0, internal_1.resolveInstalledPackage)('vite/package.json', [absoluteProjectRoot, (0, path_1.dirname)(viteReadyCheck)], workspaceRoot);
222
+ }
223
+ catch {
224
+ // A linked @nx/vite may keep Vite in its own package directory.
225
+ const nxVitePackageJsonPath = (0, internal_1.resolveInstalledPackage)('@nx/vite/package.json', [workspaceRoot], workspaceRoot);
226
+ vitePackageJsonPath = (0, internal_1.resolveInstalledPackage)('vite/package.json', [(0, path_1.dirname)(viteReadyCheck)], (0, path_1.dirname)(nxVitePackageJsonPath));
227
+ }
228
+ const viteExport = (0, devkit_1.readJsonFile)(vitePackageJsonPath).exports['.'];
229
+ // Vite 5/6 expose a deprecated CJS shim under the require condition.
230
+ const esmEntry = typeof viteExport === 'string'
231
+ ? viteExport
232
+ : (viteExport.import?.default ?? viteExport.import);
233
+ const { loadConfigFromFile } = await import((0, node_url_1.pathToFileURL)((0, path_1.join)((0, path_1.dirname)(vitePackageJsonPath), esmEntry)).href);
234
+ // Resolving serve plugins starts React Router's type generator. Loading only
235
+ // the config avoids a second generator deleting files the server is writing.
236
+ const viteConfig = await (0, internal_1.retryOnRequireEsmRace)(() => loadConfigFromFile({
237
+ command: 'serve',
238
+ mode: 'development',
239
+ isSsrBuild: false,
240
+ isPreview: false,
241
+ }, (0, path_1.join)(absoluteProjectRoot, configFile), absoluteProjectRoot, 'silent'));
242
+ return {
243
+ readyWhen: (await loadsReadySignalPlugin(viteConfig?.config.plugins))
244
+ ? { signal: true }
245
+ : commandReadyWhen,
246
+ configDependencies: viteConfig
247
+ ? [...new Set(viteConfig.dependencies.map((file) => (0, path_1.resolve)(file)))]
248
+ : undefined,
249
+ };
250
+ }
251
+ catch {
252
+ // Optional readiness detection must not prevent graph creation.
253
+ }
254
+ return { readyWhen: commandReadyWhen };
255
+ }
256
+ async function loadsReadySignalPlugin(plugins) {
257
+ const plugin = await plugins;
258
+ if (Array.isArray(plugin)) {
259
+ return (await Promise.all(plugin.map(loadsReadySignalPlugin))).some(Boolean);
167
260
  }
168
- return (0, internal_1.getReadyCheckReadyWhen)('@nx/vite/ready-check', configFile, 'react-router-dev');
261
+ const candidate = plugin;
262
+ // Function predicates may depend on the task environment.
263
+ return (candidate?.name === 'nx-ready-signal' &&
264
+ (!candidate.apply || candidate.apply === 'serve'));
169
265
  }
170
266
  async function startTarget(projectRoot, serverBuildPath, buildTargetName, namedInputs, isUsingTsSolutionSetup) {
171
267
  const serverPath = serverBuildPath === 'build/server'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/react",
3
- "version": "24.0.5",
3
+ "version": "24.0.7",
4
4
  "private": false,
5
5
  "description": "The React plugin for Nx contains executors and generators for managing React applications and libraries within an Nx workspace. It provides:\n\n\n- Integration with libraries such as Jest, Vitest, Playwright, Cypress, and Storybook.\n\n- Generators for applications, libraries, components, hooks, and more.\n\n- Library build support for publishing packages to npm or other registries.\n\n- Utilities for automatic workspace refactoring.",
6
6
  "repository": {
@@ -93,38 +93,42 @@
93
93
  "peerDependencies": {
94
94
  "babel-plugin-react-compiler": "^1.0.0",
95
95
  "react": ">=18.0.0 <20.0.0",
96
- "react-dom": ">=18.0.0 <20.0.0"
96
+ "react-dom": ">=18.0.0 <20.0.0",
97
+ "typescript": "^5.8.0 || ^6.0.0"
97
98
  },
98
99
  "peerDependenciesMeta": {
99
100
  "babel-plugin-react-compiler": {
100
101
  "optional": true
102
+ },
103
+ "typescript": {
104
+ "optional": true
101
105
  }
102
106
  },
103
107
  "dependencies": {
104
108
  "@babel/preset-react": "^7.22.5",
105
109
  "@phenomnomnominal/tsquery": "~6.2.0",
106
- "minimatch": "10.2.5",
110
+ "minimatch": "11.0.8",
107
111
  "tslib": "^2.3.0",
108
- "@nx/devkit": "24.0.5",
109
- "@nx/js": "24.0.5",
110
- "@nx/eslint": "24.0.5",
111
- "@nx/web": "24.0.5",
112
+ "@nx/devkit": "24.0.7",
113
+ "@nx/js": "24.0.7",
114
+ "@nx/eslint": "24.0.7",
115
+ "@nx/web": "24.0.7",
112
116
  "semver": "^7.6.3"
113
117
  },
114
118
  "devDependencies": {
115
- "@nx/cypress": "24.0.5",
116
- "@nx/playwright": "24.0.5",
117
- "@nx/rollup": "24.0.5",
118
- "@nx/rsbuild": "24.0.5",
119
- "@nx/rspack": "24.0.5",
120
- "@nx/vite": "24.0.5",
121
- "@nx/vitest": "24.0.5",
122
- "@nx/webpack": "24.0.5",
123
- "@nx/storybook": "24.0.5",
124
- "nx": "24.0.5"
119
+ "@nx/cypress": "24.0.7",
120
+ "@nx/playwright": "24.0.7",
121
+ "@nx/rollup": "24.0.7",
122
+ "@nx/rsbuild": "24.0.7",
123
+ "@nx/rspack": "24.0.7",
124
+ "@nx/vite": "24.0.7",
125
+ "@nx/vitest": "24.0.7",
126
+ "@nx/webpack": "24.0.7",
127
+ "@nx/storybook": "24.0.7",
128
+ "nx": "24.0.7"
125
129
  },
126
130
  "optionalDependencies": {
127
- "@nx/vite": "24.0.5"
131
+ "@nx/vite": "24.0.7"
128
132
  },
129
133
  "publishConfig": {
130
134
  "access": "public"