Update client dependencies, embed only gzipped assets and unzip on the fly if needed

This commit is contained in:
Ken-Håvard Lieng 2015-05-15 07:32:32 +02:00
parent c4ac9f782c
commit 5a535cf53e
6 changed files with 127 additions and 337 deletions

View File

@ -24,13 +24,13 @@ if (argv.production) {
} }
gulp.task('html', function() { gulp.task('html', function() {
gulp.src('src/*.html') return gulp.src('src/*.html')
.pipe(minifyHTML()) .pipe(minifyHTML())
.pipe(gulp.dest('dist')); .pipe(gulp.dest('dist'));
}); });
gulp.task('css', function() { gulp.task('css', function() {
gulp.src(['src/css/fontello.css', 'src/css/style.css']) return gulp.src(['src/css/fontello.css', 'src/css/style.css'])
.pipe(concat('bundle.css')) .pipe(concat('bundle.css'))
.pipe(autoprefixer()) .pipe(autoprefixer())
.pipe(minifyCSS()) .pipe(minifyCSS())
@ -75,34 +75,30 @@ function js(watch) {
} }
gulp.task('fonts', function() { gulp.task('fonts', function() {
gulp.src('src/font/*') return gulp.src('src/font/*')
.pipe(gulp.dest('dist/font')); .pipe(gulp.dest('dist/font'));
}); });
gulp.task('gzip', ['html', 'css', 'js', 'fonts'], function() { gulp.task('gzip', ['html', 'css', 'js', 'fonts'], function() {
gulp.src('dist/**/!(*.gz)') return gulp.src('dist/**/!(*.gz)')
.pipe(gzip()) .pipe(gzip())
.pipe(gulp.dest('dist')); .pipe(gulp.dest('dist/gz'));
}); });
function bindata(cb) { function bindata(cb) {
if (argv.production) { if (argv.production) {
exec('go-bindata -nomemcopy -pkg server -o ../server/bindata.go dist/...', function(err) { exec('go-bindata -nomemcopy -nocompress -pkg server -o ../server/bindata.go dist/gz/...', cb);
cb(err);
});
} else { } else {
exec('go-bindata -debug -pkg server -o ../server/bindata.go dist/...', function(err) { exec('go-bindata -debug -pkg server -o ../server/bindata.go dist/...', cb);
cb(err);
});
} }
} }
gulp.task('bindata', ['html', 'css', 'js', 'fonts', 'gzip'], function(cb) { gulp.task('bindata', ['gzip'], function(cb) {
bindata(cb); bindata(cb);
}); });
gulp.task('gzip:watch', function() { gulp.task('gzip:watch', function() {
gulp.src('dist/**/*.{html,css,js}') return gulp.src('dist/**/*.{html,css,js}')
.pipe(gzip()) .pipe(gzip())
.pipe(gulp.dest('dist')); .pipe(gulp.dest('dist'));
}); });

View File

@ -9,23 +9,25 @@
"vinyl-source-stream": "~1.0.0", "vinyl-source-stream": "~1.0.0",
"gulp-if": "~1.2.5", "gulp-if": "~1.2.5",
"gulp": "~3.8.10", "gulp": "~3.8.10",
"gulp-uglify": "~1.0.2", "gulp-uglify": "1.2.0",
"gulp-minify-css": "~0.3.11", "gulp-minify-css": "1.1.1",
"gulp-streamify": "0.0.5", "gulp-streamify": "0.0.5",
"gulp-minify-html": "~0.1.8", "gulp-minify-html": "1.0.2",
"watchify": "~2.2.1", "watchify": "3.2.1",
"browserify": "~8.0.3", "browserify": "10.2.0",
"gulp-autoprefixer": "~2.0.0", "gulp-autoprefixer": "2.2.0",
"gulp-gzip": "0.0.8", "gulp-gzip": "0.0.8",
"babelify": "~5.0.3", "babelify": "6.1.0",
"gulp-concat": "~2.5.2" "gulp-concat": "~2.5.2",
"reactify": "^1.1.1",
"del": "^1.1.1"
}, },
"dependencies": { "dependencies": {
"lodash": "3.2.0", "lodash": "3.8.0",
"reflux": "0.2.5", "reflux": "0.2.7",
"react-router": "0.12.0", "react-router": "0.13.3",
"react": "0.12.2", "react": "0.13.3",
"react-infinite": "0.2.3", "react-infinite": "0.3.4",
"autolinker": "khlieng/Autolinker.js" "autolinker": "khlieng/Autolinker.js"
} }
} }

