From 4481a6b59bdaaa602e1ef4f442cbedf043d4d0d6 Mon Sep 17 00:00:00 2001 From: hanityx Date: Mon, 28 Sep 2026 23:43:22 +0900 Subject: [PATCH] doc: fix synchronous module hook examples Use node:fs for readFileSync in the synchronous import-map example. Preserve transformed source in the synchronous load example, skip null and WebAssembly sources, and decode byte sources with TextDecoder. Match the special-case URL returned by the preceding resolve example. Assisted-by: opencode Signed-off-by: hanityx --- doc/api/module.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/doc/api/module.md b/doc/api/module.md index a4f83a812f96..7900cd028365 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -1027,11 +1027,11 @@ a supported one, for example `yaml` to `module`. ```mjs import { registerHooks } from 'node:module'; -import { Buffer } from 'node:buffer'; +import { TextDecoder } from 'node:util'; function load(url, context, nextLoad) { // The hook can skip default loading and provide a custom source code. - if (url === 'special-module') { + if (url === 'file:///path/to/special-module.mjs') { return { source: 'export const special = 42;', format: 'module', @@ -1042,11 +1042,14 @@ function load(url, context, nextLoad) { // It's possible to modify the source code loaded by the next - possibly default - step, // for example, replacing 'foo' with 'bar' in the source code of the module. const result = nextLoad(url, context); + if (result.source == null || result.format === 'wasm') { + return result; + } const source = typeof result.source === 'string' ? - result.source : Buffer.from(result.source).toString('utf8'); + result.source : new TextDecoder().decode(result.source); return { - source: source.replace(/foo/g, 'bar'), ...result, + source: source.replace(/foo/g, 'bar'), }; } @@ -1816,7 +1819,7 @@ export async function resolve(specifier, context, nextResolve) { ```mjs // import-map-sync-hooks.js -import fs from 'node:fs/promises'; +import fs from 'node:fs'; import module from 'node:module'; const { imports } = JSON.parse(fs.readFileSync('import-map.json', 'utf-8'));