Skip to content

Commit

Permalink
Add import.meta.resolve documentation (#1691)
Browse files Browse the repository at this point in the history

Co-authored-by: Joan López de la Franca Beltran <[email protected]>
Co-authored-by: Heitor Tashiro Sergent <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent dde3ddc commit 706d47c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/sources/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module.exports = {
root: true,
env: {
es2017: true,
es2022: true,
commonjs: true,
},
parserOptions: {
ecmaVersion: '2017',
ecmaVersion: '2024',
sourceType: 'module',
requireConfigFile: false, // <== ADD THIS
},
extends: ['plugin:mdx/recommended', 'plugin:prettier/recommended'],
plugins: ['markdown'],
Expand Down
4 changes: 4 additions & 0 deletions docs/sources/next/javascript-api/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ The list of k6 modules natively supported in your k6 scripts.

{{< docs/shared source="k6" lookup="javascript-api/init-context.md" version="<K6_VERSION>" >}}

## import.meta

{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="<K6_VERSION>" >}}

## k6

{{< docs/shared source="k6" lookup="javascript-api/k6.md" version="<K6_VERSION>" >}}
Expand Down
9 changes: 9 additions & 0 deletions docs/sources/next/javascript-api/import.meta/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: 'import.meta'
description: 'Details about the ECMAScript import meta property.'
weight: 20
---

## import.meta

{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="<K6_VERSION>" >}}
32 changes: 32 additions & 0 deletions docs/sources/next/javascript-api/import.meta/resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: 'resolve( path )'
description: 'Resolve the path to a URL string in the same way an import statement does.'
---

# resolve( path )

Resolve a path to a URL string in the same way an import statement does.'

This is useful if you want to provide a path relative to the current module to a function that uses `open` or another function that takes URLs.

This is particularly useful as some functions within k6 are not relative to the same origin, which might lead to seemingly similar paths having different files being loaded from the filesystem.

For more details, refer to this [issue](https://github.com/grafana/k6/issues/3857).

| Parameter | Type | Description |
| --------- | ------ | -------------------------- |
| path | string | A file path. |

### Example

{{< code >}}

```javascript
const pdfFile = open(import.meta.resolve('./path/to/file.pdf'), 'b');

export default function () {
// this is here to avoid an exception if run using k6
}
```
{{< /code >}}
9 changes: 9 additions & 0 deletions docs/sources/next/shared/javascript-api/import.meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: 'javascript-api/import.meta'
---

`import.meta` is only available in ECMAScript modules, but not CommonJS ones.

| Function | Description |
| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------- |
| [import.meta.resolve](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/import.meta/resolve) | resolve path to URL the same way that an ESM import would |

0 comments on commit 706d47c

Please sign in to comment.