2020-07-02 12:16:32 -04:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2020-07-12 06:47:14 -04:00
|
|
|
"strings"
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-17 12:36:09 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2020-07-12 06:47:14 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2020-07-02 12:16:32 -04:00
|
|
|
"github.com/gohugoio/hugo/hugolib/filesystems"
|
2020-07-12 06:47:14 -04:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2020-07-02 12:16:32 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/internal"
|
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
|
|
|
"github.com/gohugoio/hugo/resources"
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
2020-07-12 06:47:14 -04:00
|
|
|
// If not set, the source path will be used as the base target path.
|
|
|
|
// Note that the target path's extension may change if the target MIME type
|
|
|
|
// is different, e.g. when the source is TypeScript.
|
|
|
|
TargetPath string
|
|
|
|
|
|
|
|
// Whether to minify to output.
|
|
|
|
Minify bool
|
|
|
|
|
2020-09-01 10:19:08 -04:00
|
|
|
// Whether to write mapfiles (currently inline only)
|
|
|
|
SourceMap string
|
|
|
|
|
2020-07-12 06:47:14 -04:00
|
|
|
// The language target.
|
|
|
|
// One of: es2015, es2016, es2017, es2018, es2019, es2020 or esnext.
|
|
|
|
// Default is esnext.
|
|
|
|
Target string
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
// The output format.
|
|
|
|
// One of: iife, cjs, esm
|
|
|
|
// Default is to esm.
|
|
|
|
Format string
|
|
|
|
|
2020-07-12 06:47:14 -04:00
|
|
|
// External dependencies, e.g. "react".
|
|
|
|
Externals []string `hash:"set"`
|
|
|
|
|
2020-07-17 12:36:09 -04:00
|
|
|
// User defined symbols.
|
|
|
|
Defines map[string]interface{}
|
|
|
|
|
2020-07-12 06:47:14 -04:00
|
|
|
// What to use instead of React.createElement.
|
|
|
|
JSXFactory string
|
|
|
|
|
|
|
|
// What to use instead of React.Fragment.
|
|
|
|
JSXFragment string
|
2020-07-21 11:59:03 -04:00
|
|
|
|
|
|
|
mediaType media.Type
|
|
|
|
outDir string
|
|
|
|
contents string
|
|
|
|
sourcefile string
|
|
|
|
resolveDir string
|
2020-07-12 06:47:14 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
func decodeOptions(m map[string]interface{}) (Options, error) {
|
|
|
|
var opts Options
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &opts); err != nil {
|
|
|
|
return opts, err
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
2020-07-12 06:47:14 -04:00
|
|
|
|
|
|
|
if opts.TargetPath != "" {
|
|
|
|
opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Target = strings.ToLower(opts.Target)
|
2020-07-21 11:59:03 -04:00
|
|
|
opts.Format = strings.ToLower(opts.Format)
|
2020-07-12 06:47:14 -04:00
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
return opts, nil
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
rs *resources.Spec
|
|
|
|
sfs *filesystems.SourceFilesystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) *Client {
|
|
|
|
return &Client{rs: rs, sfs: fs}
|
|
|
|
}
|
|
|
|
|
|
|
|
type buildTransformation struct {
|
2020-07-21 11:03:06 -04:00
|
|
|
optsm map[string]interface{}
|
|
|
|
rs *resources.Spec
|
|
|
|
sfs *filesystems.SourceFilesystem
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *buildTransformation) Key() internal.ResourceTransformationKey {
|
2020-07-21 11:03:06 -04:00
|
|
|
return internal.NewResourceTransformationKey("jsbuild", t.optsm)
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
|
2020-07-12 06:47:14 -04:00
|
|
|
ctx.OutMediaType = media.JavascriptType
|
|
|
|
|
2020-07-21 11:03:06 -04:00
|
|
|
opts, err := decodeOptions(t.optsm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.TargetPath != "" {
|
|
|
|
ctx.OutPath = opts.TargetPath
|
2020-07-12 06:47:14 -04:00
|
|
|
} else {
|
|
|
|
ctx.ReplaceOutPathExtension(".js")
|
|
|
|
}
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
src, err := ioutil.ReadAll(ctx.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sdir, sfile := path.Split(ctx.SourcePath)
|
|
|
|
opts.sourcefile = sfile
|
|
|
|
opts.resolveDir = t.sfs.RealFilename(sdir)
|
|
|
|
opts.contents = string(src)
|
|
|
|
opts.mediaType = ctx.InMediaType
|
|
|
|
|
|
|
|
buildOptions, err := toBuildOptions(opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := api.Build(buildOptions)
|
|
|
|
if len(result.Errors) > 0 {
|
|
|
|
return fmt.Errorf("%s", result.Errors[0].Text)
|
|
|
|
}
|
|
|
|
ctx.To.Write(result.OutputFiles[0].Contents)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Process(res resources.ResourceTransformer, opts map[string]interface{}) (resource.Resource, error) {
|
|
|
|
return res.Transform(
|
|
|
|
&buildTransformation{rs: c.rs, sfs: c.sfs, optsm: opts},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
|
2020-07-02 12:16:32 -04:00
|
|
|
var target api.Target
|
2020-07-21 11:03:06 -04:00
|
|
|
switch opts.Target {
|
2020-07-21 11:59:03 -04:00
|
|
|
case "", "esnext":
|
2020-07-02 12:16:32 -04:00
|
|
|
target = api.ESNext
|
2020-07-20 16:26:38 -04:00
|
|
|
case "es5":
|
|
|
|
target = api.ES5
|
2020-07-02 12:16:32 -04:00
|
|
|
case "es6", "es2015":
|
|
|
|
target = api.ES2015
|
|
|
|
case "es2016":
|
|
|
|
target = api.ES2016
|
|
|
|
case "es2017":
|
|
|
|
target = api.ES2017
|
|
|
|
case "es2018":
|
|
|
|
target = api.ES2018
|
|
|
|
case "es2019":
|
|
|
|
target = api.ES2019
|
|
|
|
case "es2020":
|
|
|
|
target = api.ES2020
|
|
|
|
default:
|
2020-07-21 11:59:03 -04:00
|
|
|
err = fmt.Errorf("invalid target: %q", opts.Target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaType := opts.mediaType
|
|
|
|
if mediaType.IsZero() {
|
|
|
|
mediaType = media.JavascriptType
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var loader api.Loader
|
2020-07-21 11:59:03 -04:00
|
|
|
switch mediaType.SubType {
|
2020-07-12 06:47:14 -04:00
|
|
|
// TODO(bep) ESBuild support a set of other loaders, but I currently fail
|
|
|
|
// to see the relevance. That may change as we start using this.
|
|
|
|
case media.JavascriptType.SubType:
|
2020-07-02 12:16:32 -04:00
|
|
|
loader = api.LoaderJS
|
2020-07-12 06:47:14 -04:00
|
|
|
case media.TypeScriptType.SubType:
|
2020-07-02 12:16:32 -04:00
|
|
|
loader = api.LoaderTS
|
2020-07-12 06:47:14 -04:00
|
|
|
case media.TSXType.SubType:
|
2020-07-02 12:16:32 -04:00
|
|
|
loader = api.LoaderTSX
|
2020-07-12 06:47:14 -04:00
|
|
|
case media.JSXType.SubType:
|
|
|
|
loader = api.LoaderJSX
|
2020-07-02 12:16:32 -04:00
|
|
|
default:
|
2020-07-21 11:59:03 -04:00
|
|
|
err = fmt.Errorf("unsupported Media Type: %q", opts.mediaType)
|
|
|
|
return
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
var format api.Format
|
|
|
|
// One of: iife, cjs, esm
|
|
|
|
switch opts.Format {
|
|
|
|
case "", "iife":
|
|
|
|
format = api.FormatIIFE
|
|
|
|
case "esm":
|
|
|
|
format = api.FormatESModule
|
|
|
|
case "cjs":
|
|
|
|
format = api.FormatCommonJS
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported script output format: %q", opts.Format)
|
|
|
|
return
|
|
|
|
|
2020-07-02 12:16:32 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
var defines map[string]string
|
|
|
|
if opts.Defines != nil {
|
|
|
|
defines = cast.ToStringMapString(opts.Defines)
|
|
|
|
}
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-09-01 10:19:08 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
buildOptions = api.BuildOptions{
|
2020-07-02 12:16:32 -04:00
|
|
|
Outfile: "",
|
|
|
|
Bundle: true,
|
|
|
|
|
2020-09-01 10:19:08 -04:00
|
|
|
Target: target,
|
|
|
|
Format: format,
|
|
|
|
Sourcemap: sourceMap,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-21 11:03:06 -04:00
|
|
|
MinifyWhitespace: opts.Minify,
|
|
|
|
MinifyIdentifiers: opts.Minify,
|
|
|
|
MinifySyntax: opts.Minify,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
Outdir: opts.outDir,
|
|
|
|
Defines: defines,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-21 11:03:06 -04:00
|
|
|
Externals: opts.Externals,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-21 11:03:06 -04:00
|
|
|
JSXFactory: opts.JSXFactory,
|
|
|
|
JSXFragment: opts.JSXFragment,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
2020-07-21 11:03:06 -04:00
|
|
|
//Tsconfig: opts.TSConfig,
|
2020-07-02 12:16:32 -04:00
|
|
|
|
|
|
|
Stdin: &api.StdinOptions{
|
2020-07-21 11:59:03 -04:00
|
|
|
Contents: opts.contents,
|
|
|
|
Sourcefile: opts.sourcefile,
|
|
|
|
ResolveDir: opts.resolveDir,
|
2020-07-02 12:16:32 -04:00
|
|
|
Loader: loader,
|
|
|
|
},
|
|
|
|
}
|
2020-07-21 11:59:03 -04:00
|
|
|
return
|
2020-07-02 12:16:32 -04:00
|
|
|
|
|
|
|
}
|