Skip to content

Commit

Permalink
multiple manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
glebtv committed Sep 12, 2018
1 parent 9c2aa2e commit 7f677d2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 44 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
136 changes: 92 additions & 44 deletions webpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,75 +32,123 @@ 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" {
Plugin = "stats"
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
}

0 comments on commit 7f677d2

Please sign in to comment.