-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.go
167 lines (144 loc) · 4.72 KB
/
webpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package webpack
import (
"errors"
"html/template"
"log"
"strings"
"github.com/go-webpack/webpack/helper"
"github.com/go-webpack/webpack/reader"
)
// AssetList represents a mapping from asset name to asset filenames (maybe multiple assets, with a hash etc)
type AssetList map[string][]string
// DevHost webpack-dev-server host:port
var DevHost = "localhost:3808"
// FsPath filesystem path to public webpack dir
var FsPath = "./public/webpack"
// WebPath http path to public webpack dir
var WebPath = "webpack"
// Plugin webpack plugin to use, can be stats or manifest
var Plugin = "manifest"
// IgnoreMissing ignore assets missing on manifest or fail on them
var IgnoreMissing = true
// Verbose error messages to console (even if error is ignored)
var Verbose = true
// Config is an instance of go-webpack configuration to use with multiple manifest files (multiple webpack configs)
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
}
// AssetHelper renders asset tag with url from webpack manifest to the page. This is a default assethelper, exported at package level. You can also get your own AssetHelper with webpack.GetAssetHelper
var AssetHelper func(string) (template.HTML, error)
// Init Set current environment and preload manifest
func Init(dev bool) {
AssetHelper = GetAssetHelper(&Config{
DevHost: DevHost,
FsPath: FsPath,
WebPath: WebPath,
Plugin: Plugin,
IgnoreMissing: IgnoreMissing,
Verbose: Verbose,
IsDev: dev,
})
}
// BasicConfig returns a config with basic options set to defaults
func BasicConfig(host, path, webPath string) *Config {
return &Config{
DevHost: host,
FsPath: path,
WebPath: webPath,
Plugin: "manifest",
IgnoreMissing: true,
Verbose: true,
IsDev: false,
}
}
func readManifest(conf *Config) (AssetList, error) {
//if conf.Verbose {
//log.Println("go-webpack: reading manifest. Plugin:", conf.Plugin, "dev:", conf.IsDev, "dev host:", conf.DevHost, "fs path:", conf.FsPath, "web path:", conf.WebPath)
//}
return reader.Read(conf.Plugin, conf.DevHost, conf.FsPath, conf.WebPath, conf.IsDev)
}
// ErrorFunction returns a template function that returns a fixed error message
func ErrorFunction(err error) func(string) (template.HTML, error) {
log.Println("go-webpack: error:", err)
return func(string) (template.HTML, error) {
return template.HTML(""), err
}
}
// GetAssetHelper returns an asset helper function based on your config, for use with multiple webpack manifests
func GetAssetHelper(conf *Config) func(string) (template.HTML, error) {
preloadedAssets := AssetList{}
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(conf)
if err != nil {
log.Println(err)
}
//if err != nil {
//return ErrorFunction(err)
//}
} else {
preloadedAssets, err = readManifest(conf)
// we won't ever re-check assets in this case. this should be a hard error.
if err != nil {
return ErrorFunction(err)
}
}
return createAssetHelper(conf, preloadedAssets)
}
func displayAssetError(conf *Config, key string, assets AssetList) (template.HTML, error) {
message := "go-webpack: Asset file '" + key + "' not found in manifest"
if conf.Verbose {
log.Printf("%s. Manifest contents:", message)
for k, a := range assets {
log.Printf("%s: %s", k, a)
}
}
if conf.IgnoreMissing {
return template.HTML(""), nil
}
return template.HTML(""), errors.New(message)
}
func createAssetHelper(conf *Config, preloadedAssets AssetList) func(string) (template.HTML, error) {
return func(key string) (template.HTML, error) {
var err error
var assets AssetList
if conf.IsDev {
assets, err = readManifest(conf)
if err != nil {
return template.HTML(""), err
}
} 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 {
return displayAssetError(conf, key, assets)
}
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
}
}