js.Build: Add SourceMap flag with inline option
authorAndreas Richter <richtera@users.noreply.github.com>
Tue, 1 Sep 2020 14:19:08 +0000 (10:19 -0400)
committerGitHub <noreply@github.com>
Tue, 1 Sep 2020 14:19:08 +0000 (16:19 +0200)
Added a flag to allow turning on sourcemap in ESBuild. The current support
can only support inline or true as value for sourcemap. This is because
the way ESBuild is invoked it doesn't have a separate output path
to write the mapfile external to the asset pipeline. Add disable for "" and "0".
Add test script and make sure mage check passes.

Fixes #7607

go.mod
go.sum
resources/resource_transformers/js/build.go
resources/resource_transformers/js/build_test.go

diff --git a/go.mod b/go.mod
index 161333daeca93e628167dc0f6517b35e69f2834e..09aa7ee5897b36d66324366b0b5091c24aad0dcf 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -28,7 +28,7 @@ require (
        github.com/jdkato/prose v1.1.1
        github.com/kr/pretty v0.2.0 // indirect
        github.com/kyokomi/emoji v2.2.1+incompatible
-       github.com/magefile/mage v1.9.0
+       github.com/magefile/mage v1.10.0
        github.com/markbates/inflect v1.0.0
        github.com/mattn/go-isatty v0.0.12
        github.com/miekg/mmark v1.3.6
diff --git a/go.sum b/go.sum
index e47c224795ffffff44b20cf7a5d03bd79d829dd6..b365182a9c3341a36e7ae23b741ded7084558303 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -125,6 +125,7 @@ github.com/evanw/esbuild v0.6.2 h1:pp33TIPgiHCtKL/gMW/V/PFHWNx/5cDTqbJHqAiy0jg=
 github.com/evanw/esbuild v0.6.2/go.mod h1:mptxmSXIzBIKKCe4jo9A5SToEd1G+AKZ9JmY85dYRJ0=
 github.com/evanw/esbuild v0.6.5 h1:jVUDkQKOX9srwt/mUlvIba0/jmH46+B5wfwh0pWW7BM=
 github.com/evanw/esbuild v0.6.5/go.mod h1:mptxmSXIzBIKKCe4jo9A5SToEd1G+AKZ9JmY85dYRJ0=
+github.com/evanw/esbuild v0.6.28 h1:sTJdvTB8WCSp9N5a+T18Fw3a5So76qHQJHwYNhy0Z/c=
 github.com/fortytw2/leaktest v1.2.0 h1:cj6GCiwJDH7l3tMHLjZDo0QqPtrXJiWSI9JgpeQKw+Q=
 github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
 github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
@@ -240,6 +241,8 @@ github.com/kyokomi/emoji v2.2.1+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2px
 github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
 github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE=
 github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
+github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
+github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
 github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
 github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
index 8e0c7c13094ce2a322cd329272effd9dde0ff287..e4b4b1c2085ba0bcdb54abe825b03b209ec8bb6d 100644 (file)
@@ -42,6 +42,9 @@ type Options struct {
        // Whether to minify to output.
        Minify bool
 
+       // Whether to write mapfiles (currently inline only)
+       SourceMap string
+
        // The language target.
        // One of: es2015, es2016, es2017, es2018, es2019, es2020 or esnext.
        // Default is esnext.
@@ -217,12 +220,24 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
                defines = cast.ToStringMapString(opts.Defines)
        }
 
+       var sourceMap api.SourceMap
+       switch opts.SourceMap {
+       case "inline":
+               sourceMap = api.SourceMapInline
+       case "":
+               sourceMap = api.SourceMapNone
+       default:
+               err = fmt.Errorf("unsupported sourcemap type: %q", opts.SourceMap)
+               return
+       }
+
        buildOptions = api.BuildOptions{
                Outfile: "",
                Bundle:  true,
 
-               Target: target,
-               Format: format,
+               Target:    target,
+               Format:    format,
+               Sourcemap: sourceMap,
 
                MinifyWhitespace:  opts.Minify,
                MinifyIdentifiers: opts.Minify,
index ee97dede502d9487b13c4eeb0fa93d3745da3941..c04c0ed12b79c167f17994f7d1b16cf1f18506d5 100644 (file)
@@ -63,4 +63,18 @@ func TestToBuildOptions(t *testing.T) {
                Stdin:             &api.StdinOptions{},
        })
 
+       opts, err = toBuildOptions(Options{
+               Target: "es2018", Format: "cjs", Minify: true, mediaType: media.JavascriptType,
+               SourceMap: "inline"})
+       c.Assert(err, qt.IsNil)
+       c.Assert(opts, qt.DeepEquals, api.BuildOptions{
+               Bundle:            true,
+               Target:            api.ES2018,
+               Format:            api.FormatCommonJS,
+               MinifyIdentifiers: true,
+               MinifySyntax:      true,
+               MinifyWhitespace:  true,
+               Sourcemap:         api.SourceMapInline,
+               Stdin:             &api.StdinOptions{},
+       })
 }