diff --git a/lib/src/legacy/importer.ts b/lib/src/legacy/importer.ts index 97ef9117..5977a3d5 100644 --- a/lib/src/legacy/importer.ts +++ b/lib/src/legacy/importer.ts @@ -86,10 +86,34 @@ export class LegacyImporterWrapper canonicalize( url: string, - options: {fromImport: boolean} + options: {fromImport: boolean; containingUrl: URL | null} ): PromiseOr { if (url.startsWith(endOfLoadProtocol)) return new URL(url); + // Emulate a base importer instead of using a real base importer, + // because we want to mark containingUrl as used, which is impossible + // in a real base importer. + if (options.containingUrl !== null) { + try { + const absoluteUrl = new URL(url, options.containingUrl).toString(); + const resolved = this.canonicalize(absoluteUrl, { + fromImport: options.fromImport, + containingUrl: null, + }); + if (resolved !== null) return resolved; + } catch (error: unknown) { + if ( + error instanceof TypeError && + isErrnoException(error) && + error.code === 'ERR_INVALID_URL' + ) { + // ignore + } else { + throw error; + } + } + } + if ( url.startsWith(legacyImporterProtocolPrefix) || url.startsWith(legacyImporterProtocol) diff --git a/lib/src/legacy/index.ts b/lib/src/legacy/index.ts index 404937cf..d7543e35 100644 --- a/lib/src/legacy/index.ts +++ b/lib/src/legacy/index.ts @@ -184,11 +184,20 @@ function convertStringOptions( ): StringOptions & {legacy: true} { const modernOptions = convertOptions(options, sync); - // Find the first non-NodePackageImporter to pass as legacy `importer` option. - // NodePackageImporter will be passed in `modernOptions.importers`. - const importer = modernOptions.importers?.find( - _importer => !(_importer instanceof NodePackageImporter) - ) as Importer | FileImporter; + // Use a no-op base importer, because the LegacyImporterWrapper will emulate + // the base importer by itself in order to mark containingUrl as accessed. + const importer = modernOptions.importers?.some( + importer => importer instanceof LegacyImporterWrapper + ) + ? { + canonicalize() { + return null; + }, + load() { + return null; + }, + } + : undefined; return { ...modernOptions,