-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for esm scripts, with import... (#320)
* adding import feature * do import * support for .mjs files * retreival -> retrieval * add test import * reduce file sisze * add docs * path resolution
- Loading branch information
Showing
53 changed files
with
376 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
title: Imports | ||
sidebar: | ||
order: 20 | ||
--- | ||
|
||
import { Steps } from "@astrojs/starlight/components" | ||
import { FileTree } from "@astrojs/starlight/components" | ||
|
||
By default, the scripts cannot import modules (they are evaled) and are expected to execute the code directly. | ||
|
||
You can add support for imports by following these steps. | ||
|
||
## Converting to a module | ||
|
||
<Steps> | ||
|
||
<ol> | ||
|
||
<li> | ||
|
||
Rename the `.genai.js` file | ||
to [module file](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#aside_%E2%80%94_.mjs_versus_.js) `.genai.mjs`. | ||
|
||
<FileTree> | ||
|
||
- genaisrc/ | ||
- **poem.genai.mjs** // .js -> .mjs | ||
|
||
</FileTree> | ||
|
||
</li> | ||
|
||
<li> | ||
|
||
Wrap the script code in a function and make it the default export. You can leave `script` in the main scope. | ||
|
||
```js title="poem.genai.mjs" "export default async function() {" "}" | ||
script(...) | ||
export default async function() { | ||
$`Write a poem.` | ||
} | ||
``` | ||
|
||
</li> | ||
|
||
</ol> | ||
|
||
</Steps> | ||
|
||
## Imports | ||
|
||
Once the file is converted, you can use static or dynamic imports as any other module file. | ||
|
||
```js | ||
import { parse } from "ini" | ||
... | ||
export default async function () { | ||
// static import | ||
const res = parse("x = 1\ny = 2") | ||
console.log(res) | ||
|
||
// dynamic import | ||
const { stringify } = await import("ini") | ||
console.log(stringify(res)) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.