Skip to content

Commit

Permalink
feat(module-federation): move withModuleFederation for rspack to new …
Browse files Browse the repository at this point in the history
…package (#29045)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
The `withModuleFederation` helper currently lives in the `@nx/rspack`
package.
With the goal of consolidating the module federation support into a
single package, this introduces a divergence in where module-federation
support lies


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Move `withModuleFederation` helper for angular into the
`@nx/module-federation` package, exposed via
`@nx/module-federation/rspack`.
Adds a migration to migrate existing projects to use the new package

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
Coly010 authored Nov 22, 2024
1 parent 9526560 commit 840d935
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 43 deletions.
2 changes: 2 additions & 0 deletions packages/module-federation/rspack.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
mapRemotes,
mapRemotesForSSR,
getDependentPackagesForProject,
} from '@nx/module-federation';
} from '../../utils';

export function getFunctionDeterminateRemoteUrl(isServer = false) {
const target = 'serve';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'`;

Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion packages/rspack/migrations.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 7 additions & 1 deletion packages/rspack/module-federation.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('Convert webpack', () => {

expect(tree.exists('demo/rspack.config.ts')).toBeTruthy();
expect(tree.read('demo/rspack.config.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { withModuleFederation } from '@nx/rspack/module-federation';
"import { withModuleFederation } from '@nx/module-federation/rspack';
import { withReact } from '@nx/rspack';
import { withNx } from '@nx/rspack';
import { composePlugins } from '@nx/rspack';
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('Convert webpack', () => {
expect(tree.exists('remote1/rspack.config.ts')).toBeTruthy();
expect(tree.read('remote1/rspack.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { withModuleFederation } from '@nx/rspack/module-federation';
"import { withModuleFederation } from '@nx/module-federation/rspack';
import { withReact } from '@nx/rspack';
import { withNx } from '@nx/rspack';
import { composePlugins } from '@nx/rspack';
Expand Down Expand Up @@ -348,7 +348,7 @@ describe('Convert webpack', () => {
expect(tree.exists('remote2/rspack.config.ts')).toBeTruthy();
expect(tree.read('remote2/rspack.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { withModuleFederation } from '@nx/rspack/module-federation';
"import { withModuleFederation } from '@nx/module-federation/rspack';
import { withReact } from '@nx/rspack';
import { withNx } from '@nx/rspack';
import { composePlugins } from '@nx/rspack';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function transformModuleFederationConfig(
endIndex++;
}

const newContents = `const { ModuleFederationConfig } = require('@nx/rspack/module-federation');
const newContents = `const { ModuleFederationConfig } = require('@nx/module-federation');
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down Expand Up @@ -205,7 +205,7 @@ function transformWithModuleFederation(
endIndex++;
}

const newContents = `const { withModuleFederation } = require('@nx/rspack/module-federation');
const newContents = `const { withModuleFederation } = require('@nx/module-federation/rspack');
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down Expand Up @@ -238,7 +238,7 @@ function transformWithModuleFederationSSR(
endIndex++;
}

const newContents = `const { withModuleFederationForSSR } = require('@nx/rspack/module-federation');
const newContents = `const { withModuleFederationForSSR } = require('@nx/module-federation/rspack');
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function transformWithModuleFederation(
endIndex++;
}

const newContents = `import { withModuleFederation } from '@nx/rspack/module-federation';
const newContents = `import { withModuleFederation } from '@nx/module-federation/rspack';
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down Expand Up @@ -205,7 +205,7 @@ function transformModuleFederationConfig(
endIndex++;
}

const newContents = `import { ModuleFederationConfig } from '@nx/rspack/module-federation';
const newContents = `import { ModuleFederationConfig } from '@nx/module-federation';
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down Expand Up @@ -238,7 +238,7 @@ function transformWithModuleFederationSSR(
endIndex++;
}

const newContents = `import { withModuleFederationForSSR } from '@nx/rspack/module-federation';
const newContents = `import { withModuleFederationForSSR } from '@nx/module-federation/rspack';
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;

tree.write(configPath, newContents);
Expand Down
Loading

0 comments on commit 840d935

Please sign in to comment.