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
  • Loading branch information
Coly010 committed Nov 22, 2024
1 parent 432d9d3 commit 0db7672
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 34 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
@@ -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');
"
`);
});
});
});
Loading

0 comments on commit 0db7672

Please sign in to comment.