mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-15 20:44:01 +02:00
pipes: Add external source map support to js.Build and Babel
Fixes #8132
This commit is contained in:
@@ -18,6 +18,8 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
@@ -92,6 +94,14 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
return err
|
||||
}
|
||||
|
||||
if buildOptions.Sourcemap == api.SourceMapExternal && buildOptions.Outdir == "" {
|
||||
buildOptions.Outdir, err = ioutil.TempDir(os.TempDir(), "compileOutput")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(buildOptions.Outdir)
|
||||
}
|
||||
|
||||
result := api.Build(buildOptions)
|
||||
|
||||
if len(result.Errors) > 0 {
|
||||
@@ -145,7 +155,25 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
return errors[0]
|
||||
}
|
||||
|
||||
ctx.To.Write(result.OutputFiles[0].Contents)
|
||||
if buildOptions.Sourcemap == api.SourceMapExternal {
|
||||
content := string(result.OutputFiles[1].Contents)
|
||||
symPath := path.Base(ctx.OutPath) + ".map"
|
||||
re := regexp.MustCompile(`//# sourceMappingURL=.*\n?`)
|
||||
content = re.ReplaceAllString(content, "//# sourceMappingURL="+symPath+"\n")
|
||||
|
||||
if err = ctx.PublishSourceMap(string(result.OutputFiles[0].Contents)); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := ctx.To.Write([]byte(content))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err := ctx.To.Write(result.OutputFiles[0].Contents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -338,6 +338,8 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
|
||||
switch opts.SourceMap {
|
||||
case "inline":
|
||||
sourceMap = api.SourceMapInline
|
||||
case "external":
|
||||
sourceMap = api.SourceMapExternal
|
||||
case "":
|
||||
sourceMap = api.SourceMapNone
|
||||
default:
|
||||
|
@@ -109,4 +109,22 @@ func TestToBuildOptions(t *testing.T) {
|
||||
Loader: api.LoaderJS,
|
||||
},
|
||||
})
|
||||
|
||||
opts, err = toBuildOptions(Options{
|
||||
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.JavascriptType,
|
||||
SourceMap: "external",
|
||||
})
|
||||
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.SourceMapExternal,
|
||||
Stdin: &api.StdinOptions{
|
||||
Loader: api.LoaderJS,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user