View File

@ -2,6 +2,8 @@ var EventEmitter = require('events').EventEmitter;
class Socket extends EventEmitter { class Socket extends EventEmitter {
constructor() { constructor() {
super();
this.ws = new WebSocket('ws://' + window.location.host + '/ws'); this.ws = new WebSocket('ws://' + window.location.host + '/ws');
this.ws.onopen = () => this.emit('connect'); this.ws.onopen = () => this.emit('connect');

File diff suppressed because one or more lines are too long

View File

@ -1,37 +0,0 @@
package server
import (
"bytes"
"net/http"
"os"
)
type BindataFileSystem struct{}
func (f BindataFileSystem) Open(name string) (http.File, error) {
path := "dist" + name
data, err := Asset(path)
if err != nil {
return nil, err
}
return &BindataFile{bytes.NewReader(data), path}, nil
}
type BindataFile struct {
*bytes.Reader
path string
}
func (f *BindataFile) Close() error {
return nil
}
func (f *BindataFile) Readdir(count int) ([]os.FileInfo, error) {
return make([]os.FileInfo, 0), nil
}
func (f *BindataFile) Stat() (os.FileInfo, error) {
return AssetInfo(f.path)
}

View File

@ -1,6 +1,9 @@
package server package server
import ( import (
"bytes"
"compress/gzip"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -17,8 +20,15 @@ var (
channelStore *storage.ChannelStore channelStore *storage.ChannelStore
sessions map[string]*Session sessions map[string]*Session
sessionLock sync.Mutex sessionLock sync.Mutex
fs http.Handler
files []File files = []File{
File{"/bundle.js", "text/javascript"},
File{"/bundle.css", "text/css"},
File{"/font/fontello.eot", "application/vnd.ms-fontobject"},
File{"/font/fontello.svg", "image/svg+xml"},
File{"/font/fontello.ttf", "application/x-font-ttf"},
File{"/font/fontello.woff", "application/font-woff"},
}
upgrader = websocket.Upgrader{ upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
@ -39,16 +49,6 @@ func Run(port int) {
channelStore = storage.NewChannelStore() channelStore = storage.NewChannelStore()
sessions = make(map[string]*Session) sessions = make(map[string]*Session)
fs = http.FileServer(BindataFileSystem{})
files = []File{
File{"/bundle.js", "text/javascript"},
File{"/bundle.css", "text/css"},
File{"/font/fontello.eot", "application/vnd.ms-fontobject"},
File{"/font/fontello.svg", "image/svg+xml"},
File{"/font/fontello.ttf", "application/x-font-ttf"},
File{"/font/fontello.woff", "application/font-woff"},
}
reconnect() reconnect()
@ -72,33 +72,36 @@ func upgradeWS(w http.ResponseWriter, r *http.Request) {
} }
func serveFiles(w http.ResponseWriter, r *http.Request) { func serveFiles(w http.ResponseWriter, r *http.Request) {
var ext string
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
ext = ".gz"
}
if r.URL.Path == "/" { if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html") serveFile("dist/gz/index.html.gz", "text/html", w, r)
r.URL.Path = "/index.html" + ext
fs.ServeHTTP(w, r)
return return
} }
for _, file := range files { for _, file := range files {
if strings.HasSuffix(r.URL.Path, file.Path) { if strings.HasSuffix(r.URL.Path, file.Path) {
w.Header().Set("Content-Type", file.ContentType) serveFile("dist/gz"+file.Path+".gz", file.ContentType, w, r)
r.URL.Path = file.Path + ext
fs.ServeHTTP(w, r)
return return
} }
} }
w.Header().Set("Content-Type", "text/html") serveFile("dist/gz/index.html.gz", "text/html", w, r)
r.URL.Path = "/index.html" + ext }
fs.ServeHTTP(w, r) func serveFile(path, contentType string, w http.ResponseWriter, r *http.Request) {
data, _ := Asset(path)
w.Header().Set("Content-Type", contentType)
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
} else {
gzr, _ := gzip.NewReader(bytes.NewReader(data))
buf, _ := ioutil.ReadAll(gzr)
w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
w.Write(buf)
}
} }
func reconnect() { func reconnect() {