Skip to content

Commit bfc95eb

Browse files
authored
chore: update docs to 1.5.0 (#93)
1 parent d1d2362 commit bfc95eb

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

docs/api.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,49 @@ const zip = new Blob([data]);
341341

342342
Returns a [`FileSystemTree`](#filesystemtree) when the format is `json`, otherwise a `Uint8Array`.
343343

344+
### `setPreviewScript`
345+
346+
Added in version `1.5.0`.
347+
348+
Configure a script to be injected inside all previews. After this function resolves,
349+
every preview iframe that is either added or reloaded will now include this extra
350+
script on all HTML responses.
351+
352+
Notably, existing previews won't include the script until they have been reloaded.
353+
354+
To reload a preview you can use [`reloadPreview`](#reloadpreview)
355+
356+
:::warning
357+
This API is an advanced feature that should only be used if it is your only option.
358+
Since you can control servers running in WebContainer, it's preferable to add this code when serving the content itself.
359+
360+
<br />
361+
362+
In particular, this might break existing or future WebContainer features added in the future.
363+
:::
364+
365+
<h4 id="wc-setpreviewscript-signature">
366+
<a id="wc-setpreviewscript-signature">Signature</a>
367+
<a href="#wc-setpreviewscript-signature" class="header-anchor" aria-hidden="true">#</a>
368+
</h4>
369+
370+
<code>setPreviewScript(scriptSrc: string, options?: <a href="#previewscriptoptions">PreviewScriptOptions</a>): Promise&lt;void&gt;</code>
371+
372+
<h4 id="wc-setpreviewscript-example">
373+
<a id="wc-setpreviewscript-example">Example</a>
374+
<a href="#wc-setpreviewscript-example" class="header-anchor" aria-hidden="true">#</a>
375+
</h4>
376+
377+
```js
378+
const script = `
379+
console.log('Hello world!');
380+
`;
381+
382+
await webcontainerInstance.setPreviewScript(script);
383+
384+
// now all previews will always print hello world to the console.
385+
```
386+
344387
### `teardown`
345388

346389
Destroys the WebContainer instance, turning it unusable, and releases its resources. After this, a new WebContainer instance can be obtained by calling [`boot`](#▸-boot).
@@ -1103,6 +1146,40 @@ Globbing patterns to exclude files from the export.
11031146

11041147
---
11051148

1149+
## `PreviewScriptOptions`
1150+
1151+
Options that control attributes on a script injected into previews.
1152+
1153+
```ts
1154+
export interface PreviewScriptOptions {
1155+
type?: 'module' | 'importmap';
1156+
defer?: boolean;
1157+
async?: boolean;
1158+
}
1159+
```
1160+
1161+
### `PreviewScriptOptions` Properties
1162+
1163+
<br />
1164+
1165+
#### `type?: 'module' | 'importmap'`
1166+
1167+
The type attribute to use for the script. For more information, check the [MDN page on the script: type attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type).
1168+
1169+
<br />
1170+
1171+
#### `defer?: boolean`
1172+
1173+
If set to true, then the `defer` attribute will be set on the script tag. For more information, check the [MDN page on the script: defer attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#async).
1174+
1175+
<br />
1176+
1177+
#### `async?: boolean`
1178+
1179+
If set to true, then the `async` attribute will be set on the script tag. For more information, check the [MDN page on the script: async attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#async).
1180+
1181+
---
1182+
11061183
## `WebContainerProcess`
11071184

11081185
A running process spawned in a [WebContainer](#webcontainer) instance.

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ head:
1010

1111
# Changelog
1212

13+
## 1.5.0
14+
15+
* Add support for injecting a script in previews with [`setPreviewScript`](api#▸-setPreviewScript).
16+
1317
## 1.4.0
1418

1519
* Add support for exporting the file system with [`export`](api#▸-export).

0 commit comments

Comments
 (0)