@@ -146,125 +146,6 @@ fs.readFileSync = () => Buffer.from('Hello, ESM');
146146fs .readFileSync === readFileSync;
147147` ` `
148148
149- ## Loader hooks
150-
151- <!-- type=misc -->
152-
153- To customize the default module resolution, loader hooks can optionally be
154- provided via a ` -- loader ./ loader- name .mjs ` argument to Node.js.
155-
156- When hooks are used they only apply to ES module loading and not to any
157- CommonJS modules loaded.
158-
159- ### Resolve hook
160-
161- The resolve hook returns the resolved file URL and module format for a
162- given module specifier and parent file URL:
163-
164- ` ` ` js
165- const baseURL = new URL (' file://' );
166- baseURL .pathname = ` ${ process .cwd ()} /` ;
167-
168- export async function resolve (specifier ,
169- parentModuleURL = baseURL ,
170- defaultResolver ) {
171- return {
172- url: new URL (specifier, parentModuleURL).href ,
173- format: ' esm'
174- };
175- }
176- ` ` `
177-
178- The ` parentModuleURL` is provided as ` undefined ` when performing main Node.js
179- load itself.
180-
181- The default Node.js ES module resolution function is provided as a third
182- argument to the resolver for easy compatibility workflows.
183-
184- In addition to returning the resolved file URL value, the resolve hook also
185- returns a ` format` property specifying the module format of the resolved
186- module. This can be one of the following:
187-
188- | ` format` | Description |
189- | --- | --- |
190- | ` ' esm' ` | Load a standard JavaScript module |
191- | ` ' builtin' ` | Load a node builtin CommonJS module |
192- | ` ' dynamic' ` | Use a [dynamic instantiate hook][] |
193-
194- For example, a dummy loader to load JavaScript restricted to browser resolution
195- rules with only JS file extension and Node.js builtin modules support could
196- be written:
197-
198- ` ` ` js
199- import path from ' path' ;
200- import process from ' process' ;
201- import Module from ' module' ;
202-
203- const builtins = Module .builtinModules ;
204- const JS_EXTENSIONS = new Set ([' .js' , ' .mjs' ]);
205-
206- const baseURL = new URL (' file://' );
207- baseURL .pathname = ` ${ process .cwd ()} /` ;
208-
209- export function resolve (specifier , parentModuleURL = baseURL , defaultResolve ) {
210- if (builtins .includes (specifier)) {
211- return {
212- url: specifier,
213- format: ' builtin'
214- };
215- }
216- if (/ ^ \. {0,2} [/] / .test (specifier) !== true && ! specifier .startsWith (' file:' )) {
217- // For node_modules support:
218- // return defaultResolve(specifier, parentModuleURL);
219- throw new Error (
220- ` imports must begin with '/', './', or '../'; '${ specifier} ' does not` );
221- }
222- const resolved = new URL (specifier, parentModuleURL);
223- const ext = path .extname (resolved .pathname );
224- if (! JS_EXTENSIONS .has (ext)) {
225- throw new Error (
226- ` Cannot load file with non-JavaScript file extension ${ ext} .` );
227- }
228- return {
229- url: resolved .href ,
230- format: ' esm'
231- };
232- }
233- ` ` `
234-
235- With this loader, running:
236-
237- ` ` ` console
238- NODE_OPTIONS = ' --experimental-modules --loader ./custom-loader.mjs' node x .js
239- ` ` `
240-
241- would load the module ` x .js ` as an ES module with relative resolution support
242- (with ` node_modules` loading skipped in this example).
243-
244- ### Dynamic instantiate hook
245-
246- To create a custom dynamic module that doesn't correspond to one of the
247- existing ` format` interpretations, the ` dynamicInstantiate` hook can be used.
248- This hook is called only for modules that return ` format: ' dynamic' ` from
249- the ` resolve` hook.
250-
251- ` ` ` js
252- export async function dynamicInstantiate (url ) {
253- return {
254- exports: [' customExportName' ],
255- execute : (exports ) => {
256- // get and set functions provided for pre-allocated export names
257- exports .customExportName .set (' value' );
258- }
259- };
260- }
261- ` ` `
262-
263- With the list of module exports provided upfront, the ` execute` function will
264- then be called at the exact point of module evaluation order for that module
265- in the import tree.
266-
267149[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
268- [dynamic instantiate hook]: #esm_dynamic_instantiate_hook
269150[` module .createRequireFromPath ()` ]: modules.html#modules_module_createrequirefrompath_filename
270151[ESM Minimal Kernel]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md
0 commit comments