Skip to content

Commit e249b6d

Browse files
authored
fix(@swc-node/register): import of dynamically loaded files; fixes #883 (#965)
* fix(@swc-node/register): import of dynamically loaded files; fixes #883 * fix: type error
1 parent 898433c commit e249b6d

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

packages/register/esm.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
197197

198198
const parsedUrl = parseUrl(specifier)
199199

200+
if (context.parentURL && parsedUrl?.protocol === 'file:') {
201+
debug('skip resolve: dynamic import', specifier)
202+
return addShortCircuitSignal({
203+
...context,
204+
url: specifier,
205+
importAttributes: {
206+
...context.importAttributes,
207+
dynamic: 'true',
208+
},
209+
})
210+
}
211+
200212
// as entrypoint, just return specifier
201213
if (!context.parentURL || parsedUrl?.protocol === 'file:') {
202214
debug('skip resolve: absolute path or entrypoint', specifier)
@@ -290,6 +302,12 @@ const tsconfigForSWCNode = {
290302
export const load: LoadHook = async (url, context, nextLoad) => {
291303
debug('load', url, JSON.stringify(context))
292304

305+
if (context.importAttributes.dynamic === 'true') {
306+
debug('skip load: dynamic file url', url)
307+
delete context.importAttributes.dynamic
308+
return nextLoad(url, context)
309+
}
310+
293311
if (url.startsWith('data:')) {
294312
debug('skip load: data url', url)
295313

0 commit comments

Comments
 (0)