Skip to content

Commit

Permalink
minify service worker script before html inlining (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
anubhav7495 committed Jul 19, 2017
1 parent 6d57c87 commit 990f1ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions template/build/load-minified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fs = require('fs')
var UglifyJS = require('uglify-es')

module.exports = function(filePath) {
var code = fs.readFileSync(filePath, 'utf-8')
var result = UglifyJS.minify(code)
if (result.error) return ''
return result.code
}
5 changes: 3 additions & 2 deletions template/build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
var SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
var loadMinified = require('./load-minfied')

var env = {{#if_or unit e2e}}process.env.NODE_ENV === 'testing'
? require('../config/test.env')
Expand Down Expand Up @@ -68,8 +69,8 @@ var webpackConfig = merge(baseWebpackConfig, {
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency',
serviceWorkerLoader: `<script>${fs.readFileSync(path.join(__dirname,
'./service-worker-prod.js'), 'utf-8')}</script>`
serviceWorkerLoader: `<script>${loadMinified(path.join(__dirname,
'./service-worker-prod.js'))}</script>`
}),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
Expand Down
3 changes: 2 additions & 1 deletion template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@
"webpack": "^2.6.1",
"webpack-dev-middleware": "^1.10.0",
"webpack-hot-middleware": "^2.18.0",
"webpack-merge": "^4.1.0"
"webpack-merge": "^4.1.0",
"uglify-es": "^3.0.25"
},
"engines": {
"node": ">= 4.0.0",
Expand Down

0 comments on commit 990f1ea

Please sign in to comment.