Skip to content

[Bug] Module concatenation breaks namespace re-exports in v5.108.0 — runtime TypeError on 'import * as' pattern #21279

Description

@zxl-lxz

Bug report

What is the current behavior?

After upgrading to webpack 5.108.0 (from 5.107.2), production builds crash at runtime with:

Uncaught TypeError: Cannot read properties of undefined (reading 'Y')
    at Module.ieVersion (index.js:6:304314)

The error occurs when accessing a property on a namespace re-export object that is undefined at runtime. The pattern in question:

// util/env.js
export var ieVersion = typeof document !== 'undefined' ? document.documentMode : undefined;
export default { ieVersion };

// util/index.js
import * as _env from './env';
export var env = _env;   // <-- `env` is undefined at runtime in 5.108.0

// table/index.js (consumer)
import { env } from '../util';
var ieVersion = env.ieVersion;  // TypeError: Cannot read properties of undefined

'Y' in the error is the mangled name of ieVersion.

What is the expected behavior?

The namespace object (env) should be a valid module namespace object at runtime, as it was in 5.107.2 and all prior 5.x versions.

Steps to reproduce

  1. Create a library with a barrel util/index.js that does import * as _env from './env'; export var env = _env;
  2. The library's package.json declares "sideEffects": ["*.css"]
  3. A consumer module accesses env.someProperty at the top level (module initialization)
  4. Build with webpack 5.108.0 in production mode (mode: 'production')
  5. The bundle crashes at runtime because env is undefined

Setting optimization.concatenateModules: false resolves the issue.

Affected package

The reproduction uses @alifd/next@1.27.x (Alibaba Fusion Design), but the bug is in webpack's module concatenation logic and can affect any library using this pattern.

Root cause (likely)

The 5.108.0 release includes:

  • "Skipped pure single-star passthrough modules during export * re-exports"
  • "Deferred building unused re-export targets in side-effect-free barrel modules"
  • "Fixed merging logic for inner module top-level declarations inside concatenated modules"

One or more of these optimizations incorrectly treats the import * as X; export var y = X pattern as eliminable when the barrel module is marked side-effect-free, causing the namespace binding to be dropped from the concatenated output.

Workaround

optimization: {
  concatenateModules: false,
}

Other relevant information:

  • webpack version: 5.108.0 (works in 5.107.2)
  • Node.js version: 14.19.3 / 22.x (reproducible on both)
  • Operating System: Linux (CI) / macOS
  • Additional tools: TerserPlugin (standard, not esbuild)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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