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

feat: Add native TypeScript support using k6pack library #3710

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func runtimeOptionFlagSet(includeSysEnv bool) *pflag.FlagSet {
flags.SortFlags = false
flags.Bool("include-system-env-vars", includeSysEnv, "pass the real system environment variables to the runtime")
flags.String("compatibility-mode", "extended",
`JavaScript compiler compatibility mode, "extended" or "base"
`JavaScript compiler compatibility mode, "enhanced" or "extended" or "base"
base: pure goja - Golang JS VM supporting ES5.1+
extended: base + Babel with parts of ES2015 preset
slower to compile in case the script uses syntax unsupported by base
enhanced: esbuild-based bundling for TypeSript and ES6+ support
`)
flags.StringP("type", "t", "", "override test type, \"js\" or \"archive\"")
flags.StringArrayP("env", "e", nil, "add/override environment variable with `VAR=value`")
Expand Down
17 changes: 17 additions & 0 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"syscall"

"github.com/grafana/k6pack"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -22,6 +23,7 @@ import (
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/loader"
"go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
)

const (
Expand Down Expand Up @@ -66,6 +68,21 @@ func loadLocalTest(gs *state.GlobalState, cmd *cobra.Command, args []string) (*l
return nil, err
}

if runtimeOptions.CompatibilityMode.String == lib.CompatibilityModeEnhanced.String() {
script, err := k6pack.Pack(string(src.Data), &k6pack.Options{
Filename: src.URL.Path,
SourceMap: true,
SourceRoot: pwd,
})
if err != nil {
return nil, err
}

src.Data = script

runtimeOptions.CompatibilityMode = null.StringFrom(lib.CompatibilityModeBase.String())
}

registry := metrics.NewRegistry()
state := &lib.TestPreInitState{
Logger: gs.Logger,
Expand Down
6 changes: 6 additions & 0 deletions examples/enhanced/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { User, newUser } from "./user";

export default () => {
const user: User = newUser("John");
console.log(user);
};
20 changes: 20 additions & 0 deletions examples/enhanced/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface User {
name: string;
id: number;
}

class UserAccount implements User {
name: string;
id: number;

constructor(name: string) {
this.name = name;
this.id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
}
}

function newUser(name: string): User {
return new UserAccount(name);
}

export { User, newUser };
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/go-sourcemap/sourcemap v2.1.4+incompatible
github.com/golang/protobuf v1.5.4
github.com/gorilla/websocket v1.5.1
github.com/grafana/k6pack v0.1.5
github.com/grafana/xk6-browser v1.4.3
github.com/grafana/xk6-dashboard v0.7.2
github.com/grafana/xk6-output-prometheus-remote v0.3.1
Expand Down Expand Up @@ -70,6 +71,7 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/evanw/esbuild v0.20.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanw/esbuild v0.20.2 h1:E4Y0iJsothpUCq7y0D+ERfqpJmPWrZpNybJA3x3I4p8=
github.com/evanw/esbuild v0.20.2/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
Expand Down Expand Up @@ -96,6 +98,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/grafana/k6pack v0.1.5 h1:XrOM7GQX30ISzashmegcowxKVhpFULm9fQIZe0+vJXA=
github.com/grafana/k6pack v0.1.5/go.mod h1:cltO5vmQOObJDtKbRkNVGymk3J+MRhriJyQg1JJzeEc=
github.com/grafana/xk6-browser v1.4.3 h1:QwOi2dpcgUyY6tXz0khBaLPh38dn44fWkfE9cUCEeDg=
github.com/grafana/xk6-browser v1.4.3/go.mod h1:pj4OWy+VL3vM5qmsuAxwGleySq/6fhRsocfmHGQV7YQ=
github.com/grafana/xk6-dashboard v0.7.2 h1:CLaWeRfPZ388IS6rBn0nI+lqtX50QoQ73z0Hz5BIrS4=
Expand Down
44 changes: 38 additions & 6 deletions lib/compatibility_mode_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
CompatibilityModeExtended CompatibilityMode = iota + 1
// CompatibilityModeBase is standard goja ES5.1+
CompatibilityModeBase
// CompatibilityModeEnhanced achives ES6+ and TypeScript compatibility with k6pack
CompatibilityModeEnhanced
)

// RuntimeOptions are settings passed onto the goja JS runtime
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/evanw/esbuild/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading