Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: expose importAttributes to loaders via _importAttributes#20232

Open
samarthsinh2660 wants to merge 6 commits intowebpack:mainfrom
samarthsinh2660:feat/loader-import-attributes
Open

feat: expose importAttributes to loaders via _importAttributes#20232
samarthsinh2660 wants to merge 6 commits intowebpack:mainfrom
samarthsinh2660:feat/loader-import-attributes

Conversation

@samarthsinh2660
Copy link
Contributor

Summary

This PR exposes importAttributes to the loader interface so loaders can react to metadata on import/import() statements, as requested in #20228.

Today, the parser already understands import attributes, and they are used for things like rule matching, but loaders cannot see them. This change threads the parsed ImportAttributes from the dependency/resolve data into NormalModule and then into the loader context as _importAttributes, so loaders can read them via this._importAttributes.

High-level implementation:

  • Extend NormalModuleFactory to include importAttributes in the createData passed to NormalModule.
  • Store importAttributes on NormalModule instances.
  • Expose the stored attributes on the loader context as _importAttributes, following the pattern of other internal context fields like _module, _compilation, etc.
  • Update declarations/LoaderContext.d.ts to include _importAttributes?: Record<string, unknown>.
  • Add a configCases test case under test/configCases/loaders/import-attributes-loader-context:
    • A custom loader reads this._importAttributes and exports it.
    • One import with attributes and one without, asserting:
      • Attributes object is passed through when present.
      • undefined is produced when no attributes are present.

What kind of change does this PR introduce?

Feature.

It adds a new capability for loaders to inspect the import attributes of the module they are processing (via _importAttributes on the loader context).

Did you add tests for your changes?

Yes.

  • Added a new config test:
    • test/configCases/loaders/import-attributes-loader-context/webpack.config.js
    • test/configCases/loaders/import-attributes-loader-context/loader.js
    • test/configCases/loaders/import-attributes-loader-context/index.js
  • Ran targeted Jest tests for this case:
    • node node_modules/jest/bin/jest.js test/ConfigTestCases.basictest.js --no-coverage --runInBand --testNamePattern=import-attributes-loader-context
  • Also ran loader/import-attributes related config tests via --testNamePattern="loaders import-attributes" to ensure existing cases still pass.

On my Windows environment, running the full Jest suite surfaces unrelated snapshot differences (absolute paths vs <cwd>) and missing optional dependencies like @swc/core / esbuild, which appear to be environment-specific and not related to this change.

Does this PR introduce a breaking change?

No.

  • The change only adds a new internal property _importAttributes on the loader context.
  • Existing loaders and configurations continue to work as before.
  • No existing public APIs are removed or changed.

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

  • Loader API docs should mention that the loader context now provides _importAttributes (an object of import attributes when present, otherwise undefined) for modules that were imported with attributes.
  • It may be worth adding:
    • A short example of a loader that branches behavior based on _importAttributes (e.g., treating assets differently when with { type: "image", preload: true } is used).
    • A note that this is populated only when the module is reached via an import with attributes and is otherwise undefined.

@changeset-bot
Copy link

changeset-bot bot commented Dec 12, 2025

⚠️ No Changeset found

Latest commit: c5ad681

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 12, 2025

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing samarthsinh2660:feat/loader-import-attributes (c5ad681) with main (d2a124d)

Summary

✅ 72 untouched benchmarks

@snitin315
Copy link
Member

@samarthsinh2660 Could you please check the lint failure?

@samarthsinh2660
Copy link
Contributor Author

@samarthsinh2660 Could you please check the lint failure?

Thanks for the heads-up.
I’ll look into the lint failure and push a fix shortly

@samarthsinh2660 samarthsinh2660 force-pushed the feat/loader-import-attributes branch from aa878d8 to 9e3eaa7 Compare December 12, 2025 20:29
@hai-x
Copy link
Member

hai-x commented Dec 13, 2025

We also need to handle serialization and deserialization.

@samarthsinh2660
Copy link
Contributor Author

We also need to handle serialization and deserialization.

okay i will do it

@samarthsinh2660 samarthsinh2660 force-pushed the feat/loader-import-attributes branch from a7bb93d to c5ad681 Compare January 10, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants