From 0db767205170f3cce8f2c7b20bc257c84e545ba8 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 22 Nov 2024 12:26:31 -0500 Subject: [PATCH] feat(module-federation): move withModuleFederation for rspack to new package --- packages/module-federation/rspack.ts | 2 + .../with-module-federation/rspack}/utils.ts | 2 +- .../rspack}/with-module-federation-ssr.ts | 5 +- .../rspack}/with-module-federation.ts | 6 +- .../__snapshots__/host.rspack.spec.ts.snap | 8 +- .../rspack.server.config.ts__tmpl__ | 2 +- .../rspack.server.config.js__tmpl__ | 2 +- .../rspack.config.prod.ts__tmpl__ | 2 +- .../rspack.config.ts__tmpl__ | 2 +- .../rspack.config.js__tmpl__ | 2 +- .../rspack.config.prod.js__tmpl__ | 2 +- .../__snapshots__/remote.rspack.spec.ts.snap | 10 +- .../rspack.server.config.ts__tmpl__ | 2 +- .../rspack.server.config.js__tmpl__ | 2 +- .../rspack.config.ts__tmpl__ | 2 +- .../rspack.config.js__tmpl__ | 2 +- .../migrate-with-mf-import-to-new-package.ts | 4 +- packages/rspack/migrations.json | 9 +- packages/rspack/module-federation.ts | 8 +- ...rate-with-mf-import-to-new-package.spec.ts | 138 ++++++++++++++++++ .../migrate-with-mf-import-to-new-package.ts | 57 ++++++++ .../src/utils/module-federation/public-api.ts | 4 - 22 files changed, 239 insertions(+), 34 deletions(-) create mode 100644 packages/module-federation/rspack.ts rename packages/{rspack/src/utils/module-federation/with-module-federation => module-federation/src/with-module-federation/rspack}/utils.ts (99%) rename packages/{rspack/src/utils/module-federation/with-module-federation => module-federation/src/with-module-federation/rspack}/with-module-federation-ssr.ts (94%) rename packages/{rspack/src/utils/module-federation/with-module-federation => module-federation/src/with-module-federation/rspack}/with-module-federation.ts (95%) create mode 100644 packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.spec.ts create mode 100644 packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts delete mode 100644 packages/rspack/src/utils/module-federation/public-api.ts diff --git a/packages/module-federation/rspack.ts b/packages/module-federation/rspack.ts new file mode 100644 index 00000000000000..b7823a5e9ea32b --- /dev/null +++ b/packages/module-federation/rspack.ts @@ -0,0 +1,2 @@ +export * from './src/with-module-federation/rspack/with-module-federation'; +export * from './src/with-module-federation/rspack/with-module-federation-ssr'; diff --git a/packages/rspack/src/utils/module-federation/with-module-federation/utils.ts b/packages/module-federation/src/with-module-federation/rspack/utils.ts similarity index 99% rename from packages/rspack/src/utils/module-federation/with-module-federation/utils.ts rename to packages/module-federation/src/with-module-federation/rspack/utils.ts index 476e3e91cdb921..54b76b786a6f68 100644 --- a/packages/rspack/src/utils/module-federation/with-module-federation/utils.ts +++ b/packages/module-federation/src/with-module-federation/rspack/utils.ts @@ -13,7 +13,7 @@ import { mapRemotes, mapRemotesForSSR, getDependentPackagesForProject, -} from '@nx/module-federation'; +} from '../../utils'; export function getFunctionDeterminateRemoteUrl(isServer = false) { const target = 'serve'; diff --git a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts b/packages/module-federation/src/with-module-federation/rspack/with-module-federation-ssr.ts similarity index 94% rename from packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts rename to packages/module-federation/src/with-module-federation/rspack/with-module-federation-ssr.ts index a8d38aae0358d1..4a43ebb063b50b 100644 --- a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts +++ b/packages/module-federation/src/with-module-federation/rspack/with-module-federation-ssr.ts @@ -2,9 +2,8 @@ import { DefinePlugin } from '@rspack/core'; import { ModuleFederationConfig, NxModuleFederationConfigOverride, -} from '@nx/module-federation'; +} from '../../utils'; import { getModuleFederationConfig } from './utils'; -import { NxRspackExecutionContext } from '../../config'; export async function withModuleFederationForSSR( options: ModuleFederationConfig, @@ -19,7 +18,7 @@ export async function withModuleFederationForSSR( isServer: true, }); - return (config, { context }: NxRspackExecutionContext) => { + return (config, { context }) => { config.target = 'async-node'; config.output.uniqueName = options.name; config.output.library = { diff --git a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts b/packages/module-federation/src/with-module-federation/rspack/with-module-federation.ts similarity index 95% rename from packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts rename to packages/module-federation/src/with-module-federation/rspack/with-module-federation.ts index 18e6268cf226a9..d162a281354f0c 100644 --- a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts +++ b/packages/module-federation/src/with-module-federation/rspack/with-module-federation.ts @@ -4,9 +4,9 @@ import { DefinePlugin } from '@rspack/core'; import { ModuleFederationConfig, NxModuleFederationConfigOverride, -} from '@nx/module-federation'; +} from '../../utils'; import { getModuleFederationConfig } from './utils'; -import { NxRspackExecutionContext } from '../../config'; +import { type ExecutorContext } from '@nx/devkit'; const isVarOrWindow = (libType?: string) => libType === 'var' || libType === 'window'; @@ -31,7 +31,7 @@ export async function withModuleFederation( return function makeConfig( config: Configuration, - { context }: NxRspackExecutionContext + { context } ): Configuration { config.output.uniqueName = options.name; config.output.publicPath = 'auto'; diff --git a/packages/react/src/generators/host/__snapshots__/host.rspack.spec.ts.snap b/packages/react/src/generators/host/__snapshots__/host.rspack.spec.ts.snap index c655daa70dab45..d797b2f6828179 100644 --- a/packages/react/src/generators/host/__snapshots__/host.rspack.spec.ts.snap +++ b/packages/react/src/generators/host/__snapshots__/host.rspack.spec.ts.snap @@ -2,7 +2,7 @@ exports[`hostGenerator bundler=rspack should generate host files and configs for SSR 1`] = ` "const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederationForSSR } = require('@nx/rspack/module-federation'); +const { withModuleFederationForSSR } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); @@ -44,7 +44,7 @@ module.exports = moduleFederationConfig; exports[`hostGenerator bundler=rspack should generate host files and configs for SSR when --typescriptConfiguration=true 1`] = ` "import { composePlugins, withNx, withReact } from '@nx/rspack'; -import { withModuleFederationForSSR } from '@nx/rspack/module-federation'; +import { withModuleFederationForSSR } from '@nx/module-federation/rspack'; import baseConfig from './module-federation.config'; @@ -83,7 +83,7 @@ export default config; exports[`hostGenerator bundler=rspack should generate host files and configs when --typescriptConfiguration=false 1`] = ` "const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); @@ -130,7 +130,7 @@ module.exports = { exports[`hostGenerator bundler=rspack should generate host files and configs when --typescriptConfiguration=true 1`] = ` "import {composePlugins, withNx, withReact} from '@nx/rspack'; -import { withModuleFederation } from '@nx/rspack/module-federation'; +import { withModuleFederation } from '@nx/module-federation/rspack'; import { ModuleFederationConfig } from '@nx/module-federation'; import baseConfig from './module-federation.config'; diff --git a/packages/react/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ index 0d7fb7aa318428..9252df87732f58 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ @@ -1,5 +1,5 @@ import {composePlugins, withNx, withReact} from '@nx/rspack'; -import {withModuleFederationForSSR} from '@nx/rspack/module-federation'; +import {withModuleFederationForSSR} from '@nx/module-federation/rspack'; import baseConfig from './module-federation.config'; diff --git a/packages/react/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ index 724e6f0c4aabbb..f1a458119ac101 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ @@ -1,5 +1,5 @@ const {composePlugins, withNx, withReact} = require('@nx/rspack'); -const {withModuleFederationForSSR} = require('@nx/rspack/module-federation'); +const {withModuleFederationForSSR} = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); diff --git a/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ index ca4f9e0304cbe7..68d9545d940fdd 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ @@ -1,5 +1,5 @@ import { composePlugins, withNx, withReact } from '@nx/rspack'; -import { withModuleFederation } from '@nx/rspack/module-federation'; +import { withModuleFederation } from '@nx/module-federation/rspack'; import { ModuleFederationConfig } from '@nx/module-federation'; import baseConfig from './module-federation.config'; diff --git a/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ index 47578a044e1b11..c7fa3c7cba6c0c 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ @@ -1,5 +1,5 @@ import {composePlugins, withNx, withReact} from '@nx/rspack'; -import { withModuleFederation } from '@nx/rspack/module-federation'; +import { withModuleFederation } from '@nx/module-federation/rspack'; import { ModuleFederationConfig } from '@nx/module-federation'; import baseConfig from './module-federation.config'; diff --git a/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ index acf84ac0a4cfd2..5157345a7b6d8e 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ @@ -1,5 +1,5 @@ const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); diff --git a/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ b/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ index ae08aecf78e7f0..186bfde7586c82 100644 --- a/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ +++ b/packages/react/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ @@ -1,5 +1,5 @@ const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); diff --git a/packages/react/src/generators/remote/__snapshots__/remote.rspack.spec.ts.snap b/packages/react/src/generators/remote/__snapshots__/remote.rspack.spec.ts.snap index dc57d07fdc9f45..ce6df0a3f5aa94 100644 --- a/packages/react/src/generators/remote/__snapshots__/remote.rspack.spec.ts.snap +++ b/packages/react/src/generators/remote/__snapshots__/remote.rspack.spec.ts.snap @@ -2,7 +2,7 @@ exports[`remote generator bundler=rspack should create the remote with the correct config files 1`] = ` "const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); @@ -37,7 +37,7 @@ module.exports = { exports[`remote generator bundler=rspack should create the remote with the correct config files when --js=true 1`] = ` "const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); @@ -72,7 +72,7 @@ module.exports = { exports[`remote generator bundler=rspack should create the remote with the correct config files when --typescriptConfiguration=true 1`] = ` "import { composePlugins, withNx, withReact } from '@nx/rspack'; -import { withModuleFederation } from '@nx/rspack/module-federation'; +import { withModuleFederation } from '@nx/module-federation/rspack'; import baseConfig from './module-federation.config'; @@ -118,7 +118,7 @@ export default config; exports[`remote generator bundler=rspack should generate correct remote with config files when using --ssr 1`] = ` "const {composePlugins, withNx, withReact} = require('@nx/rspack'); -const {withModuleFederationForSSR} = require('@nx/rspack/module-federation'); +const {withModuleFederationForSSR} = require('@nx/module-federation/rspack'); const baseConfig = require("./module-federation.server.config"); @@ -151,7 +151,7 @@ module.exports = { exports[`remote generator bundler=rspack should generate correct remote with config files when using --ssr and --typescriptConfiguration=true 1`] = ` "import { composePlugins, withNx, withReact } from '@nx/rspack'; -import { withModuleFederationForSSR } from '@nx/rspack/module-federation'; +import { withModuleFederationForSSR } from '@nx/module-federation/rspack'; import baseConfig from './module-federation.server.config'; diff --git a/packages/react/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ b/packages/react/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ index e28a7ba5917fe9..93875578385e47 100644 --- a/packages/react/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +++ b/packages/react/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ @@ -1,5 +1,5 @@ import {composePlugins, withNx, withReact} from '@nx/rspack'; -import {withModuleFederationForSSR} from '@nx/rspack/module-federation'; +import {withModuleFederationForSSR} from '@nx/module-federation/rspack'; import baseConfig from "./module-federation.server.config"; diff --git a/packages/react/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ b/packages/react/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ index d645983cc2b613..2ad8ae4fba2d04 100644 --- a/packages/react/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +++ b/packages/react/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ @@ -1,5 +1,5 @@ const {composePlugins, withNx, withReact} = require('@nx/rspack'); -const {withModuleFederationForSSR} = require('@nx/rspack/module-federation'); +const {withModuleFederationForSSR} = require('@nx/module-federation/rspack'); const baseConfig = require("./module-federation.server.config"); diff --git a/packages/react/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ b/packages/react/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ index 7d8d925be0c9a5..42d8abf4a95cb8 100644 --- a/packages/react/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +++ b/packages/react/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ @@ -1,5 +1,5 @@ import {composePlugins, withNx, withReact} from '@nx/rspack'; -import {withModuleFederation} from '@nx/rspack/module-federation'; +import {withModuleFederation} from '@nx/module-federation/rspack'; import baseConfig from './module-federation.config'; diff --git a/packages/react/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ b/packages/react/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ index f85ac78d1d5087..e0aa7b195afb5f 100644 --- a/packages/react/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ +++ b/packages/react/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ @@ -1,5 +1,5 @@ const { composePlugins, withNx, withReact } = require('@nx/rspack'); -const { withModuleFederation } = require('@nx/rspack/module-federation'); +const { withModuleFederation } = require('@nx/module-federation/rspack'); const baseConfig = require('./module-federation.config'); diff --git a/packages/react/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts b/packages/react/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts index 273f56bb7db8e2..bd297f5786d81a 100644 --- a/packages/react/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts +++ b/packages/react/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts @@ -7,7 +7,7 @@ import { import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils'; import { tsquery } from '@phenomnomnominal/tsquery'; -const NX_ANGULAR_MODULE_FEDERATION_IMPORT_SELECTOR = +const NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR = 'ImportDeclaration > StringLiteral[value=@nx/react/module-federation], VariableStatement CallExpression:has(Identifier[name=require]) > StringLiteral[value=@nx/react/module-federation]'; const NEW_IMPORT_PATH = `'@nx/module-federation/webpack'`; @@ -36,7 +36,7 @@ export default async function migrateWithMfImport(tree: Tree) { const ast = tsquery.ast(contents); const importNodes = tsquery( ast, - NX_ANGULAR_MODULE_FEDERATION_IMPORT_SELECTOR + NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR ); if (importNodes.length === 0) { diff --git a/packages/rspack/migrations.json b/packages/rspack/migrations.json index f9585605f4329d..8ceeacad5cbb50 100644 --- a/packages/rspack/migrations.json +++ b/packages/rspack/migrations.json @@ -1,5 +1,12 @@ { - "generators": {}, + "generators": { + "update-20-2-0-update-with-module-federation-import": { + "cli": "nx", + "version": "20.2.0-beta.3", + "description": "Update the withModuleFederation import use @nx/module-federation/rspack.", + "factory": "./src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package" + } + }, "packageJsonUpdates": { "18.1.0": { "version": "18.1.0-beta.0", diff --git a/packages/rspack/module-federation.ts b/packages/rspack/module-federation.ts index a4cf0dd1a4364a..52ed3189cc4127 100644 --- a/packages/rspack/module-federation.ts +++ b/packages/rspack/module-federation.ts @@ -1 +1,7 @@ -export * from './src/utils/module-federation/public-api'; +/** + * @deprecated Use `@nx/module-federation/rspack` instead. This will be removed in Nx v22. + */ +export { + withModuleFederation, + withModuleFederationForSSR, +} from '@nx/module-federation/rspack'; diff --git a/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.spec.ts b/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.spec.ts new file mode 100644 index 00000000000000..4b0b741b0abba8 --- /dev/null +++ b/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.spec.ts @@ -0,0 +1,138 @@ +import { type Tree } from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import migrateWithMfImport from './migrate-with-mf-import-to-new-package'; + +describe('migrate-with-mf-import-to-new-package', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + tree.write( + 'apps/shell/project.json', + JSON.stringify({ + name: 'shell', + root: 'apps/shell', + sourceRoot: 'apps/shell/src', + projectType: 'application', + targets: { + serve: { + executor: '@nx/rspack:module-federation-dev-server', + options: {}, + }, + }, + }) + ); + }); + + it('should migrate the import correctly for withMf', async () => { + // ARRANGE + tree.write( + 'apps/shell/rspack.config.ts', + `import { withModuleFederation } from '@nx/rspack/module-federation';` + ); + + // ACT + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/rspack.config.ts', 'utf-8')) + .toMatchInlineSnapshot(` + "import { withModuleFederation } from '@nx/module-federation/rspack'; + " + `); + }); + + it('should migrate the require correctly for withMf', async () => { + // ARRANGE + tree.write( + 'apps/shell/rspack.config.js', + `const { withModuleFederation } = require('@nx/rspack/module-federation');` + ); + + // ACT + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/rspack.config.js', 'utf-8')) + .toMatchInlineSnapshot(` + "const { withModuleFederation } = require('@nx/module-federation/rspack'); + " + `); + }); + + it('should migrate the import correctly for withMfSSR', async () => { + // ARRANGE + tree.write( + 'apps/shell/webpack.config.ts', + `import { withModuleFederationForSSR } from '@nx/rspack/module-federation';` + ); + + // ACT + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/webpack.config.ts', 'utf-8')) + .toMatchInlineSnapshot(` + "import { withModuleFederationForSSR } from '@nx/module-federation/rspack'; + " + `); + }); + + it('should migrate the require correctly for withMfSSR', async () => { + // ARRANGE + tree.write( + 'apps/shell/rspack.config.js', + `const { withModuleFederationForSSR } = require('@nx/rspack/module-federation');` + ); + + // ACT + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/rspack.config.js', 'utf-8')) + .toMatchInlineSnapshot(` + "const { withModuleFederationForSSR } = require('@nx/module-federation/rspack'); + " + `); + }); + + describe('idempotent', () => { + it('should migrate the import correctly for withMf even when run twice', async () => { + // ARRANGE + tree.write( + 'apps/shell/webpack.config.ts', + `import { withModuleFederation } from '@nx/rspack/module-federation';` + ); + + // ACT + await migrateWithMfImport(tree); + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/webpack.config.ts', 'utf-8')) + .toMatchInlineSnapshot(` + "import { withModuleFederation } from '@nx/module-federation/rspack'; + " + `); + }); + + it('should migrate the require correctly for withMfSSR even when run twice', async () => { + // ARRANGE + tree.write( + 'apps/shell/rspack.config.js', + `const { withModuleFederationForSSR } = require('@nx/rspack/module-federation');` + ); + + // ACT + await migrateWithMfImport(tree); + await migrateWithMfImport(tree); + + // ASSERT + expect(tree.read('apps/shell/rspack.config.js', 'utf-8')) + .toMatchInlineSnapshot(` + "const { withModuleFederationForSSR } = require('@nx/module-federation/rspack'); + " + `); + }); + }); +}); diff --git a/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts b/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts new file mode 100644 index 00000000000000..8e9461633c0945 --- /dev/null +++ b/packages/rspack/src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package.ts @@ -0,0 +1,57 @@ +import { + type Tree, + formatFiles, + readProjectConfiguration, + visitNotIgnoredFiles, +} from '@nx/devkit'; +import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils'; +import { tsquery } from '@phenomnomnominal/tsquery'; + +const NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR = + 'ImportDeclaration > StringLiteral[value=@nx/rspack/module-federation], VariableStatement CallExpression:has(Identifier[name=require]) > StringLiteral[value=@nx/rspack/module-federation]'; +const NEW_IMPORT_PATH = `'@nx/module-federation/rspack'`; + +export default async function migrateWithMfImport(tree: Tree) { + const projects = new Set(); + + forEachExecutorOptions( + tree, + '@nx/rspack:module-federation-dev-server', + (options, project, target) => { + const projectConfig = readProjectConfiguration(tree, project); + projects.add(projectConfig.root); + } + ); + + for (const projectRoot of projects) { + visitNotIgnoredFiles(tree, projectRoot, (filePath) => { + if (!filePath.endsWith('.ts') && !filePath.endsWith('.js')) { + return; + } + let contents = tree.read(filePath, 'utf-8'); + if (!contents.includes('@nx/rspack/module-federation')) { + return; + } + + const ast = tsquery.ast(contents); + const importNodes = tsquery( + ast, + NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR + ); + + if (importNodes.length === 0) { + return; + } + const importPathNode = importNodes[0]; + + contents = `${contents.slice( + 0, + importPathNode.getStart() + )}${NEW_IMPORT_PATH}${contents.slice(importPathNode.getEnd())}`; + + tree.write(filePath, contents); + }); + } + + await formatFiles(tree); +} diff --git a/packages/rspack/src/utils/module-federation/public-api.ts b/packages/rspack/src/utils/module-federation/public-api.ts deleted file mode 100644 index 46e6b418f4cd5d..00000000000000 --- a/packages/rspack/src/utils/module-federation/public-api.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { withModuleFederation } from './with-module-federation/with-module-federation'; -import { withModuleFederationForSSR } from './with-module-federation/with-module-federation-ssr'; - -export { withModuleFederation, withModuleFederationForSSR };