From 90c96791be8a05d8d44c328163f33e49d63457a7 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 29 May 2018 17:47:46 +0300 Subject: [PATCH] refactor(sw) add directory --- .webpack/js.js | 2 +- client/client.js | 18 ++---------------- client/sw/register.js | 17 +++++++++++++++++ client/{ => sw}/sw.js | 0 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 client/sw/register.js rename client/{ => sw}/sw.js (100%) diff --git a/.webpack/js.js b/.webpack/js.js index 9f88ad1c..79a0f0a1 100644 --- a/.webpack/js.js +++ b/.webpack/js.js @@ -50,7 +50,7 @@ const rules = clean([ const plugins = [ new EnvironmentPlugin(['NODE_ENV']), new ServiceWorkerWebpackPlugin({ - entry: join(__dirname, '../client', 'sw.js'), + entry: join(__dirname, '..', 'client', 'sw', 'sw.js'), }), ]; diff --git a/client/client.js b/client/client.js index f0ef8ed9..c910dce0 100644 --- a/client/client.js +++ b/client/client.js @@ -8,12 +8,11 @@ const inherits = require('inherits'); const rendy = require('rendy/legacy'); const exec = require('execon'); const Images = require('./dom/images'); +const registerSW = require('./sw/register'); const join = require('join-io/www/join'); const jonny = require('jonny/legacy'); const currify = require('currify/legacy'); -const runtime = require('serviceworker-webpack-plugin/lib/runtime'); - const bind = (f, ...a) => () => f(...a); const noop = () => {}; @@ -41,7 +40,7 @@ function CloudCmdProto(Util, DOM) { console.log(str); }; - serviceWorker(); + registerSW(); Emitify.call(this); @@ -611,18 +610,5 @@ function CloudCmdProto(Util, DOM) { }); }); }; - - function serviceWorker() { - if (!navigator.serviceWorker) - return; - - const isHTTPS = location.protocol === 'https:'; - const isLocalhost = location.hostname === 'localhost'; - - if (!isHTTPS && !isLocalhost) - return; - - runtime.register(); - } } diff --git a/client/sw/register.js b/client/sw/register.js new file mode 100644 index 00000000..c0a6669e --- /dev/null +++ b/client/sw/register.js @@ -0,0 +1,17 @@ +'use strict'; + +const runtime = require('serviceworker-webpack-plugin/lib/runtime'); + +module.exports = () => { + if (!navigator.serviceWorker) + return; + + const isHTTPS = location.protocol === 'https:'; + const isLocalhost = location.hostname === 'localhost'; + + if (!isHTTPS && !isLocalhost) + return; + + runtime.register(); +}; + diff --git a/client/sw.js b/client/sw/sw.js similarity index 100% rename from client/sw.js rename to client/sw/sw.js