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

Disable sourcemaps support in babel if input is big #2345

Merged
merged 7 commits into from
Jan 19, 2022
Merged
Changes from 2 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
32 changes: 31 additions & 1 deletion js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
_ "embed" // we need this for embedding Babel
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"sync"
"time"

Expand All @@ -34,6 +37,25 @@ import (
"go.k6.io/k6/lib"
)

//nolint:gochecknoglobals
var maxSrcLenForBabelSourceMap = 250 * 1024

const maxSrcLenForBabelSourceMapVarName = "K6_DEBUG_SOURCEMAP_FILESIZE_LIMIT"

// drop this code and everything it's connected to when goja is dropped
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
func init() {
v := os.Getenv(maxSrcLenForBabelSourceMapVarName)
if len(v) > 0 {
i, err := strconv.Atoi(v)
if err != nil {
//nolint:forbidigo
fmt.Printf("Tried to parse %q from %s as integer but couldn't %s\n",
na-- marked this conversation as resolved.
Show resolved Hide resolved
v, maxSrcLenForBabelSourceMapVarName, err)
}
maxSrcLenForBabelSourceMap = i
}
}

//go:embed lib/babel.min.js
var babelSrc string //nolint:gochecknoglobals

Expand Down Expand Up @@ -124,7 +146,15 @@ func (c *Compiler) Transform(src, filename string, inputSrcMap []byte) (code str
return
}

code, srcMap, err = c.babel.transformImpl(c.logger, src, filename, c.Options.SourceMapLoader != nil, inputSrcMap)
sourceMapEnabled := c.Options.SourceMapLoader != nil
if sourceMapEnabled && len(src) > maxSrcLenForBabelSourceMap {
sourceMapEnabled = false
c.logger.Warnf("the source for `%s` needs to go through babel but is over %d bytes. "+
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
"For performance reasons sourcemaps support will be disabled for this particular file.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention K6_DEBUG_SOURCEMAP_FILESIZE_LIMIT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't mention this, for a couple of reasons:

  • K6_DEBUG_SOURCEMAP_FILESIZE_LIMIT=whatever k6 cloud won't work
  • we intend to get rid of this option eventually

So we shouldn't really document this. It's a temporary option until we get rid of Babel. We can always mention it and the caveats if someone asks in the forum.

mstoykov marked this conversation as resolved.
Show resolved Hide resolved
filename, maxSrcLenForBabelSourceMap)
}

code, srcMap, err = c.babel.transformImpl(c.logger, src, filename, sourceMapEnabled, inputSrcMap)
return
}

Expand Down