From d360323492405d2a16e13653819107e2efb89b97 Mon Sep 17 00:00:00 2001 From: Wangyisheng Date: Fri, 8 Sep 2017 16:00:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=95=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E5=A2=9E=E5=8A=A0SAMPLE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- build/webpack.conf.js | 2 +- main.js | 8 ++++- product/SAMPLE.md | 63 ++++++++++++++++++++++++++++++++++++++ product/index.js | 14 --------- product/pageSearch.conf.js | 28 ----------------- product/superFrame.conf.js | 28 ----------------- 7 files changed, 72 insertions(+), 73 deletions(-) create mode 100644 product/SAMPLE.md delete mode 100644 product/index.js delete mode 100644 product/pageSearch.conf.js delete mode 100644 product/superFrame.conf.js diff --git a/README.md b/README.md index 3feff03..d25d772 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ crater 会根据根目录下的 `product` 目录中的配置文件生成一份 ` ## 使用方式 -开发者将代码 clone 到本地后有 __三__ 个步骤需要进行: +开发者将代码 clone 到本地后有三个步骤需要进行: ### 编写配置文件 diff --git a/build/webpack.conf.js b/build/webpack.conf.js index 704e006..5b6d7e5 100644 --- a/build/webpack.conf.js +++ b/build/webpack.conf.js @@ -16,7 +16,7 @@ module.exports = { ['serviceworker-cache-polyfill', resolve('main.js')], output: { path: config.build.outputPath, - filename: isTest ? 'service-worker.js' : 'service-worker-[hash:8].js' + filename: isTest ? 'service-worker.js' : 'service-worker.js' }, devtool: config.build.productionSourceMap ? '#source-map' : false, module: { diff --git a/main.js b/main.js index 561eb97..4f2186d 100644 --- a/main.js +++ b/main.js @@ -6,7 +6,13 @@ 'use strict'; import sw from './lib/sw-base'; -import product from './product'; + +function requireAll(requireContext) { + return requireContext.keys().map(requireContext); +} + +// import product/* +const product = requireAll(require.context('./product', true, /^\.\/.*\.js$/)); /** * 获取缓存名称 diff --git a/product/SAMPLE.md b/product/SAMPLE.md new file mode 100644 index 0000000..a606e6f --- /dev/null +++ b/product/SAMPLE.md @@ -0,0 +1,63 @@ +# Product Sample + +You should list all configures of your products here, including __WHAT__ resources should be cached in __WHICH__ strategy. Sample config files are as follows: + +## Sample 1: pageSearch.conf.js + +```javascript + +'use strict'; + +export default { + + name: 'pageSearch', + + // referrer url patterns + referrerPattern: /\/s\?/, + + validateReferrer: 'https://m.baidu.com/s?word=123&ts=0&t_kt=0&ie=utf-8&rsv_iqid=2950933944&rsv_t=59feNNSsxLiVpQVUwj7SxdZ%252BTEvttKzgZj7zBw%252BUz%252FRihNQcWwKR&sa=ib&rsv_pq=2950933944', + + routers: [ + { + method: 'get', + // static file url patterns + urlPattern: /se\/static\/(js|pmd|css)\/.*(css|js)$/, + strategy: 'networkFirst', + validate: [{ + url: '/se/static/js/uiamd/bdbox/follow_4ff41a2.js' + }] + } + ] +}; + +``` + +## Sample 2: superFrame.conf.js + +```javascript + +'use strict'; + +export default { + + name: 'superFrame', + + // referrer url patterns + referrerPattern: /\/sf\?/, + + validateReferrer: 'https://m.baidu.com/sf?openapi=1&dspName=iphone&from_sf=1&pd=city&resource_id=4324&ms=1&ms=1&word=%E5%8C%97%E4%BA%AC&hide=1&apitn=tangram&top=%7B%22sfhs%22%3A2%7D&city_name=None&title=%E5%8C%97%E4%BA%AC%E6%97%85%E6%B8%B8&lid=4572408700669128688&frsrcid=32228&frorder=1', + + routers: [ + { + method: 'get', + // static file url patterns + urlPattern: /se\/static\/sf\/.*(css|js)$/, + strategy: 'networkFirst', + validate: [{ + url: '/se/static/sf/app/pmd/pmd/deps/naboo_eaee39f.js' + }] + } + ] +}; + +``` diff --git a/product/index.js b/product/index.js deleted file mode 100644 index a60d881..0000000 --- a/product/index.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @file 引入所有配置文件,对外唯一接口 - * @author wangyisheng@outlook.com (wangyisheng) - */ - -'use strict'; - -import pageSearch from './pageSearch.conf'; -import superFrame from './superFrame.conf'; - -export default { - pageSearch, - superFrame -}; diff --git a/product/pageSearch.conf.js b/product/pageSearch.conf.js deleted file mode 100644 index 8fce8ea..0000000 --- a/product/pageSearch.conf.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file 大搜静态文件 - * @author wangyisheng@outlook.com (wangyisheng) - */ - -'use strict'; - -export default { - - name: 'pageSearch', - - // referrer url patterns - referrerPattern: /\/s\?/, - - validateReferrer: 'https://m.baidu.com/s?word=123&ts=0&t_kt=0&ie=utf-8&rsv_iqid=2950933944&rsv_t=59feNNSsxLiVpQVUwj7SxdZ%252BTEvttKzgZj7zBw%252BUz%252FRihNQcWwKR&sa=ib&rsv_pq=2950933944', - - routers: [ - { - method: 'get', - // static file url patterns - urlPattern: /se\/static\/(js|pmd|css)\/.*(css|js)$/, - strategy: 'networkFirst', - validate: [{ - url: '/se/static/js/uiamd/bdbox/follow_4ff41a2.js' - }] - } - ] -}; diff --git a/product/superFrame.conf.js b/product/superFrame.conf.js deleted file mode 100644 index 0325037..0000000 --- a/product/superFrame.conf.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file superframe静态文件 - * @author wangyisheng@outlook.com (wangyisheng) - */ - -'use strict'; - -export default { - - name: 'superFrame', - - // referrer url patterns - referrerPattern: /\/sf\?/, - - validateReferrer: 'https://m.baidu.com/sf?openapi=1&dspName=iphone&from_sf=1&pd=city&resource_id=4324&ms=1&ms=1&word=%E5%8C%97%E4%BA%AC&hide=1&apitn=tangram&top=%7B%22sfhs%22%3A2%7D&city_name=None&title=%E5%8C%97%E4%BA%AC%E6%97%85%E6%B8%B8&lid=4572408700669128688&frsrcid=32228&frorder=1', - - routers: [ - { - method: 'get', - // static file url patterns - urlPattern: /se\/static\/sf\/.*(css|js)$/, - strategy: 'networkFirst', - validate: [{ - url: '/se/static/sf/app/pmd/pmd/deps/naboo_eaee39f.js' - }] - } - ] -};