Skip to content

fix(register): pass imports with import attributes through to the default loader#1054

Open
pdeveltere wants to merge 1 commit into
swc-project:masterfrom
pdeveltere:fix/esm-load-import-attributes
Open

fix(register): pass imports with import attributes through to the default loader#1054
pdeveltere wants to merge 1 commit into
swc-project:masterfrom
pdeveltere:fix/esm-load-import-attributes

Conversation

@pdeveltere

@pdeveltere pdeveltere commented Jul 9, 2026

Copy link
Copy Markdown

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/esm registered, these imports crash:

Exception during run: [Error:   x Expression expected
   ,-[/app/src/queries/query.sql:2:1]
 1 | SELECT ...
   :                     ^
Caused by:
    Syntax Error] { code: 'GenericFailure' }

The resolve hook already defers to nextResolve when import attributes are present, but the load hook does not: it takes the source returned by nextLoad and compiles it with swc as TypeScript, so the raw text content is fed to the parser.

Fix

Skip the load hook when context.importAttributes.type is set, mirroring the existing behaviour of the resolve hook. The default loader then handles the module (json was already covered by the context.format check above; this also covers text and any future attribute-driven formats).

Test

Added an integrate-module test that spawns a child Node process with --experimental-import-text --import=@swc-node/register/esm-register and imports a .txt file with { type: 'text' } from a .ts entry. 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).

✔ text import attributes should pass through to the default loader (306.208667ms)
ℹ tests 23
ℹ pass 23
ℹ fail 0

…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.
@CLAassistant

CLAassistant commented Jul 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants