Skip to content

Commit 019b61b

Browse files
update ESM doc (remove globalPreload section
1 parent c9a86ea commit 019b61b

File tree

1 file changed

+6
-75
lines changed

1 file changed

+6
-75
lines changed

doc/api/esm.md

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ changes:
700700
- version: REPLACEME
701701
pr-url: https://github.com/nodejs/node/pull/48842
702702
description: Added `initialize` hook to replace `globalPreload`.
703+
- version: REPLACEME
704+
pr-url: https://github.com/nodejs/node/pull/49144
705+
description: Removed `globalPreload`.
703706
- version:
704707
- v18.6.0
705708
- v16.17.0
@@ -775,10 +778,10 @@ This hook can send and receive data from a [`register`][] invocation, including
775778
ports and other transferrable objects. The return value of `initialize` must be
776779
either:
777780
778-
* `undefined`,
781+
* `undefined`
779782
* something that can be posted as a message between threads (e.g. the input to
780-
[`port.postMessage`][]),
781-
* a `Promise` resolving to one of the aforementioned values.
783+
[`port.postMessage`][])
784+
* a `Promise` resolving to one of the aforementioned values
782785
783786
Loader code:
784787
@@ -1042,78 +1045,6 @@ export async function load(url, context, nextLoad) {
10421045
In a more advanced scenario, this can also be used to transform an unsupported
10431046
source to a supported one (see [Examples](#examples) below).
10441047
1045-
#### `globalPreload()`
1046-
1047-
<!-- YAML
1048-
changes:
1049-
- version:
1050-
- v18.6.0
1051-
- v16.17.0
1052-
pr-url: https://github.com/nodejs/node/pull/42623
1053-
description: Add support for chaining globalPreload hooks.
1054-
-->
1055-
1056-
> This hook will be removed in a future version. Use [`initialize`][] instead.
1057-
> When a loader has an `initialize` export, `globalPreload` will be ignored.
1058-
1059-
> In a previous version of this API, this hook was named
1060-
> `getGlobalPreloadCode`.
1061-
1062-
* `context` {Object} Information to assist the preload code
1063-
* `port` {MessagePort}
1064-
* Returns: {string} Code to run before application startup
1065-
1066-
Sometimes it might be necessary to run some code inside of the same global
1067-
scope that the application runs in. This hook allows the return of a string
1068-
that is run as a sloppy-mode script on startup.
1069-
1070-
Similar to how CommonJS wrappers work, the code runs in an implicit function
1071-
scope. The only argument is a `require`-like function that can be used to load
1072-
builtins like "fs": `getBuiltin(request: string)`.
1073-
1074-
If the code needs more advanced `require` features, it has to construct
1075-
its own `require` using `module.createRequire()`.
1076-
1077-
```js
1078-
export function globalPreload(context) {
1079-
return `\
1080-
globalThis.someInjectedProperty = 42;
1081-
console.log('I just set some globals!');
1082-
1083-
const { createRequire } = getBuiltin('module');
1084-
const { cwd } = getBuiltin('process');
1085-
1086-
const require = createRequire(cwd() + '/<preload>');
1087-
// [...]
1088-
`;
1089-
}
1090-
```
1091-
1092-
In order to allow communication between the application and the loader, another
1093-
argument is provided to the preload code: `port`. This is available as a
1094-
parameter to the loader hook and inside of the source text returned by the hook.
1095-
Some care must be taken in order to properly call [`port.ref()`][] and
1096-
[`port.unref()`][] to prevent a process from being in a state where it won't
1097-
close normally.
1098-
1099-
```js
1100-
/**
1101-
* This example has the application context send a message to the loader
1102-
* and sends the message back to the application context
1103-
*/
1104-
export function globalPreload({ port }) {
1105-
port.onmessage = (evt) => {
1106-
port.postMessage(evt.data);
1107-
};
1108-
return `\
1109-
port.postMessage('console.log("I went to the Loader and back");');
1110-
port.onmessage = (evt) => {
1111-
eval(evt.data);
1112-
};
1113-
`;
1114-
}
1115-
```
1116-
11171048
### Examples
11181049
11191050
The various loader hooks can be used together to accomplish wide-ranging

0 commit comments

Comments
 (0)