Skip to content

fix(webpack): handle package.json exports field for non-buildable libs#31444

Merged
Coly010 merged 1 commit intomasterfrom
issue-31419-fix
Jun 9, 2025
Merged

fix(webpack): handle package.json exports field for non-buildable libs#31444
Coly010 merged 1 commit intomasterfrom
issue-31419-fix

Conversation

@Coly010
Copy link
Copy Markdown
Contributor

@Coly010 Coly010 commented Jun 3, 2025

Current Behavior
The webpack and rspack plugins for handling non-buildable libraries don't properly process the exports field in package.json. They incorrectly assume libraries have only a single entry point, typically through a barrel file (index.ts).

When a library defines multiple export paths using the exports field (e.g., "./": "./src/.ts"), the plugins fail to generate the correct allowlist patterns for webpack externals. This causes build failures when trying to use non-buildable libraries that expose multiple entry points without barrel files.

Expected Behavior
The webpack and rspack plugins should properly parse the exports field from package.json and generate appropriate allowlist patterns for all exported subpaths. This includes:

Handling wildcard patterns ("./": "./src/.ts")
Processing conditional exports (import/require/development)
Supporting exact subpath exports ("./utils": "./src/utils.ts")
Escaping special characters in package names for regex patterns
Gracefully falling back to reading package.json directly when metadata is unavailable

@Coly010 Coly010 self-assigned this Jun 3, 2025
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview Jun 9, 2025 8:43am

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented Jun 3, 2025

View your CI Pipeline Execution ↗ for commit 4b49c31.

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 38m 39s View ↗
nx run-many -t check-imports check-commit check... ✅ Succeeded 17s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 2s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 5s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 4s View ↗
nx documentation ✅ Succeeded 1m 17s View ↗

☁️ Nx Cloud last updated this comment at 2025-06-09 09:15:57 UTC

- Add utility to parse exports field and generate regex patterns for webpack allowlist
- Convert wildcard exports like './*': './src/*.ts' to regex patterns
- Support conditional exports (import/require/development/default)
- Handle exact subpath exports and mixed export patterns
- Maintain backward compatibility with libraries without exports field
- Add comprehensive unit tests for all export patterns

Fixes #31419

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@Coly010
Copy link
Copy Markdown
Contributor Author

Coly010 commented Jun 9, 2025

@claude provide a good description for this PR

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Jun 9, 2025

Claude finished @Coly010's task in 1m 30s —— View job


PR Description Complete ✅

Here's a comprehensive description for this PR:


Current Behavior

The webpack and rspack plugins for handling non-buildable libraries don't properly process the exports field in package.json. They incorrectly assume libraries have only a single entry point, typically through a barrel file (index.ts).

When a library defines multiple export paths using the exports field (e.g., "./*": "./src/*.ts"), the plugins fail to generate the correct allowlist patterns for webpack externals. This causes build failures when trying to use non-buildable libraries that expose multiple entry points without barrel files.

Expected Behavior

The webpack and rspack plugins should properly parse the exports field from package.json and generate appropriate allowlist patterns for all exported subpaths. This includes:

  • Handling wildcard patterns ("./*": "./src/*.ts")
  • Processing conditional exports (import/require/development)
  • Supporting exact subpath exports ("./utils": "./src/utils.ts")
  • Escaping special characters in package names for regex patterns
  • Gracefully falling back to reading package.json directly when metadata is unavailable

Changes Made

Core Features Added:

  • createAllowlistFromExports(): New function that parses package.json exports and creates allowlist patterns
  • Wildcard support: Converts export patterns like "./*" into proper regex patterns
  • Conditional export resolution: Handles complex export maps with import/require/development conditions
  • Fallback mechanism: Reads package.json directly when project metadata doesn't contain exports

Files Modified:

  • packages/webpack/src/plugins/nx-webpack-plugin/lib/utils.ts - Added exports parsing logic
  • packages/rspack/src/plugins/utils/get-non-buildable-libs.ts - Same logic for rspack
  • Added comprehensive test suites for both packages

Implementation Details:

  • Exports are processed with priority: development > import > require > default
  • Special characters in package names are properly escaped for regex matching
  • Main export (.) is handled separately to avoid duplication
  • Graceful error handling when package.json reading fails

Related Issue(s)

Fixes #31419


This description provides the context and technical details needed for reviewers to understand the scope and impact of the changes.


@Coly010 Coly010 marked this pull request as ready for review June 9, 2025 09:21
@Coly010 Coly010 requested a review from a team as a code owner June 9, 2025 09:21
@Coly010 Coly010 requested a review from leosvelperez June 9, 2025 09:21
@Coly010 Coly010 merged commit f9c427a into master Jun 9, 2025
8 checks passed
@Coly010 Coly010 deleted the issue-31419-fix branch June 9, 2025 12:58
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants