k6pack

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: AGPL-3.0 Imports: 8 Imported by: 1

README

Go Reference GitHub Release Go Report Card GitHub Actions codecov GitHub Downloads

k6pack

TypeScript transpiler and module bundler for k6

The main goal of k6pack is to make TypeScript and modern JavaScript features available in k6 tests.

Features

  • Supports TypeScript (.ts) and JavaScript (.js) input
  • Supports remote (https) modules
  • Single executable file, no external dependencies
  • No Node.js installation required

Install

Precompiled binaries can be downloaded and installed from the Releases page.

If you have a go development environment, the installation can also be done with the following command:

go install github.com/grafana/k6pack/cmd/k6pack@latest

Usage

The name of the entry point must be specified as a parameter. The k6 compatible script is sent to the standard output, so it can be executed directly with k6.

k6pack script.ts | k6 run -
script.ts
import { User, newUser } from "./user";

export default () => {
  const user: User = newUser("John");
  console.log(user);
};
user.ts
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 };
Flags
Usage:
  k6pack [flags] filename

Flags:
      --external stringArray   exclude module(s) from the bundle
  -h, --help                   help for k6pack
      --minify                 minify the output
  -o, --output string          write output to file (default stdout)
      --source-root string     sets the sourceRoot field in generated source maps
      --sourcemap              emit the source map with an inline data URL
      --timeout duration       HTTP timeout for remote modules (default 30s)
      --typescript             force TypeScript loader
  -v, --version                version for k6pack

sourcemap

If sourcemap is enabled, by default, the current directory will be set in the sourcemap as the source root directory. This can be changed by using the --source-root flag. You can even disable the source root setting by specifying the empty string.

How It Works

Under the hood, k6pack uses the esbuild library. A special esbuild plugin contains k6 specific configuration and another esbuild plugin implements loading from http/https URL.

k6pack can also be used as a go library.

Development

Tasks

This section contains a description of the tasks performed during development. If you have the xc (Markdown defined task runner) command-line tool, individual tasks can be executed simply by using the xc task-name command.

Click to expand
lint

Run the static analyzer.

golangci-lint run
test

Run the tests.

go test -count 1 -race -coverprofile=build/coverage.txt ./...
coverage

View the test coverage report.

go tool cover -html=build/coverage.txt
build

Build the executable binary.

This is the easiest way to create an executable binary (although the release process uses the goreleaser tool to create release versions).

go build -ldflags="-w -s" -o build/k6pack .
snapshot

Creating an executable binary with a snapshot version.

The goreleaser command-line tool is used during the release process. During development, it is advisable to create binaries with the same tool from time to time.

goreleaser build --snapshot --clean --single-target -o build/k6pack
examples

Run scripts from examples directory.

go run ./cmd/k6pack examples/script.ts | go run go.k6.io/k6@latest run -
go run ./cmd/k6pack examples/simple.ts | go run go.k6.io/k6@latest run -
clean

Delete the build directory.

rm -rf build
all

Run all tasks.

Requires: lint,test,build,snapshot,examples

Documentation

Overview

Package k6pack contains a k6 TypeScript/JavaScript script packager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Imports

func Imports(source string, opts *Options) ([]string, error)

Imports returns k6 imports from input script.

func Pack

func Pack(source string, opts *Options) ([]byte, error)

Pack transforms TypeScript/JavaScript sources into single k6 compatible JavaScript test script.

Types

type Options

type Options struct {
	Directory  string
	Filename   string
	Timeout    time.Duration
	SourceMap  bool
	Minify     bool
	TypeScript bool
	Externals  []string
	SourceRoot string
}

Options used to specify transform/build options.

type PackError

type PackError = interface {
	error
	Format(width int, color bool) string
}

PackError is returned if any errors occur during transforming or bundling.

Directories

Path Synopsis
cmd
k6pack
Package main contains the k6pack CLI entry point.
Package main contains the k6pack CLI entry point.
internal
cli
Package cli contains CLI command for k6pack.
Package cli contains CLI command for k6pack.
plugins/http
Package http contains esbuild http plugin.
Package http contains esbuild http plugin.
plugins/k6
Package k6 contains esbuild k6 plugin.
Package k6 contains esbuild k6 plugin.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL