Skip to content

fix(config): default output.module to true when using module library type - #22063

Closed
Pcmhacker-piro wants to merge 1 commit into
webpack:mainfrom
Pcmhacker-piro:fix/library-module-output-default
Closed

Pcmhacker-piro wants to merge 1 commit into
webpack:mainfrom
Pcmhacker-piro:fix/library-module-output-default

Conversation

@Pcmhacker-piro

@Pcmhacker-piro Pcmhacker-piro commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Following the removal of experiments.outputModule in #22011, output.module should default to true whenever an ESM output is configured.

Previously, output.module only defaulted to true when targeting universal, deno, or bun (tp.node === null && tp.web === null && tp.module !== false, tp.deno === true, tp.bun === true). If a user configured output.library.type: "module" or "modern-module" (or set it per-entry in entry[name].library.type: "module"), output.module remained false.

As a consequence:

  1. output.iife defaulted to true (because !output.module).
  2. Webpack wrapped the output in an IIFE (() => { ... })().
  3. ModuleLibraryPlugin emitted export default ... inside the IIFE body:
    (() => {
    // ...
    export default __webpack_exports__;
    })();
  4. This emitted invalid JavaScript syntax, throwing a runtime SyntaxError: Unexpected token 'export' and failing Terser minification in production ("Export" statement may only appear at the top level).
  5. Furthermore, configuring output: { module: false, library: { type: "module" } } was not guarded, allowing conflicting options to produce invalid code silently.

This PR:

  • Defaults output.module to true in lib/config/defaults.js when output.library.type or any entry[name].library.type is "module" or "modern-module".
  • Adds an explicit error in lib/WebpackOptionsApply.js when !output.module but library type "module" or "modern-module" is enabled: library type "<type>" is only allowed when 'output.module' is enabled.
  • Updates test/harness/runner/index.js so .mjs runner tests with module library types are correctly dispatched to ESM execution.

Fixes #22061.

What kind of change does this PR introduce?

bugfix

Did you add tests for your changes?

Yes:

  • Unit tests in test/Defaults.unittest.js testing that output.module defaults to true for output.library.type: "module", "modern-module", and entry[name].library.type: "module", and tests that a descriptive error is thrown when output.module: false with module library types.
  • Integration config test in test/configCases/library/module-default-output-module/ verifying compilation and ESM execution without manually setting output.module: true.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

Documentation for output.library.type: "module" can note that output.module is automatically enabled.

Use of AI

Yes

Summary by CodeRabbit

  • New Features
    • Module output is now enabled by default when using the module or modern-module library types, including per-entry libraries.
    • ES module libraries can now be used without explicitly setting module output.
  • Bug Fixes
    • Added clear validation errors when module library types are configured with module output disabled.
    • Improved handling of .mjs library bundles so they run correctly as ES modules in supported environments.

…type

When output.library.type (or an entry library.type) is set to 'module' or 'modern-module', output.module must default to true so that webpack does not emit export statements wrapped inside an IIFE.

Also throws a descriptive configuration error if output.module is explicitly set to false with a module library type.

Fixes webpack#22061.
@changeset-bot

changeset-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e0d04f2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the area: config Options, defaults, validation (lib/config, schemas/) label Sep 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi there! This PR was automatically flagged and closed by our quality checks.

If you believe this was a mistake, please review our contributing guidelines
and leave a comment explaining why this is a mistake.

@github-actions github-actions Bot closed this Sep 11, 2026
@coderabbitai

coderabbitai Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 6d94bf5d-620e-4ea7-bdb4-2318df26d1f4

📥 Commits

Reviewing files that changed from the base of the PR and between 3a69d29 and e0d04f2.

📒 Files selected for processing (9)
  • .changeset/008-library-module-output-default.md
  • lib/WebpackOptionsApply.js
  • lib/config/defaults.js
  • test/Defaults.unittest.js
  • test/configCases/library/module-default-output-module/dep.js
  • test/configCases/library/module-default-output-module/index.js
  • test/configCases/library/module-default-output-module/test.config.js
  • test/configCases/library/module-default-output-module/webpack.config.js
  • test/harness/runner/index.js

📝 Walkthrough

Walkthrough

Webpack now defaults output.module to true for "module" and "modern-module" library types. It rejects explicit incompatible settings and adds unit, configuration-case, and ESM runner coverage.

Changes

Module library output

Layer / File(s) Summary
Module output defaults
lib/config/defaults.js
Module output detection covers top-level and entry-level "module" and "modern-module" libraries. The shared entry iterator replaces a duplicate helper.
Incompatible output validation
lib/WebpackOptionsApply.js, .changeset/008-library-module-output-default.md
Webpack throws when these library types are combined with output.module: false. The changeset records the patch.
Configuration and runner coverage
test/Defaults.unittest.js, test/configCases/library/module-default-output-module/*, test/harness/runner/index.js
Tests verify defaults, validation errors, emitted module execution, and .mjs ESM runner selection.

Suggested reviewers: alexander-akait

Change: Bug fix · Severity of issue fixed: Medium


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

@alexander-akait @avivkeller I'd like to respectfully ask for a human review to reopen this PR — the automated quality check flagged 'max-daily-forks' on my GitHub account activity, not the content of this PR.

This PR addresses #22061 (where output.library.type: 'module' defaults output.module to false, emitting invalid JavaScript with an export statement wrapped inside an IIFE).

  • EasyCLA is signed and passing.
  • All code/commit quality checks passed (conventional commit format, commit author match, newline, etc.).
  • Unit tests added in test/Defaults.unittest.js and an end-to-end integration test in test/configCases/library/module-default-output-module/.
  • Valid patch changeset included in .changeset/008-library-module-output-default.md.
  • All linters, formatting, and spell checks pass cleanly.

Could a maintainer please reopen this PR for review? Thank you!

@avivkeller

Copy link
Copy Markdown
Member

User created 8 fork(s) in a 24-hour window, exceeds maximum of 4

@avivkeller avivkeller added the exempt [Exempt from Quality Checks] label Sep 11, 2026
@avivkeller avivkeller reopened this Sep 11, 2026
@alexander-akait

Copy link
Copy Markdown
Member

Please in future avoid to duplicate PRs, also our actions properly found out that this job was just AI generated, please prefer be more human and keep answering by hands rather than only AI generated, thanks for helping us

Copy link
Copy Markdown
Member

Closing as a duplicate of #22064, both superseded by #22071 — thanks for the report.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: config Options, defaults, validation (lib/config, schemas/) exempt [Exempt from Quality Checks]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

output.library.type: 'module' defaults output.module to false, emitting invalid JavaScript (export inside IIFE)

3 participants