Skip to content

withModuleFederation plugin overwrites config.optimization options #27201

@grigorigoldman

Description

@grigorigoldman

Current Behavior

I'm using NX-enhanced webpack plugins as per documentation to configure react and module federation, e.g.

import { withReact } from '@nx/react';
import { composePlugins, withNx } from '@nx/webpack';
import { withModuleFederation } from '@nx/react/module-federation';

export default composePlugins(
  withNx(),
  withReact(),
  withModuleFederation(config)
}

I have noticed that the optimization options are lost when withModuleFederation is invoked, e.g. when I do this:

export default composePlugins(
  withNx(),
  withReact(),
  (config) => {
    console.log({ optimization: config.optimization });
    return config;
  },
  withModuleFederation(config),
  (config) => {
    console.log({ optimization: config.optimization });
    return config;
  }
}

These are the config options before:

optimization: {
  sideEffects: true,
  minimize: false,
  minimizer: [Array],
  runtimeChunk: [Object],
  concatenateModules: true,
  emitOnErrors: false,
  moduleIds: 'deterministic',
  splitChunks: [Object]
}

and this is after:

optimization: {
  runtimeChunk: false
},

This is causing problems for me later on:

ERROR in main.js
main.js from Terser plugin
"Import" statement may only appear at the top level [main.js:12,0]
    at js_error (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:536:11)
    at TreeWalker.visit (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:11818:13)
    at TreeWalker._visit (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:6728:24)
    at AST_Import._walk (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:5122:28)
    at walk_body (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:3869:17)
    at AST_Function.<anonymous> (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:4368:17)
    at /Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:6729:21
    at TreeWalker.visit (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:11720:13)
    at TreeWalker._visit (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:6728:24)
    at AST_Function._walk (/Users/xxxx/workspace/xxxx/node_modules/terser/dist/bundle.min.js:4362:28)

Expected Behavior

I was expecting optimization options to be unchanged when withModuleFederation plugin is used.

GitHub Repo

No response

Steps to Reproduce

  1. Enable withNx, withReact, and withModuleFederation plugins
  2. Optimization options after withModuleFederation will not have all of the default values

Nx Report

NX   Report complete - copy this into the issue template

Node   : 18.18.0
OS     : darwin-x64
yarn   : 1.22.22

nx                 : 19.5.3
@nx/js             : 19.5.3
@nx/jest           : 19.5.3
@nx/linter         : 19.5.3
@nx/eslint         : 19.5.3
@nx/workspace      : 19.5.3
@nx/devkit         : 19.5.3
@nx/eslint-plugin  : 19.5.3
@nx/react          : 19.5.3
@nrwl/tao          : 19.5.3
@nx/web            : 19.5.3
@nx/webpack        : 19.5.3
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/webpack/plugin
@nx/eslint/plugin
@nx/jest/plugin

Failure Logs

No response

Package Manager Version

1.22.22

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions