Skip to content

Commit 706d47c

Browse files
mstoykovjoanlopezheitortsergent
authored
Add import.meta.resolve documentation (#1691)
Co-authored-by: Joan López de la Franca Beltran <[email protected]> Co-authored-by: Heitor Tashiro Sergent <[email protected]>
1 parent dde3ddc commit 706d47c

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

docs/sources/.eslintrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module.exports = {
22
root: true,
33
env: {
4-
es2017: true,
4+
es2022: true,
55
commonjs: true,
66
},
77
parserOptions: {
8-
ecmaVersion: '2017',
8+
ecmaVersion: '2024',
99
sourceType: 'module',
10+
requireConfigFile: false, // <== ADD THIS
1011
},
1112
extends: ['plugin:mdx/recommended', 'plugin:prettier/recommended'],
1213
plugins: ['markdown'],

docs/sources/next/javascript-api/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ The list of k6 modules natively supported in your k6 scripts.
1111

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

14+
## import.meta
15+
16+
{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="<K6_VERSION>" >}}
17+
1418
## k6
1519

1620
{{< docs/shared source="k6" lookup="javascript-api/k6.md" version="<K6_VERSION>" >}}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 'import.meta'
3+
description: 'Details about the ECMAScript import meta property.'
4+
weight: 20
5+
---
6+
7+
## import.meta
8+
9+
{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="<K6_VERSION>" >}}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: 'resolve( path )'
3+
description: 'Resolve the path to a URL string in the same way an import statement does.'
4+
---
5+
6+
# resolve( path )
7+
8+
Resolve a path to a URL string in the same way an import statement does.'
9+
10+
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.
11+
12+
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.
13+
14+
For more details, refer to this [issue](https://github.com/grafana/k6/issues/3857).
15+
16+
| Parameter | Type | Description |
17+
| --------- | ------ | -------------------------- |
18+
| path | string | A file path. |
19+
20+
### Example
21+
22+
{{< code >}}
23+
24+
```javascript
25+
const pdfFile = open(import.meta.resolve('./path/to/file.pdf'), 'b');
26+
27+
export default function () {
28+
// this is here to avoid an exception if run using k6
29+
}
30+
```
31+
32+
{{< /code >}}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 'javascript-api/import.meta'
3+
---
4+
5+
`import.meta` is only available in ECMAScript modules, but not CommonJS ones.
6+
7+
| Function | Description |
8+
| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------- |
9+
| [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 commit comments

Comments
 (0)