"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "resources/resource_transformers/babel/babel.go" between
hugo-0.80.0.tar.gz and hugo-0.81.0.tar.gz

About: Hugo is a static site generator that takes a source directory of Markdown files and templates and uses these as input to create a complete website (written in Go).

babel.go  (hugo-0.80.0):babel.go  (hugo-0.81.0)
skipping to change at line 19 skipping to change at line 19
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package babel package babel
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"os"
"path"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"github.com/cli/safeexec" "github.com/cli/safeexec"
"github.com/gohugoio/hugo/common/hexec" "github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/resources/internal" "github.com/gohugoio/hugo/resources/internal"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
skipping to change at line 46 skipping to change at line 50
// Options from https://babeljs.io/docs/en/options // Options from https://babeljs.io/docs/en/options
type Options struct { type Options struct {
Config string // Custom path to config file Config string // Custom path to config file
Minified bool Minified bool
NoComments bool NoComments bool
Compact *bool Compact *bool
Verbose bool Verbose bool
NoBabelrc bool NoBabelrc bool
SourceMap string
} }
// DecodeOptions decodes options to and generates command flags
func DecodeOptions(m map[string]interface{}) (opts Options, err error) { func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
if m == nil { if m == nil {
return return
} }
err = mapstructure.WeakDecode(m, &opts) err = mapstructure.WeakDecode(m, &opts)
return return
} }
func (opts Options) toArgs() []string { func (opts Options) toArgs() []string {
var args []string var args []string
// external is not a known constant on the babel command line
// .sourceMaps must be a boolean, "inline", "both", or undefined
switch opts.SourceMap {
case "external":
args = append(args, "--source-maps")
case "inline":
args = append(args, "--source-maps=inline")
}
if opts.Minified { if opts.Minified {
args = append(args, "--minified") args = append(args, "--minified")
} }
if opts.NoComments { if opts.NoComments {
args = append(args, "--no-comments") args = append(args, "--no-comments")
} }
if opts.Compact != nil { if opts.Compact != nil {
args = append(args, "--compact="+strconv.FormatBool(*opts.Compact )) args = append(args, "--compact="+strconv.FormatBool(*opts.Compact ))
} }
if opts.Verbose { if opts.Verbose {
skipping to change at line 144 skipping to change at line 158
// We need an absolute filename to the config file. // We need an absolute filename to the config file.
if !filepath.IsAbs(configFile) { if !filepath.IsAbs(configFile) {
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile) configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if configFile == "" && t.options.Config != "" { if configFile == "" && t.options.Config != "" {
// Only fail if the user specified config file is not fou nd. // Only fail if the user specified config file is not fou nd.
return errors.Errorf("babel config %q not found:", config File) return errors.Errorf("babel config %q not found:", config File)
} }
} }
ctx.ReplaceOutPathExtension(".js")
var cmdArgs []string var cmdArgs []string
if configFile != "" { if configFile != "" {
logger.Infoln("babel: use config file", configFile) logger.Infoln("babel: use config file", configFile)
cmdArgs = []string{"--config-file", configFile} cmdArgs = []string{"--config-file", configFile}
} }
if optArgs := t.options.toArgs(); len(optArgs) > 0 { if optArgs := t.options.toArgs(); len(optArgs) > 0 {
cmdArgs = append(cmdArgs, optArgs...) cmdArgs = append(cmdArgs, optArgs...)
} }
cmdArgs = append(cmdArgs, "--filename="+ctx.SourcePath) cmdArgs = append(cmdArgs, "--filename="+ctx.SourcePath)
// Create compile into a real temp file:
// 1. separate stdout/stderr messages from babel (https://github.com/gohu
goio/hugo/issues/8136)
// 2. allow generation and retrieval of external source map.
compileOutput, err := ioutil.TempFile("", "compileOut-*.js")
if err != nil {
return err
}
cmdArgs = append(cmdArgs, "--out-file="+compileOutput.Name())
defer os.Remove(compileOutput.Name())
cmd, err := hexec.SafeCommand(binary, cmdArgs...) cmd, err := hexec.SafeCommand(binary, cmdArgs...)
if err != nil { if err != nil {
return err return err
} }
cmd.Stdout = ctx.To
cmd.Stderr = io.MultiWriter(infoW, &errBuf) cmd.Stderr = io.MultiWriter(infoW, &errBuf)
cmd.Stdout = cmd.Stderr
cmd.Env = hugo.GetExecEnviron(t.rs.WorkingDir, t.rs.Cfg, t.rs.BaseFs.Asse ts.Fs) cmd.Env = hugo.GetExecEnviron(t.rs.WorkingDir, t.rs.Cfg, t.rs.BaseFs.Asse ts.Fs)
stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
return err return err
} }
go func() { go func() {
defer stdin.Close() defer stdin.Close()
io.Copy(stdin, ctx.From) io.Copy(stdin, ctx.From)
}() }()
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return errors.Wrap(err, errBuf.String()) return errors.Wrap(err, errBuf.String())
} }
content, err := ioutil.ReadAll(compileOutput)
if err != nil {
return err
}
mapFile := compileOutput.Name() + ".map"
if _, err := os.Stat(mapFile); err == nil {
defer os.Remove(mapFile)
sourceMap, err := ioutil.ReadFile(mapFile)
if err != nil {
return err
}
if err = ctx.PublishSourceMap(string(sourceMap)); err != nil {
return err
}
targetPath := path.Base(ctx.OutPath) + ".map"
re := regexp.MustCompile(`//# sourceMappingURL=.*\n?`)
content = []byte(re.ReplaceAllString(string(content), "//# source
MappingURL="+targetPath+"\n"))
}
ctx.To.Write(content)
return nil return nil
} }
// Process transforms the given Resource with the Babel processor. // Process transforms the given Resource with the Babel processor.
func (c *Client) Process(res resources.ResourceTransformer, options Options) (re source.Resource, error) { func (c *Client) Process(res resources.ResourceTransformer, options Options) (re source.Resource, error) {
return res.Transform( return res.Transform(
&babelTransformation{rs: c.rs, options: options}, &babelTransformation{rs: c.rs, options: options},
) )
} }
 End of changes. 10 change blocks. 
1 lines changed or deleted 52 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)