Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: add a common chunk (#636)

Guilherme Oenning8e63220c615e
diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.go
index 644952dee..9edff9627 100644
--- a/app/pkg/web/renderer.go
+++ b/app/pkg/web/renderer.go
@@ -1,10 +1,13 @@
package web
import (
+ "encoding/json"
"fmt"
"html/template"
"io"
+ "os"
"strings"
+ "sync"
"io/ioutil"
@@ -26,12 +29,18 @@ var templateFunctions = template.FuncMap{
},
}
+type clientAssets struct {
+ CSS []string
+ JS []string
+}
+
//Renderer is the default HTML Render
type Renderer struct {
templates map[string]*template.Template
logger log.Logger
settings *models.SystemSettings
- assets map[string]string
+ assets *clientAssets
+ sync.RWMutex
}
// NewRenderer creates a new Renderer
@@ -40,7 +49,6 @@ func NewRenderer(settings *models.SystemSettings, logger log.Logger) *Renderer {
templates: make(map[string]*template.Template),
logger: logger,
settings: settings,
- assets: make(map[string]string, 0),
}
}
@@ -57,33 +65,67 @@ func (r *Renderer) add(name string) *template.Template {
return tpl
}
-func (r *Renderer) getBundle(folder, prefix, suffix string) string {
- files, _ := ioutil.ReadDir(env.Path(folder))
- if len(files) > 0 {
- for _, file := range files {
- fileName := file.Name()
- if strings.HasPrefix(fileName, prefix) && strings.HasSuffix(fileName, suffix) {
- return fileName
- }
- }
+func (r *Renderer) loadAssets(ctx *Context) error {
+ r.Lock()
+ defer r.Unlock()
+
+ if r.assets != nil && env.IsProduction() {
+ return nil
+ }
+
+ ctx.logger.Debug("Loading client assets")
+
+ type assetsFile struct {
+ Entrypoints struct {
+ Main struct {
+ Assets []string `json:"assets"`
+ } `json:"main"`
+ } `json:"entrypoints"`
}
- // Panic if bundle is not available in production mode
- if env.IsProduction() {
- panic(fmt.Sprintf("Bundle not found: %s/%s.", folder, prefix))
+ jsonFile, err := os.Open(env.Path("/dist/assets.json"))
+ if err != nil {
+ return errors.Wrap(err, "failed to open file: assets.json")
}
+ defer jsonFile.Close()
- return ""
+ jsonBytes, _ := ioutil.ReadAll(jsonFile)
+ file := &assetsFile{}
+ err = json.Unmarshal([]byte(jsonBytes), file)
+ if err != nil {
+ return errors.Wrap(err, "failed to parse file: assets.json")
+ }
+
+ r.assets = &clientAssets{
+ CSS: make([]string, 0),
+ JS: make([]string, 0),
+ }
+
+ for _, asset := range file.Entrypoints.Main.Assets {
+ if strings.HasSuffix(asset, ".map") {
+ continue
+ }
+
+ assetURL := ctx.GlobalAssetsURL("/assets/" + asset)
+ if strings.HasSuffix(asset, ".css") {
+ r.assets.CSS = append(r.assets.CSS, assetURL)
+ } else if strings.HasSuffix(asset, ".js") {
+ r.assets.JS = append(r.assets.JS, assetURL)
+ }
+ }
+
+ return nil
}
//Render a template based on parameters
func (r *Renderer) Render(w io.Writer, name string, props Props, ctx *Context) {
var err error
- if len(r.assets) == 0 || env.IsDevelopment() {
- r.assets["main.js"] = r.getBundle("/dist/js", "main", "js")
- r.assets["vendor.js"] = r.getBundle("/dist/js", "vendor", "js")
- r.assets["main.css"] = r.getBundle("/dist/css", "main", "css")
+ if r.assets == nil || env.IsDevelopment() {
+ err := r.loadAssets(ctx)
+ if err != nil && !env.IsTest() {
+ panic(err)
+ }
}
tmpl, ok := r.templates[name]
@@ -110,9 +152,7 @@ func (r *Renderer) Render(w io.Writer, name string, props Props, ctx *Context) {
m["__description"] = fmt.Sprintf("%.150s", description)
}
- m["__vendorBundle"] = ctx.GlobalAssetsURL("/assets/js/%s", r.assets["vendor.js"])
- m["__jsBundle"] = ctx.GlobalAssetsURL("/assets/js/%s", r.assets["main.js"])
- m["__cssBundle"] = ctx.GlobalAssetsURL("/assets/css/%s", r.assets["main.css"])
+ m["__assets"] = r.assets
m["__logo"] = ctx.LogoURL()
m["__favicon"] = ctx.FaviconURL()
m["__contextID"] = ctx.ContextID()
diff --git a/public/index.tsx b/public/index.tsx
index 6ea45d165..1ac6f55ef 100644
--- a/public/index.tsx
+++ b/public/index.tsx
@@ -7,7 +7,6 @@ import { classSet, Fider, actions, navigator } from "@fider/services";
import { ToastContainer, toast } from "react-toastify";
import { IconContext } from "react-icons";
-import "react-toastify/dist/ReactToastify.css";
import "@fider/assets/styles/main.scss";
const logProductionError = (err: Error) => {
diff --git a/views/base.html b/views/base.html
index bd8810839..e5af1ae8f 100644
--- a/views/base.html
+++ b/views/base.html
@@ -1,23 +1,27 @@
<!DOCTYPE html>
<html>
<head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <link rel="icon" type="image/x-icon" href="{{ .__favicon }}">
- <link rel="stylesheet" href="{{ .__cssBundle }}" />
- <title>{{ .__title }}</title>
- <meta name="description" content="{{ .__description }}" />
- <meta property="og:title" content="{{ .__title }}" />
- <meta property="og:description" content="{{ .__description }}" />
- <meta property="og:type" content="website" />
- <meta property="og:url" content="{{ .__currentURL }}" />
- <meta property="og:image" content="{{ .__logo }}">
- {{ if .__tenant }}
- {{ if .__tenant.CustomCSS }}
- <link rel="stylesheet" href="{{ .__settings.tenantAssetsURL }}/custom/{{ .__tenant.CustomCSS | md5 }}.css" />
- {{ end }}
+ <meta charset="utf-8"/>
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <link rel="icon" type="image/x-icon" href="{{ .__favicon }}">
+ {{ if .__assets }}
+ {{range .__assets.CSS}}
+ <link rel="stylesheet" href="{{ . }}" />
+ {{end}}
+ {{end}}
+ <title>{{ .__title }}</title>
+ <meta name="description" content="{{ .__description }}" />
+ <meta property="og:title" content="{{ .__title }}" />
+ <meta property="og:description" content="{{ .__description }}" />
+ <meta property="og:type" content="website" />
+ <meta property="og:url" content="{{ .__currentURL }}" />
+ <meta property="og:image" content="{{ .__logo }}">
+ {{ if .__tenant }}
+ {{ if .__tenant.CustomCSS }}
+ <link rel="stylesheet" href="{{ .__settings.tenantAssetsURL }}/custom/{{ .__tenant.CustomCSS | md5 }}.css" />
{{ end }}
+ {{ end }}
</head>
<body>
<noscript class="container">
diff --git a/views/index.html b/views/index.html
index ba9a091c8..6a53ada5c 100644
--- a/views/index.html
+++ b/views/index.html
@@ -1,10 +1,13 @@
// Print all variables that does not start with _ (95)
// Compressed to avoid whitespace being printed
{{define "server-data"}}
- {{ . }}
+ {{ . }}
{{ end }}
{{define "content"}}<div id="root"></div><div id="root-modal"></div>{{end}}
{{define "end-of-body"}}
-<script src="{{ .__jsBundle }}" crossorigin="anonymous"></script>
-<script src="{{ .__vendorBundle }}" crossorigin="anonymous"></script>
+ {{ if .__assets }}
+ {{range .__assets.JS}}
+ <script src="{{ . }}" crossorigin="anonymous"></script>
+ {{end}}
+ {{end}}
{{end}}
\ No newline at end of file
diff --git a/webpack.config.js b/webpack.config.js
index 8a3861c9b..6bf19f18e 100755
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -22,7 +22,7 @@ const plugins = [
children: false,
chunks: false,
entrypoints: true,
- chunkGroups: true,
+ chunkGroups: false,
modules: false
}
})
@@ -40,7 +40,19 @@ module.exports = {
mode: process.env.NODE_ENV || "development",
entry: {
main: "./public/index.tsx",
- vendor: [ "react", "react-dom", "tslib", "markdown-it", "react-textarea-autosize", "react-toastify", "react-loadable" ]
+ vendor: [
+ "react",
+ "react-dom",
+ "tslib",
+ "markdown-it",
+ "react-textarea-autosize",
+ "react-toastify",
+ "react-loadable",
+ "react-icons/lib"
+ ],
+ vendorCSS: [
+ "react-toastify/dist/ReactToastify.css"
+ ],
},
output: {
path: __dirname + "/dist",
@@ -57,7 +69,7 @@ module.exports = {
performance: {
maxEntrypointSize: 389120 * maxSizeFactor, // 380 KiB. Should ideally be ~240 KiB
maxAssetSize: 256000 * maxSizeFactor, // 250 KiB
- hints: 'error'
+ hints: "error"
},
module: {
rules: [
@@ -82,6 +94,11 @@ module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
+ common: {
+ chunks: 'all',
+ name: 'common',
+ test: /[\\/]public[\\/](components|services|models)[\\/]/
+ },
vendor: {
chunks: 'all',
name: 'vendor',