js: Add Shims option

This commit adds a new `shims` option to `js.Build` that allows swapping out a component with another.

Fixes #8165
This commit is contained in:
Bjørn Erik Pedersen
2021-01-21 16:52:32 +01:00
parent a1fe552fc9
commit e19a046c4b
3 changed files with 46 additions and 35 deletions

View File

@@ -62,11 +62,14 @@ type Options struct {
Format string
// External dependencies, e.g. "react".
Externals []string `hash:"set"`
Externals []string
// User defined symbols.
Defines map[string]interface{}
// Maps a component import to another.
Shims map[string]string
// User defined params. Will be marshaled to JSON and available as "@params", e.g.
// import * as params from '@params';
Params interface{}
@@ -138,6 +141,13 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
fs := c.rs.Assets
resolveImport := func(args api.OnResolveArgs) (api.OnResolveResult, error) {
impPath := args.Path
if opts.Shims != nil {
override, found := opts.Shims[impPath]
if found {
impPath = override
}
}
isStdin := args.Importer == stdinImporter
var relDir string
if !isStdin {
@@ -153,8 +163,6 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
relDir = filepath.Dir(opts.sourcefile)
}
impPath := args.Path
// Imports not starting with a "." is assumed to live relative to /assets.
// Hugo makes no assumptions about the directory structure below /assets.
if relDir != "" && strings.HasPrefix(impPath, ".") {