From 7f677d21c17c44f4ec5bf4b12c050757a416da50 Mon Sep 17 00:00:00 2001 From: Gleb Tv Date: Wed, 12 Sep 2018 16:04:13 +0300 Subject: [PATCH] multiple manifests --- README.md | 31 ++++++++++++ webpack.go | 136 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 123 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 3bc28b9..6f037e6 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,16 @@ This module allows proper integration with webpack, with support for proper asse This module is compatible with both webpack 4.0, 3.0, 2.0 and 1.0. Example config file is for 3.0. +## Examples + +Are [here](https://github.com/go-webpack/examples) + ## Changelog +#### Version 1.3.0 + +- 2018-09-12 Add ability to use multiple webpack manifests per app + #### Version 1.2 - 2018-07-17 Don't hardcode manifest path (thx @rodumani) @@ -149,6 +157,29 @@ func main() { {{ asset "application.js" }} ``` +#### Using multiple webpack manifests per app + +Alternative usage if you need multiple webpack manifests (if you have separate js apps with separate webpack configs for separate portions of your app for example) + +for qor/render, other would be similar + +``` + LandingRender = render.New(&render.Config{ + ViewPaths: []string{"app/views"}, + DefaultLayout: "landing", + FuncMapMaker: func(*render.Render, *http.Request, http.ResponseWriter) template.FuncMap { + return template.FuncMap{ + "asset": webpack.GetAssetHelper(webpack.BasicConfig( + "localhost:7420", // dev server + "./public/landing", // file path + "landing", // web path + )), + //"json": json.Marshal, + } + }, + }) +``` + #### Usage with other frameworks - Configure webpack to serve manifest.json via ~~StatsPlugin~~ ManifestPlugin diff --git a/webpack.go b/webpack.go index 4376642..2f073ed 100644 --- a/webpack.go +++ b/webpack.go @@ -32,10 +32,28 @@ var isDev = false var initDone = false var preloadedAssets map[string][]string -func readManifest() (map[string][]string, error) { - return reader.Read(Plugin, DevHost, FsPath, WebPath, isDev) +type Config struct { + // DevHost webpack-dev-server host:port + DevHost string + // FsPath filesystem path to public webpack dir + FsPath string + // WebPath http path to public webpack dir + WebPath string + // Plugin webpack plugin to use, can be stats or manifest + Plugin string + // IgnoreMissing ignore assets missing on manifest or fail on them + IgnoreMissing bool + // Verbose - show more info + Verbose bool + // IsDev - true to use webpack-serve or webpack-dev-server, false to use filesystem and manifest.json + IsDev bool + + initDone bool + preloadedAssets map[string][]string } +var AssetHelper func(string) (template.HTML, error) + // Init Set current environment and preload manifest func Init(dev bool) { if Plugin == "deprecated-stats" { @@ -43,64 +61,94 @@ func Init(dev bool) { log.Println("go-webpack: default plugin will be changed to manifest instead of stats-plugin") log.Println("go-webpack: to continue using stats-plugin, please set webpack.Plugin = 'stats' explicitly") } - var err error isDev = dev - if isDev { + + AssetHelper = GetAssetHelper(&Config{ + DevHost: DevHost, + FsPath: FsPath, + WebPath: WebPath, + Plugin: Plugin, + IgnoreMissing: IgnoreMissing, + Verbose: Verbose, + IsDev: dev, + }) +} + +func BasicConfig(host, path, webPath string) *Config { + return &Config{ + DevHost: host, + FsPath: path, + WebPath: webPath, + Plugin: "manifest", + IgnoreMissing: true, + Verbose: true, + IsDev: isDev, + } +} + +// AssetHelper renders asset tag with url from webpack manifest to the page + +func readManifest(conf *Config) (map[string][]string, error) { + return reader.Read(conf.Plugin, conf.DevHost, conf.FsPath, conf.WebPath, conf.IsDev) +} + +func GetAssetHelper(conf *Config) func(string) (template.HTML, error) { + var err error + if conf.IsDev { // Try to preload manifest, so we can show an error if webpack-dev-server is not running - _, err = readManifest() + _, err = readManifest(conf) } else { - preloadedAssets, err = readManifest() + conf.preloadedAssets, err = readManifest(conf) } if err != nil { log.Println(err) } initDone = true -} -// AssetHelper renders asset tag with url from webpack manifest to the page -func AssetHelper(key string) (template.HTML, error) { - var err error - - if !initDone { - return "", errors.New("Please call webpack.Init() first (see readme)") - } + return func(key string) (template.HTML, error) { + var err error - var assets map[string][]string - if isDev { - assets, err = readManifest() - if err != nil { - return template.HTML(""), err + if !initDone { + return "", errors.New("Please call webpack.Init() first (see readme)") } - } else { - assets = preloadedAssets - } - parts := strings.Split(key, ".") - kind := parts[len(parts)-1] - //log.Println("showing assets:", key, parts, kind) - - v, ok := assets[key] - if !ok { - message := "go-webpack: Asset file '" + key + "' not found in manifest" - if Verbose { - log.Printf("%s. Manifest contents:", message) - for k, a := range assets { - log.Printf("%s: %s", k, a) + var assets map[string][]string + if isDev { + assets, err = readManifest(conf) + if err != nil { + return template.HTML(""), err } + } else { + assets = preloadedAssets } - if IgnoreMissing { - return template.HTML(""), nil + + parts := strings.Split(key, ".") + kind := parts[len(parts)-1] + //log.Println("showing assets:", key, parts, kind) + + v, ok := assets[key] + if !ok { + message := "go-webpack: Asset file '" + key + "' not found in manifest" + if Verbose { + log.Printf("%s. Manifest contents:", message) + for k, a := range assets { + log.Printf("%s: %s", k, a) + } + } + if IgnoreMissing { + return template.HTML(""), nil + } + return template.HTML(""), errors.New(message) } - return template.HTML(""), errors.New(message) - } - buf := []string{} - for _, s := range v { - if strings.HasSuffix(s, "."+kind) { - buf = append(buf, helper.AssetTag(kind, s)) - } else { - log.Println("skip asset", s, ": bad type") + buf := []string{} + for _, s := range v { + if strings.HasSuffix(s, "."+kind) { + buf = append(buf, helper.AssetTag(kind, s)) + } else { + log.Println("skip asset", s, ": bad type") + } } + return template.HTML(strings.Join(buf, "\n")), nil } - return template.HTML(strings.Join(buf, "\n")), nil }