Authored by 毕凯

add webpack & es6 support

... ... @@ -6,4 +6,4 @@ public/static/yas-jssdk
public/js/home/jquery.upload.js
public/js/activity/live
public/build/dll
workbox*.js
public/static/sw.js
... ...
... ... @@ -11,6 +11,7 @@
"start": "NODE_ENV=\"production\" node app.js",
"dev": "nodemon -e js,hbs -i public/ app.js",
"static": "node ./public/build/dev-server.js",
"build:sw": "webpack --config public/build/webpack.sw.config.js --verbose --debug",
"build:dll": "webpack --config ./public/build/webpack.dll.indexcss.config.js",
"build": "webpack --config ./public/build/webpack.dll.indexcss.config.js && webpack --config ./public/build/webpack.prod.config.js",
"debug": "DEBUG=\"express:*\" nodemon -e js,hbs -i public/ app.js",
... ... @@ -128,6 +129,7 @@
"webpack-dev-server": "^2.8.2",
"webpack-hot-middleware": "^2.19.1",
"webpack-merge": "^4.1.0",
"workbox-sw": "^2.1.2",
"yoho-cookie": "^1.2.0",
"yoho-fastclick": "^1.0.6",
"yoho-hammer": "^2.0.8",
... ...
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: {
sw: path.join(__dirname, '../js/pwa/sw.js')
},
output: {
path: path.join(__dirname, '../static'),
filename: '[name].js'
},
target: 'webworker',
module: {
rules: [{
test: /\.(js|mjs)$/,
include: [path.join(__dirname, '../../node_modules/workbox-sw')],
use: 'babel-loader'
}]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin()
]
};
... ...
... ... @@ -9,7 +9,7 @@ const qs = require('yoho-qs');
const tip = require('plugin/tip');
const yoho = require('./yoho-app');
require('./pwa');
require('./pwa/pwa');
// 如果不是 App,就把这个 cookie 清理掉,解决之前缓存错误的问题 20171020
const isApp = qs.app_version || /YohoBuy/i.test(navigator.userAgent || '');
... ...
/* eslint-env worker */
import WorkboxSW from 'workbox-sw';
import parseQs from 'yoho-qs/parse';
const config = {
customCacheUrl: [
/^https:\/\/(.*)cdn\.yoho\.cn/i,
/^https:\/\/(.*)static\.yhbimg\.com/i
],
precacheStaticFile: [
'/index.css',
'/common.css',
'/libs.js'
],
precacheFile: [{
url: 'https://cdn.yoho.cn/pwa/workbox-sw.prod.v2.1.2.js'
}]
};
const qs = parseQs(self.location.search.substr(1));
const workboxSW = new WorkboxSW({
clientsClaim: true,
skipWaiting: true
});
const cacheFirstStrategy = workboxSW.strategies.cacheFirst({
cacheableResponse: {
statuses: [0, 200]
},
cacheExpiration: {
maxEntries: 1000,
maxAgeSeconds: 7 * 24 * 60 * 60 // 7 day
}
});
// 特殊路径的预缓存文件文件
config.precacheFile = config.precacheFile.concat([{
url: '/sw.js?t=' + qs.t + '&staticServer=' + qs.staticServer
}]).concat(config.precacheStaticFile.map(function(file) {
// 根据业务自定义资源路径
const url = self.location.protocol + qs.staticServer + file + '?t=' + qs.t;
return {
url: url
};
}));
// 预加载文件
workboxSW.precache(config.precacheFile);
// 自定义缓存
config.customCacheUrl.forEach(function(urlRegExp) {
workboxSW.router.registerRoute(
urlRegExp,
cacheFirstStrategy
);
});
... ...
This diff could not be displayed because it is too large.