You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sources/next/using-k6/javascript-typescript-compatibility-mode.md
+8-17Lines changed: 8 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,24 +9,20 @@ weight: 19
9
9
10
10
# JavaScript and TypeScript compatibility mode
11
11
12
-
You can write k6 scripts in various ECMAScript versions:
12
+
You can write k6 tests in various ECMAScript versions:
13
13
14
14
- ES6+ JavaScript with ES modules (ESM).
15
15
- ES6+ JavaScript with CommonJS modules.
16
16
17
17
k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud.
18
18
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
-

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.
24
20
25
21
Some users prefer to bundle their test code outside k6. For this reason, k6 offers three JavaScript compatibility modes:
26
22
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.
30
26
31
27
When running tests, you can change the mode by using the `--compatibility-mode` option:
32
28
@@ -46,9 +42,7 @@ $ k6 run script.js
46
42
47
43
{{< /code >}}
48
44
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`.
52
46
53
47
## Experimental enhanced mode
54
48
@@ -64,7 +58,7 @@ $ K6_COMPATIBILITY_MODE=experimental_enhanced k6 run script.ts
64
58
65
59
{{< /code >}}
66
60
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.
68
62
69
63
TypeScript support is partial as it removes the type information but doesn't provide type safety.
70
64
@@ -82,9 +76,7 @@ $ K6_COMPATIBILITY_MODE=base k6 run script.js
82
76
83
77
{{< /code >}}
84
78
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.
88
80
89
81
### CommonJS Example
90
82
@@ -126,7 +118,6 @@ The examples below demonstrate the use of Babel with bundlers like [Webpack](htt
126
118
127
119
## Read more
128
120
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.
130
121
-[Running large tests](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/running-large-tests): Optimize k6 for better performance.
131
122
-[k6 Modules](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/modules): Different options to import modules in k6.
132
123
-[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