Skip to content

Commit 5746fe9

Browse files
committed
Update after native ESM support
1 parent 9ca09ac commit 5746fe9

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

docs/sources/next/using-k6/javascript-typescript-compatibility-mode.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@ weight: 19
99

1010
# JavaScript and TypeScript compatibility mode
1111

12-
You can write k6 scripts in various ECMAScript versions:
12+
You can write k6 tests in various ECMAScript versions:
1313

1414
- ES6+ JavaScript with ES modules (ESM).
1515
- ES6+ JavaScript with CommonJS modules.
1616

1717
k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud.
1818

19-
To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows:
20-
21-
![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png)
22-
23-
Additionally, k6 also has experimental support for [esbuild](https://esbuild.github.io/), to transpile TypeScript (TS) code and to support most ES6+ features.
19+
Additionally, k6 also has experimental support for [esbuild](https://esbuild.github.io/), to transpile TypeScript (TS) code.
2420

2521
Some users prefer to bundle their test code outside k6. For this reason, k6 offers three JavaScript compatibility modes:
2622

27-
- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features.
28-
- [Experimental enhanced mode](#experimental-enhanced-mode): The experimental option, supporting TS and most ES6+ features.
29-
- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step.
23+
- [Extended mode](#extended-mode): The default option.
24+
- [Experimental enhanced mode](#experimental-enhanced-mode): The experimental option, supporting TS.
25+
- [Base mode](#base-mode): After v0.53.0 this is almost the same as extended, but doesn't alias `global` to `globalThis` - a nodejs compatibility.
3026

3127
When running tests, you can change the mode by using the `--compatibility-mode` option:
3228

@@ -46,9 +42,7 @@ $ k6 run script.js
4642

4743
{{< /code >}}
4844

49-
As illustrated in the previous diagram, if k6 detects unsupported ES+ features while parsing the test script, it then transforms the script with Babel to polyfill the unsupported features.
50-
51-
Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`.
45+
After v0.53.0 the only difference with base is that `global` (node's global variable) is aliased to the value of `globalThis`.
5246

5347
## Experimental enhanced mode
5448

@@ -64,7 +58,7 @@ $ K6_COMPATIBILITY_MODE=experimental_enhanced k6 run script.ts
6458

6559
{{< /code >}}
6660

67-
The experimental enhanced mode is similar to the extended mode, but it uses [esbuild](https://esbuild.github.io/) instead of Babel to transpile TypeScript (TS) code and to support most ES6+ features.
61+
The experimental enhanced mode is similar to the extended mode, but it uses [esbuild](https://esbuild.github.io/) instead of Babel to transpile TypeScript (TS) code.
6862

6963
TypeScript support is partial as it removes the type information but doesn't provide type safety.
7064

@@ -82,9 +76,7 @@ $ K6_COMPATIBILITY_MODE=base k6 run script.js
8276

8377
{{< /code >}}
8478

85-
The base mode omits the Babel transformation step, supporting only ES5.1+ code. You may want to enable this mode if your scripts are already written using only ES5.1 features or were previously transformed by Babel.
86-
87-
Generally, this mode is not recommended as it offers minor benefits in reducing startup time.
79+
After v0.53.0 there isn't a big reason to use this. Before that it was dropping ESM support for usually improved startup speed.
8880

8981
### CommonJS Example
9082

@@ -126,7 +118,6 @@ The examples below demonstrate the use of Babel with bundlers like [Webpack](htt
126118

127119
## Read more
128120

129-
- [Native ESM support](https://github.com/grafana/k6/issues/3265): GitHub issue for native ESM support in k6. This feature aims to eliminate the Babel transformation step within k6.
130121
- [Running large tests](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/running-large-tests): Optimize k6 for better performance.
131122
- [k6 Modules](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/modules): Different options to import modules in k6.
132123
- [k6 Archive Command](https://grafana.com/docs/k6/<K6_VERSION>/misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time.

0 commit comments

Comments
 (0)