@@ -78,6 +78,101 @@ isBuiltin('fs'); // true
7878isBuiltin (' wss' ); // false
7979` ` `
8080
81+ ### ` module .register ()`
82+
83+ <!-- YAML
84+ added: REPLACEME
85+ -->
86+
87+ In addition to using the ` -- experimental- loader` option in the CLI,
88+ loaders can be registered programmatically using the
89+ ` module .register ()` method.
90+
91+ ` ` ` mjs
92+ import { register } from ' node:module' ;
93+
94+ register (' http-to-https' , import .meta.url);
95+
96+ // Because this is a dynamic `import()`, the `http-to-https` hooks will run
97+ // before importing `./my-app.mjs`.
98+ await import (' ./my-app.mjs' );
99+ ` ` `
100+
101+ In the example above, we are registering the ` http- to- https` loader,
102+ but it will only be available for subsequently imported modules—in
103+ this case, ` my- app .mjs ` . If the ` await import (' ./my-app.mjs' )` had
104+ instead been a static ` import ' ./my-app.mjs' ` , _the app would already
105+ have been loaded_ before the ` http-to-https` hooks were
106+ registered. This is part of the design of ES modules, where static
107+ imports are evaluated from the leaves of the tree first back to the
108+ trunk. There can be static imports _within_ ` my-app.mjs` , which
109+ will not be evaluated until ` my-app.mjs` is when it's dynamically
110+ imported.
111+
112+ The ` --experimental-loader` flag of the CLI can be used together
113+ with the ` register` function; the loaders registered with the
114+ function will follow the same evaluation chain of loaders registered
115+ within the CLI:
116+
117+ ` ` ` console
118+ node \
119+ -- experimental- loader unpkg \
120+ -- experimental- loader http- to- https \
121+ -- experimental- loader cache- buster \
122+ entrypoint .mjs
123+ ` ` `
124+
125+ ` ` ` mjs
126+ // entrypoint.mjs
127+ import { URL } from ' node:url' ;
128+ import { register } from ' node:module' ;
129+
130+ const loaderURL = new URL (' ./my-programmatically-loader.mjs' , import .meta.url);
131+
132+ register (loaderURL);
133+ await import (' ./my-app.mjs' );
134+ ` ` `
135+
136+ The ` my- programmatic- loader .mjs ` can leverage ` unpkg` ,
137+ ` http- to- https` , and ` cache- buster` loaders.
138+
139+ It's also possible to use ` register` more than once:
140+
141+ ` ` ` mjs
142+ // entrypoint.mjs
143+ import { URL } from ' node:url' ;
144+ import { register } from ' node:module' ;
145+
146+ register (new URL (' ./first-loader.mjs' , import .meta.url));
147+ register (' ./second-loader.mjs' , import .meta.url);
148+ await import (' ./my-app.mjs' );
149+ ` ` `
150+
151+ Both loaders (` first- loader .mjs ` and ` second- loader .mjs ` ) can use
152+ all the resources provided by the loaders registered in the CLI. But
153+ remember that they will only be available in the next imported
154+ module (` my- app .mjs ` ). The evaluation order of the hooks when
155+ importing ` my- app .mjs ` and consecutive modules in the example above
156+ will be:
157+
158+ ` ` ` console
159+ resolve: second- loader .mjs
160+ resolve: first- loader .mjs
161+ resolve: cache- buster
162+ resolve: http- to- https
163+ resolve: unpkg
164+ load: second- loader .mjs
165+ load: first- loader .mjs
166+ load: cache- buster
167+ load: http- to- https
168+ load: unpkg
169+ globalPreload: second- loader .mjs
170+ globalPreload: first- loader .mjs
171+ globalPreload: cache- buster
172+ globalPreload: http- to- https
173+ globalPreload: unpkg
174+ ` ` `
175+
81176### ` module .syncBuiltinESMExports ()`
82177
83178<!-- YAML
0 commit comments