Authored by 毕凯

add webpack & es6 support

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