where are the codes that K6 support import js?

40 views Asked by At

In k6 github repo, we found we can import js modules as below codes:

out, err := vm.RunString(
`
    var m = require("./math.js");
    m.add(1,2);
`
)

But k6 also can do it like this:

out, err := vm.RunString(
`
import {add} from 'k6/x/math';
add(1,2);
`
)

I found the codes where k6 module registering modules as below (Register method just load the module script to goja runtime and put it into a modules array),

modules.Register("k6/x/math", new(math.RootModule))

The question is where are k6 codes to support import a path like "import {add} from 'k6/x/math'"? Thanks.

1

There are 1 answers

0
javaducky On

You'd need to define the exports within your module code. As an example, take a look at the Exports() function in the encoding module.

Hope this helps!