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