next.js
b067991e - Add support for with `type: "text"` under a new experimental flag, following what webpack did (#89560)

Commit
2 days ago
Add support for with `type: "text"` under a new experimental flag, following what webpack did (#89560) ## Summary Adds support for the TC39 `import ... with { type: "text" }` proposal to Turbopack, allowing files to be imported as strings. This is gated behind a new experimental flag `turbopackImportTypeText`. ```js // next.config.js export default { experimental: { turbopackImportTypeText: true, }, } ``` ```js import text from './file.txt' with { type: 'text' } // text is a string containing the file contents ``` ### Related TC39 Proposals - **[`type: "text"`](https://github.com/tc39/proposal-import-text)** - Stage 2 - Import file contents as a string - **[`type: "bytes"`](https://github.com/nicolo-ribaudo/proposal-import-bytes)** - Stage 2.7 - Import file contents as a Uint8Array (already supported via `turbopackImportTypeBytes`) ### Implementation Details - Created `TextSourceTransform` which transforms any file into an ES module exporting the file contents as a string - Added `ResourceHasModifier` rule condition to match on ident modifiers, enabling proper module type resolution after source transforms - Refactored `BytesSourceTransform` to use the same pattern for consistency - Both transforms add a modifier to the ident (`text_module` / `bytes_module`) for uniqueness, allowing the same file to be imported both normally and with a type attribute Using a source transform is simpler and allows the modules to participate in other bundler optimizations more easily (scope hoisting in particular) This also fixes a few bugs where `with` imports would conflict with other module type rules: * if you attempted to import a `.js` file with `type:'bytes'` didn't work since the file extension matching would trigger first. * if you set a module type rule in your next config it would override the `with` attribute ## Test Plan - Expanded e2e tests at `test/e2e/turbopack-import-with-type/`
Author
Parents
Loading