@@ -700,6 +700,9 @@ changes:
700
700
- version: REPLACEME
701
701
pr-url: https://github.com/nodejs/node/pull/48842
702
702
description: Added ` initialize` hook to replace ` globalPreload` .
703
+ - version: REPLACEME
704
+ pr-url: https://github.com/nodejs/node/pull/49144
705
+ description: Removed ` globalPreload` .
703
706
- version:
704
707
- v18.6.0
705
708
- v16.17.0
@@ -775,10 +778,10 @@ This hook can send and receive data from a [`register`][] invocation, including
775
778
ports and other transferrable objects. The return value of ` initialize` must be
776
779
either:
777
780
778
- * ` undefined` ,
781
+ * ` undefined`
779
782
* 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
782
785
783
786
Loader code:
784
787
@@ -1042,78 +1045,6 @@ export async function load(url, context, nextLoad) {
1042
1045
In a more advanced scenario, this can also be used to transform an unsupported
1043
1046
source to a supported one (see [Examples](#examples) below).
1044
1047
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
-
1117
1048
### Examples
1118
1049
1119
1050
The various loader hooks can be used together to accomplish wide-ranging
0 commit comments