node/test/fixtures/es-module-loaders/loader-resolve-dynamic-import.mjs
Izaak Schroeder a2fc4a383e
esm: unflag Module.register and allow nested loader import()
Major functional changes:

- Allow `import()` to work within loaders that require other loaders,
- Unflag the use of `Module.register`.

A new interface `Customizations` has been created in order to unify
`ModuleLoader` (previously `DefaultModuleLoader`), `Hooks` and
`CustomizedModuleLoader` all of which now implement it:

```ts
interface LoadResult {
  format: ModuleFormat;
  source: ModuleSource;
}

interface ResolveResult {
  format: string;
  url: URL['href'];
}

interface Customizations {
  allowImportMetaResolve: boolean;
  load(url: string, context: object): Promise<LoadResult>
  resolve(
    originalSpecifier:
    string, parentURL: string,
    importAssertions: Record<string, string>
  ): Promise<ResolveResult>
  resolveSync(
    originalSpecifier:
    string, parentURL: string,
    importAssertions: Record<string, string>
  ) ResolveResult;
  register(specifier: string, parentUrl: string): any;
  forceLoadHooks(): void;
  importMetaInitialize(meta, context, loader): void;
}
```

The `ModuleLoader` class now has `setCustomizations` which takes an
object of this shape and delegates its responsibilities to this object
if present.

Note that two properties `allowImportMetaResolve` and `resolveSync`
exist now as a mechanism for `import.meta.resolve` – since `Hooks`
does not implement `resolveSync` other loaders cannot use
`import.meta.resolve`; `allowImportMetaResolve` is a way of checking
for that case instead of invoking `resolveSync` and erroring.

Fixes https://github.com/nodejs/node/issues/48515
Closes https://github.com/nodejs/node/pull/48439

PR-URL: https://github.com/nodejs/node/pull/48559
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-07-19 10:33:47 +00:00

15 lines
401 B
JavaScript

import { writeSync } from 'node:fs';
export async function resolve(specifier, context, next) {
if (specifier === 'node:fs' || specifier.includes('loader')) {
return next(specifier);
}
// Here for asserting dynamic import
await import('xxx/loader-resolve-passthru.mjs');
writeSync(1, 'resolve dynamic import' + '\n'); // Signal that this specific hook ran
return next(specifier);
}