next.js
740d55cd - Turbopack: Add inline loader configuration via import attributes (#89644)

Commit
50 days ago
Turbopack: Add inline loader configuration via import attributes (#89644) ## What? Adds support for inline loader configuration in Turbopack via import attributes. Developers can apply webpack-compatible loaders to individual imports using the `with` clause, without needing global `turbopack.rules` configuration. ```tsx import rawText from '../data.txt' with { turbopackLoader: 'raw-loader', turbopackAs: '*.js' } import value from '../data.js' with { turbopackLoader: 'string-replace-loader', turbopackLoaderOptions: '{"search":"PLACEHOLDER","replace":"replaced value"}' } ``` ## Why? Global `turbopack.rules` applies loaders to all files matching a pattern. This PR provides per-import granularity — useful when only a specific import needs special treatment, avoiding side effects on other imports of the same file type. ## How? ### Import attributes parsing (`turbopack-ecmascript/src/analyzer/imports.rs`) - Extended `ImportAnnotations` to parse four new attributes: `turbopackLoader`, `turbopackLoaderOptions`, `turbopackAs`, `turbopackModuleType` - All values are strings per TC39 import attributes spec (`turbopackLoaderOptions` is a JSON-encoded string) - Added unit tests for the parser ### New structured loader type (`turbopack-core/src/loader.rs`) - Added `WebpackLoaderItem` struct shared between inline and global loader paths ### Reference type plumbing (`turbopack-core/src/reference_type.rs`, `turbopack-ecmascript/src/references/esm/base.rs`) - Added `ImportWithTurbopackUse` variant to `EcmaScriptModulesReferenceSubType` carrying the parsed loader, rename_as, and module_type - ESM reference resolution maps the parsed annotations to this new subtype ### Loader execution (`turbopack/src/lib.rs`) - In `process_default_internal`, detects `ImportWithTurbopackUse` references and applies the loader as a source transform via the existing webpack loader infrastructure - Requires `enable_webpack_loaders` to be configured (same as global rules) - Supports `turbopackAs` (re-processes with new extension) and `turbopackModuleType` (directly sets module type) - Avoids infinite recursion by switching to plain `Import` reference type on re-processing ### Documentation (`docs/.../turbopack.mdx`) - Added section documenting the four import attributes with examples - Added version history entry for v16.2.0 ### Testing (`test/development/app-dir/turbopack-import-assertions-use/`) - E2e test covering raw loader, replace loader with options, `turbopackModuleType: 'ecmascript'`, and `turbopackModuleType: 'json'` - Turbopack-only (skipped for webpack)
Author
Parents
Loading