Authored by 毕凯

去除 webpack 插件

... ... @@ -128,7 +128,6 @@
"webpack-dev-server": "^2.8.2",
"webpack-hot-middleware": "^2.19.1",
"webpack-merge": "^4.1.0",
"workbox-webpack-plugin": "^2.1.2",
"yoho-cookie": "^1.2.0",
"yoho-fastclick": "^1.0.6",
"yoho-hammer": "^2.0.8",
... ...
... ... @@ -12,7 +12,6 @@ const shelljs = require('shelljs');
const _ = require('lodash');
const webpack = require('webpack');
const HappyPack = require('happypack');
const WorkboxPlugin = require('workbox-webpack-plugin');
const { cssLoader, hbsLoader } = require('./utils.js');
const postcssConfig = require('./postcss.config.js');
... ... @@ -125,13 +124,6 @@ module.exports = (env) => {
$: 'yoho-jquery',
jQuery: 'yoho-jquery',
'window.jQuery': 'yoho-jquery'
}),
new WorkboxPlugin({
globDirectory: path.join(__dirname, 'bundle'),
globPatterns: ['**/*.{css,js}'],
swDest: path.join(__dirname, '../static', 'sw.js'),
clientsClaim: true,
skipWaiting: true,
})
]
};
... ...
const webpack = require('webpack');
const path = require('path');
const merge = require('webpack-merge');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
... ... @@ -8,9 +7,10 @@ let baseConfig = require('./webpack.base.config.js');
baseConfig = baseConfig('dev');
Object.keys(baseConfig.entry).forEach(function(name) {
baseConfig.entry[name] = [path.join(__dirname, './dev-client.js')].concat(baseConfig.entry[name]);
});
// const path = require('path');
// Object.keys(baseConfig.entry).forEach(function(name) {
// baseConfig.entry[name] = [path.join(__dirname, './dev-client.js')].concat(baseConfig.entry[name]);
// });
module.exports = merge(baseConfig, {
output: {
... ...
... ... @@ -9,6 +9,8 @@ const qs = require('yoho-qs');
const tip = require('plugin/tip');
const yoho = require('./yoho-app');
require('./pwa');
// 如果不是 App,就把这个 cookie 清理掉,解决之前缓存错误的问题 20171020
const isApp = qs.app_version || /YohoBuy/i.test(navigator.userAgent || '');
... ... @@ -48,59 +50,3 @@ $(document).ajaxError((event, xhr) => {
}
}
});
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js', {scope: '/'}).then(function(registration) {
console.log('SW registered: ', registration);
// this.addEventListener('install', function(event) {
// event.waitUntil(
// caches.open('my-test-cache-v1').then(function(cache) {
// return cache.addAll([
// '/'
// ]);
// })
// );
// });
// this.addEventListener('fetch', function(event) {
// console.log(event.request);
// event.respondWith(
// caches.match(event.request).then(function(response) {
// // 来来来,代理可以搞一些代理的事情
// // 如果 Service Worker 有自己的返回,就直接返回,减少一次 http 请求
// if (response) {
// return response;
// }
// // 如果 service worker 没有返回,那就得直接请求真实远程服务
// let request = event.request.clone(); // 把原始请求拷过来
// return fetch(request).then(function(httpRes) {
// // http请求的返回已被抓到,可以处置了。
// // 请求失败了,直接返回失败的结果就好了。。
// if (!httpRes || httpRes.status !== 200) {
// return httpRes;
// }
// // 请求成功的话,将请求缓存起来。
// let responseClone = httpRes.clone();
// caches.open('my-test-cache-v1').then(function(cache) {
// cache.put(event.request, responseClone);
// });
// return httpRes;
// });
// })
// );
// });
}).catch(registrationError => {
console.log('SW registration failed: ', registrationError);
});
});
}
... ...
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js', {scope: '/'}).then(function(registration) {
console.log('SW registered: ', registration);
}).catch(registrationError => {
console.log('SW registration failed: ', registrationError);
});
});
}
... ...
importScripts('workbox-sw.prod.v2.1.2.js');
/**
* DO NOT EDIT THE FILE MANIFEST ENTRY
*
* The method precache() does the following:
* 1. Cache URLs in the manifest to a local cache.
* 2. When a network request is made for any of these URLs the response
* will ALWAYS comes from the cache, NEVER the network.
* 3. When the service worker changes ONLY assets with a revision change are
* updated, old cache entries are left as is.
*
* By changing the file manifest manually, your users may end up not receiving
* new versions of files because the revision hasn't changed.
*
* Please use workbox-build or some other tool / approach to generate the file
* manifest which accounts for changes to local files and update the revision
* accordingly.
*/
const fileManifest = [
const workboxSW = new self.WorkboxSW({
clientsClaim: true,
skipWaiting: true
});
// self.addEventListener('install', function(event) {
// event.waitUntil(
// caches.open('my-test-cache-v1').then(function(cache) {
// return cache.addAll([
// '/'
// ]);
// })
// );
// });
self.addEventListener('error', event => {
// 上报错误信息
// 常用的属性:
// event.message
// event.filename
// event.lineno
// event.colno
// event.error.stack
})
self.addEventListener('unhandledrejection', event => {
// 上报错误信息
// 常用的属性:
// event.reason
})
self.addEventListener('fetch', function (event) {
console.log(event.request);
event.respondWith(
caches.match(event.request).then(function (response) {
// 来来来,代理可以搞一些代理的事情
// 如果 Service Worker 有自己的返回,就直接返回,减少一次 http 请求
if (response) {
return response;
}
// 如果 service worker 没有返回,那就得直接请求真实远程服务
let request = event.request.clone(); // 把原始请求拷过来
return fetch(request).then(function (httpRes) {
// http请求的返回已被抓到,可以处置了。
// 请求失败了,直接返回失败的结果就好了。。
if (!httpRes || httpRes.status !== 200) {
return httpRes;
}
// 请求成功的话,将请求缓存起来。
let responseClone = httpRes.clone();
console.log(httpRes)
caches.open('custom-cache').then(function (cache) {
cache.put(event.request, responseClone);
});
return httpRes;
}).catch(console.error);
})
);
});
workboxSW.precache([
{
"url": "sw.js",
"revision": "3764cbf82eff6f9c492a134d2059ff8f"
"url": "sw.js"
},
{
"url": "workbox-sw.prod.v2.1.2.js",
"revision": "685d1ceb6b9a9f94aacf71d6aeef8b51"
"url": "workbox-sw.prod.v2.1.2.js"
}
];
const workboxSW = new self.WorkboxSW({
"skipWaiting": true,
"clientsClaim": true
});
workboxSW.precache(fileManifest);
]);
... ...
... ... @@ -184,4 +184,3 @@ class WorkboxSW$1{constructor({cacheId:a,skipWaiting:b,clientsClaim:c,handleFetc
return WorkboxSW$1;
}());
//# sourceMappingURL=workbox-sw.prod.v2.1.2.js.map
... ...
This diff could not be displayed because it is too large.