Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import.meta.resolve documentation #1691

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 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,14 @@ 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
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

`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 |
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

## k6

{{< docs/shared source="k6" lookup="javascript-api/k6.md" version="<K6_VERSION>" >}}
Expand Down
11 changes: 11 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,11 @@
---
title: 'import.meta'
description: 'the ECMAScript import meta property'
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
weight: 20
---

# import.meta

| 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 |
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 a path to a URL strign the same way an import would.'
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
---

# resolve(path)
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

Resolve a path to a URL string the same way an import would.
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

This is useful if you want to provide a path relative to the current module to a function that will use `open` or some other function taking URLs.
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

This is particularly useful as some functions within k6 are not relative to the same origin, which might lead to seemingly similar paths having very different file being loaded from the filesystem.
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

For ongoing discussions please see this [issue](https://github.com/grafana/k6/issues/3857).
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

| Parameter | Type | Description |
| --------- | ------ | -------------------------- |
| path | string | a path to a potential file |
mstoykov marked this conversation as resolved.
Show resolved Hide resolved

### Example

{{< code >}}

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

export default function () {
// this is here to not have an exception if ran in k6
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
}
```

{{< /code >}}
Loading