Skip to content

Commit

Permalink
feat: inject service worker for PWA ✨ #24
Browse files Browse the repository at this point in the history
  • Loading branch information
nicejade committed May 5, 2018
1 parent 865fb4f commit ea2ef91
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
20 changes: 12 additions & 8 deletions build/webpack.dev.conf.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const fs = require('fs')
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
Expand All @@ -29,7 +31,9 @@ module.exports = merge(baseWebpackConfig, {
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.ejs',
inject: true
inject: true,
serviceWorkerLoader: `<script>${fs.readFileSync(path.join(__dirname,
'./service-worker-dev.js'), 'utf-8')}</script>`
}),
new FriendlyErrorsPlugin()
]
Expand Down
37 changes: 20 additions & 17 deletions build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
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 LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
var ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')
const fs = require('fs')
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')

var env = process.env.NODE_ENV === 'testing'
const env = process.env.NODE_ENV === 'testing'
? require('../config/test.env')
: config.build.env

var webpackConfig = merge(baseWebpackConfig, {
const webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
Expand Down Expand Up @@ -83,7 +84,9 @@ var webpackConfig = merge(baseWebpackConfig, {
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
chunksSortMode: 'dependency',
serviceWorkerLoader: `<script>${loadMinified(path.join(__dirname,
'./service-worker-prod.js'))}</script>`
}),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
Expand Down Expand Up @@ -118,7 +121,7 @@ var webpackConfig = merge(baseWebpackConfig, {
})

if (config.build.productionGzip) {
var CompressionWebpackPlugin = require('compression-webpack-plugin')
const CompressionWebpackPlugin = require('compression-webpack-plugin')

webpackConfig.plugins.push(
new CompressionWebpackPlugin({
Expand All @@ -136,7 +139,7 @@ if (config.build.productionGzip) {
}

if (config.build.bundleAnalyzerReport) {
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}

Expand Down
3 changes: 3 additions & 0 deletions index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<!--[if lt IE 9]>
<script async src="<%= webpackConfig.output.publicPath %>static/js/browsermodal.js"></script>
<![endif]-->
<!-- Todo: only include in production -->
<%= htmlWebpackPlugin.options.serviceWorkerLoader %>
<!-- built files will be auto injected -->
</body>
<script>
console.log('“倾心缔造,痴心为你”:%c倾城之链', 'font-weight: 600;color: #2c85ff')
Expand Down
2 changes: 1 addition & 1 deletion static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "image/png"
}
],
"start_url": "/?from=device-screen",
"start_url": "https://nicelinks.site/?from=device-screen",
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
Expand Down

0 comments on commit ea2ef91

Please sign in to comment.