Skip to content

Commit 6948c7c

Browse files
committed
example doc wip
1 parent 27ee9f1 commit 6948c7c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/api/esm.md

+24
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,30 @@ const require = createRequire(cwd() + '/<preload>');
828828
}
829829
```
830830
831+
In order to allow communication between the application and the loader another
832+
argument is provided to the preload code `port`. This is available as an
833+
parameter to the loader hook and inside of the source text returned by the hook.
834+
Some care must be taken in order to properly `ref()` and `unref()` the
835+
`MessagePort` to prevent a process from being in a state where it won't close
836+
normally.
837+
838+
```js
839+
/**
840+
* This example causes
841+
* @param {object} utilities
842+
* @param {MessagePort} utilities.port
843+
*/
844+
export function getGlobalPreloadCode({port}) {
845+
port.onmessage = (evt) => {
846+
// ...
847+
};
848+
return `\
849+
port.postMessage('I went to the Loader and back');
850+
port.onmessage = eval;
851+
`;
852+
}
853+
```
854+
831855
### Examples
832856
833857
The various loader hooks can be used together to accomplish wide-ranging

0 commit comments

Comments
 (0)