fix(register): pass imports with import attributes through to the default loader#1054
Open
pdeveltere wants to merge 1 commit into
Open
fix(register): pass imports with import attributes through to the default loader#1054pdeveltere wants to merge 1 commit into
pdeveltere wants to merge 1 commit into
Conversation
…ault loader
The resolve hook already defers to nextResolve when import attributes are
present, but the load hook still compiled the resolved source as TypeScript.
This breaks text imports (`with { type: 'text' }`, Node.js 26.5+ behind
--experimental-import-text): the raw text was fed to swc and failed to parse.
Skip the load hook when import attributes are present so the default loader
handles the module, mirroring the existing resolve behaviour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Node.js 26.5.0 introduced text imports (
import sql from './query.sql' with { type: 'text' }, behind--experimental-import-text, nodejs/node#62300). With@swc-node/register/esmregistered, these imports crash:The
resolvehook already defers tonextResolvewhen import attributes are present, but theloadhook does not: it takes the source returned bynextLoadand compiles it with swc as TypeScript, so the raw text content is fed to the parser.Fix
Skip the
loadhook whencontext.importAttributes.typeis set, mirroring the existing behaviour of theresolvehook. The default loader then handles the module (jsonwas already covered by thecontext.formatcheck above; this also coverstextand any future attribute-driven formats).Test
Added an
integrate-moduletest that spawns a child Node process with--experimental-import-text --import=@swc-node/register/esm-registerand imports a.txtfilewith { type: 'text' }from a.tsentry. Skipped on Node < 26.5 (the current CI matrix of 22/24 skips it; it runs and passes on 26.5.0, and fails without the fix).