Skip to content

Commit

Permalink
example doc wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Sep 16, 2021
1 parent b590f28 commit 257ac6f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,30 @@ const require = createRequire(cwd() + '/<preload>');
}
```
In order to allow communication between the application and the loader another
argument is provided to the preload code `port`. This is available as an
parameter to the loader hook and inside of the source text returned by the hook.
Some care must be taken in order to properly `ref()` and `unref()` the
`MessagePort` to prevent a process from being in a state where it won't close
normally.
```js
/**
* This example causes
* @param {object} utilities
* @param {MessagePort} utilities.port
*/
export function getGlobalPreloadCode({port}) {
port.onmessage = (evt) => {
// ...
};
return `\
port.postMessage('I went to the Loader and back');
port.onmessage = eval;
`;
}
```
### Examples
The various loader hooks can be used together to accomplish wide-ranging
Expand Down

0 comments on commit 257ac6f

Please sign in to comment.