From 3e310724ff3d876cb04dcdbfe4092888805ac988 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 21 Jun 2018 17:52:52 +0300 Subject: [PATCH] feature(cloudcmd) import -> load.js --- .webpack/css.js | 7 +- .webpack/js.js | 4 +- bin/cloudcmd.js | 2 +- client/client.js | 8 + client/load-module.js | 12 +- client/modules/cloud.js | 3 + client/modules/config.js | 5 +- client/modules/contact.js | 47 ++-- client/modules/edit-file-vim.js | 2 + client/modules/edit-file.js | 2 + client/modules/edit-names-vim.js | 83 +++---- client/modules/edit-names.js | 394 ++++++++++++++---------------- client/modules/edit.js | 3 + client/modules/help.js | 2 + client/modules/konsole.js | 2 + client/modules/markdown.js | 4 +- client/modules/menu.js | 2 + client/modules/operation/index.js | 3 +- client/modules/terminal.js | 2 + client/modules/upload.js | 2 + client/modules/view.js | 3 +- client/sw/register.spec.js | 4 +- client/sw/sw.js | 33 +-- package.json | 2 +- 24 files changed, 306 insertions(+), 325 deletions(-) diff --git a/.webpack/css.js b/.webpack/css.js index 2668695f..a196a23a 100644 --- a/.webpack/css.js +++ b/.webpack/css.js @@ -29,11 +29,13 @@ const plugins = [ extractMain, ]; +const cssLoader = isDev ? 'css-loader' : 'css-loader?minimize'; + const rules = [{ test: /\.css$/, exclude: /css\/(nojs|view|config|columns.*)\.css/, use: extractMain.extract([ - 'css-loader?minimize', + cssLoader, ]), }, ...cssPlugins.map(extract), { @@ -60,12 +62,11 @@ function getCSSList(dir) { function extract(extractPlugin) { const {filename} = extractPlugin; - const loader = isDev ? 'css-loader' : 'css-loader?minimize'; return { test: RegExp(`css/${filename}`), use: extractPlugin.extract([ - loader + cssLoader ]) }; } diff --git a/.webpack/js.js b/.webpack/js.js index 00685e7e..61f9b078 100644 --- a/.webpack/js.js +++ b/.webpack/js.js @@ -43,7 +43,7 @@ const rules = clean([ loader: 'babel-loader', }, isDev && { - test: /sw.js$/, + test: /sw\.js$/, exclude: /node_modules/, loader: 'babel-loader', options: babelDev @@ -58,7 +58,7 @@ const plugins = [ const splitChunks = { name: 'cloudcmd.common', - chunks: 'async', + chunks: 'all', }; module.exports = { diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index e2dd3e8f..9ad2cc60 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -54,7 +54,7 @@ const args = require('minimist')(argv.slice(2), { default: { server : true, name : choose(env('name'), config('name')), - auth : choose(env('auth'), config('auth')), + auth : choose(env.bool('auth'), config('auth')), port : config('port'), online : config('online'), open : config('open'), diff --git a/client/client.js b/client/client.js index 2302c17f..e0256aea 100644 --- a/client/client.js +++ b/client/client.js @@ -128,6 +128,7 @@ function CloudCmdProto(DOM) { initModules, baseInit, loadPlugins, + loadStyle, exec.with(CloudCmd.route, location.hash), ], noop); @@ -164,6 +165,13 @@ function CloudCmdProto(DOM) { exec.if(document.body.scrollIntoViewIfNeeded, func, funcBefore); }; + function loadStyle(callback) { + const prefix = CloudCmd.PREFIX; + const name = prefix + '/dist/cloudcmd.common.css'; + + DOM.load.css(name, callback); + } + function loadPlugins(callback) { const prefix = CloudCmd.PREFIX; const plugins = prefix + '/plugins.js'; diff --git a/client/load-module.js b/client/load-module.js index 7629f9a2..17fcf48b 100644 --- a/client/load-module.js +++ b/client/load-module.js @@ -3,11 +3,14 @@ /* global CloudCmd */ const exec = require('execon'); +const {promisify} = require('es6-promisify'); const tryToCatch = require('try-to-catch/legacy'); const { kebabToCamelCase, } = require('../common/util'); +const loadJS = promisify(require('./dom/load').js); + /** * function load modules * @params = {name, path, func, dobefore, arg} @@ -16,7 +19,7 @@ module.exports = function loadModule(params) { if (!params) return; - let path = params.path; + const {path} = params; const name = params.name || path && kebabToCamelCase(path); const doBefore = params.dobefore; @@ -25,10 +28,15 @@ module.exports = function loadModule(params) { CloudCmd[name] = () => { exec(doBefore); - return import(`./modules/${path}` /* webpackChunkName: "cloudcmd-" */).then(async (module) => { + const prefix = CloudCmd.PREFIX; + const pathFull = prefix + CloudCmd.DIRCLIENT_MODULES + path + '.js'; + + return loadJS(pathFull).then(async () => { const newModule = async (f) => f && f(); + const module = CloudCmd[name]; Object.assign(newModule, module); + CloudCmd[name] = newModule; CloudCmd.log('init', name); diff --git a/client/modules/cloud.js b/client/modules/cloud.js index 6980de8e..0f31b85f 100644 --- a/client/modules/cloud.js +++ b/client/modules/cloud.js @@ -14,6 +14,9 @@ const Images = require('../dom/images'); const upload = currify(_upload); +const Name = 'Cloud'; +CloudCmd[Name] = module.exports; + module.exports.init = async () => { await loadFiles(); }; diff --git a/client/modules/config.js b/client/modules/config.js index 573b6b16..69b3263c 100644 --- a/client/modules/config.js +++ b/client/modules/config.js @@ -19,8 +19,9 @@ const Files = require('../dom/files'); const {getTitle} = require('../../common/cloudfunc'); const {Dialog, setTitle} = DOM; -const TITLE = 'Config'; -const alert = currify(Dialog.alert, TITLE); +const Name = 'Config'; +CloudCmd[Name] = module.exports; +const alert = currify(Dialog.alert, Name); const loadSocket = promisify(DOM.loadSocket); diff --git a/client/modules/contact.js b/client/modules/contact.js index 4e7bb89d..54c50671 100644 --- a/client/modules/contact.js +++ b/client/modules/contact.js @@ -4,16 +4,12 @@ 'use strict'; -CloudCmd.Contact = ContactProto; +CloudCmd.Contact = exports; + +const {promisify} = require('es6-promisify'); -const exec = require('execon'); const Images = require('../dom/images'); - -function ContactProto(callback) { - init(callback); - - return exports; -} +const loadJS = require('../dom/load').js; const Events = DOM.Events; const Key = CloudCmd.Key; @@ -23,41 +19,38 @@ module.exports.hide = hide; let Inited = false; -function init(callback) { +module.exports.init = async () => { if (Inited) return; - load(() => { - Inited = true; - - olark.identify('6216-545-10-4223'); - olark('api.box.onExpand', show); - olark('api.box.onShow', show); - olark('api.box.onShrink', hide); - - exec(callback); - }); - Events.addKey(onKey); -} + await load(); + + Inited = true; + + olark.identify('6216-545-10-4223'); + olark('api.box.onExpand', show); + olark('api.box.onShow', show); + olark('api.box.onShrink', hide); +}; -function load(callback) { +const load = promisify((callback) => { const {PREFIX} = CloudCmd; const path = `${PREFIX}/modules/olark/olark.min.js`; Images.show.load('top'); - DOM.load.js(path, callback); -} + return loadJS(path, callback); +}); function show() { Key.unsetBind(); Images.hide(); - if (Inited) - return olark('api.box.expand'); + if (!Inited) + return; - init(show); + olark('api.box.expand'); } function hide() { diff --git a/client/modules/edit-file-vim.js b/client/modules/edit-file-vim.js index dc0493d0..6846c7f3 100644 --- a/client/modules/edit-file-vim.js +++ b/client/modules/edit-file-vim.js @@ -2,6 +2,8 @@ /* global CloudCmd */ +CloudCmd.EditFileVim = exports; + const Events = require('../dom/events'); const {Key} = CloudCmd; diff --git a/client/modules/edit-file.js b/client/modules/edit-file.js index 9a6f3083..1b549f38 100644 --- a/client/modules/edit-file.js +++ b/client/modules/edit-file.js @@ -2,6 +2,8 @@ /* global CloudCmd, DOM*/ +CloudCmd.EditFile = exports; + const Format = require('format-io/legacy'); const exec = require('execon'); const supermenu = require('supermenu'); diff --git a/client/modules/edit-names-vim.js b/client/modules/edit-names-vim.js index 3b360a21..a12af0d1 100644 --- a/client/modules/edit-names-vim.js +++ b/client/modules/edit-names-vim.js @@ -2,54 +2,47 @@ /* global CloudCmd */ -const exec = require('execon'); -const Events = require('../dom/events'); +CloudCmd.EditNamesVim = exports; +const Events = require('../dom/events'); const {Key} = CloudCmd; -CloudCmd.EditNamesVim = function EditNamesVimProto(callback) { - const EditNamesVim = this; - - const ConfigView = { - bindKeys: false, - beforeClose: () => { - Events.rmKey(listener); - CloudCmd.EditNames.isChanged(); - } - }; - - function init(callback) { - exec.series([ - CloudCmd.EditNames, - callback || EditNamesVim.show, - ]); +const ConfigView = { + bindKeys: false, + beforeClose: () => { + Events.rmKey(listener); + CloudCmd.EditNames.isChanged(); } - - this.show = () => { - Events.addKey(listener); - - CloudCmd.EditNames - .show(ConfigView) - .getEditor() - .setKeyMap('vim'); - }; - - this.hide = () => { - CloudCmd.Edit.hide(); - }; - - function listener(event) { - const { - keyCode, - shiftKey, - } = event; - - if (shiftKey && keyCode === Key.ESC) { - event.preventDefault(); - EditNamesVim.hide(); - } - } - - init(callback); }; +module.exports.init = async () => { + await CloudCmd.EditNames(); +}; + +module.exports.show = () => { + Events.addKey(listener); + + CloudCmd.EditNames + .show(ConfigView) + .getEditor() + .setKeyMap('vim'); +}; + +module.exports.hide = hide; + +function hide() { + CloudCmd.Edit.hide(); +} + +function listener(event) { + const { + keyCode, + shiftKey, + } = event; + + if (shiftKey && keyCode === Key.ESC) { + event.preventDefault(); + hide(); + } +} + diff --git a/client/modules/edit-names.js b/client/modules/edit-names.js index ecefce52..bd4bce69 100644 --- a/client/modules/edit-names.js +++ b/client/modules/edit-names.js @@ -2,227 +2,207 @@ /* global CloudCmd, DOM */ +CloudCmd.EditNames = exports; + const currify = require('currify/legacy'); -const store = require('fullstore/legacy'); -const squad = require('squad/legacy'); -const wraptile = require('wraptile/legacy'); const exec = require('execon'); const supermenu = require('supermenu'); const reject = Promise.reject.bind(Promise); -const call = currify((fn, callback) => { - fn(); - callback(); -}); +const Info = DOM.CurrentInfo; +const Dialog = DOM.Dialog; -CloudCmd.EditNames = function EditNamesProto(callback) { - const Info = DOM.CurrentInfo; - const Dialog = DOM.Dialog; +const TITLE = 'Edit Names'; +const alert = currify(Dialog.alert, TITLE); +const refresh = currify(_refresh); +const rename = currify(_rename); + +let Menu; + +const EditNames = exec.bind(); +const ConfigView = { + beforeClose: () => { + exec.ifExist(Menu, 'hide'); + DOM.Events.remove('keydown', keyListener); + isChanged(); + } +}; + +module.exports.init = async () => { + await CloudCmd.Edit(); - const TITLE = 'Edit Names'; - const alert = currify(Dialog.alert, TITLE); - const refresh = currify(_refresh); - const rename = currify(_rename); + const editor = CloudCmd.Edit.getEditor(); + setListeners(editor); +}; + +module.exports.show = (options) => { + const names = getActiveNames().join('\n'); + const config = { + ...ConfigView, + ...options, + }; - let Menu; + if (Info.name === '..' && names.length === 1) + return Dialog.alert.noFiles(TITLE); - const EditNames = exec.bind(); - const ConfigView = { - beforeClose: () => { - exec.ifExist(Menu, 'hide'); - DOM.Events.remove('keydown', keyListener); - EditNames.isChanged(); + DOM.Events.addKey(keyListener); + + CloudCmd.Edit + .getEditor() + .setValueFirst('edit-names', names) + .setMode() + .setOption('keyMap', 'default') + .disableKey(); + + CloudCmd.Edit.show(config); + + return CloudCmd.Edit; +}; + +function keyListener(event) { + const ctrl = event.ctrlKey; + const meta = event.metaKey; + const ctrlMeta = ctrl || meta; + const Key = CloudCmd.Key; + + if (!ctrlMeta || event.keyCode !== Key.S) + return; + + EditNames.hide(); +} + +function getActiveNames() { + return DOM.getFilenames(DOM.getActiveFiles()); +} + +module.exports.hide = () => { + CloudCmd.Edit.hide(); +}; + +function setListeners() { + const element = CloudCmd.Edit.getElement(); + + DOM.Events.addOnce('contextmenu', element, setMenu); +} + +function applyNames() { + const dir = Info.dirPath; + const from = getActiveNames(); + const nameIndex = from.indexOf(Info.name); + + const editor = CloudCmd.Edit.getEditor(); + const to = editor + .getValue() + .split('\n'); + + const root = CloudCmd.config('root'); + + Promise.resolve(root) + .then(rename(dir, from, to)) + .then(refresh(to, nameIndex)) + .catch(alert); +} + +function _refresh(to, nameIndex, res) { + if (res.status === 404) + return res.text().then(reject); + + const currentName = to[nameIndex]; + + CloudCmd.refresh({ + currentName + }); +} + +function getDir(root, dir) { + if (root === '/') + return dir; + + return root + dir; +} + +function _rename(path, from, to, root) { + const dir = getDir(root, path); + + return fetch(CloudCmd.PREFIX + '/rename', { + method: 'put', + credentials: 'include', + body: JSON.stringify({ + from, + to, + dir, + }) + }); +} + +function setMenu(event) { + const position = { + x: event.clientX, + y: event.clientY + }; + + event.preventDefault(); + + if (Menu) + return; + + const editor = CloudCmd.Edit.getEditor(); + const options = { + beforeShow: (params) => { + params.x -= 18; + params.y -= 27; + }, + + afterClick: () => { + editor.focus(); } }; - function init(callback) { - const editor = store(); - - const getMainEditor = () => CloudCmd.Edit.getEditor(); - const getEditor = squad(editor, getMainEditor); - const listeners = squad(setListeners, editor); - - const show = callback ? exec : EditNames.show; - - exec.series([ - CloudCmd.Edit, - call(getEditor), - call(listeners), - show, - ], callback); - } - - EditNames.show = (options) => { - const names = getActiveNames().join('\n'); - const config = { - ...ConfigView, - ...options, - }; - - if (Info.name === '..' && names.length === 1) - return Dialog.alert.noFiles(TITLE); - - DOM.Events.addKey(keyListener); - - CloudCmd.Edit - .getEditor() - .setValueFirst('edit-names', names) - .setMode() - .setOption('keyMap', 'default') - .disableKey(); - - CloudCmd.Edit.show(config); - - return CloudCmd.Edit; + const menuData = { + 'Save Ctrl+S' : () => { + editor.save(); + EditNames.hide(); + }, + 'Go To Line Ctrl+G' : () => { + editor.goToLine(); + }, + 'Cut Ctrl+X' : () => { + editor.cutToClipboard(); + }, + 'Copy Ctrl+C' : () => { + editor.copyToClipboard(); + }, + 'Paste Ctrl+V' : () => { + editor.pasteFromClipboard(); + }, + 'Delete Del' : () => { + editor.remove('right'); + }, + 'Select All Ctrl+A' : () => { + editor.selectAll(); + }, + 'Close Esc' : () => { + EditNames.hide(); + } }; - function keyListener(event) { - const ctrl = event.ctrlKey; - const meta = event.metaKey; - const ctrlMeta = ctrl || meta; - const Key = CloudCmd.Key; - - if (!ctrlMeta || event.keyCode !== Key.S) - return; - - EditNames.hide(); - } + const element = CloudCmd.Edit.getElement(); - function getActiveNames() { - return DOM.getFilenames(DOM.getActiveFiles()); - } - - EditNames.hide = () => { - CloudCmd.Edit.hide(); - }; - - function setListeners() { - const element = CloudCmd.Edit.getElement(); - - DOM.Events.addOnce('contextmenu', element, setMenu); - } - - function applyNames() { - const dir = Info.dirPath; - const from = getActiveNames(); - const nameIndex = from.indexOf(Info.name); - - const editor = CloudCmd.Edit.getEditor(); - const to = editor - .getValue() - .split('\n'); - - const root = CloudCmd.config('root'); - - Promise.resolve(root) - .then(rename(dir, from, to)) - .then(refresh(to, nameIndex)) - .catch(alert); - } - - function _refresh(to, nameIndex, res) { - if (res.status === 404) - return res.text().then(reject); - - const currentName = to[nameIndex]; - - CloudCmd.refresh({ - currentName - }); - } - - function getDir(root, dir) { - if (root === '/') - return dir; - - return root + dir; - } - - function _rename(path, from, to, root) { - const dir = getDir(root, path); - - return fetch(CloudCmd.PREFIX + '/rename', { - method: 'put', - credentials: 'include', - body: JSON.stringify({ - from, - to, - dir, - }) - }); - } - - function setMenu(event) { - const position = { - x: event.clientX, - y: event.clientY - }; - - event.preventDefault(); - - if (Menu) - return; - - const editor = CloudCmd.Edit.getEditor(); - const options = { - beforeShow: (params) => { - params.x -= 18; - params.y -= 27; - }, - - afterClick: () => { - editor.focus(); - } - }; - - const menuData = { - 'Save Ctrl+S' : () => { - editor.save(); - EditNames.hide(); - }, - 'Go To Line Ctrl+G' : () => { - editor.goToLine(); - }, - 'Cut Ctrl+X' : () => { - editor.cutToClipboard(); - }, - 'Copy Ctrl+C' : () => { - editor.copyToClipboard(); - }, - 'Paste Ctrl+V' : () => { - editor.pasteFromClipboard(); - }, - 'Delete Del' : () => { - editor.remove('right'); - }, - 'Select All Ctrl+A' : () => { - editor.selectAll(); - }, - 'Close Esc' : () => { - EditNames.hide(); - } - }; - - const element = CloudCmd.Edit.getElement(); - - Menu = supermenu(element, options, menuData); - Menu.show(position.x, position.y); - } - - EditNames.isChanged = () => { - const editor = CloudCmd.Edit.getEditor(); - const msg = 'Apply new names?'; - - if (!editor.isChanged()) - return; - - Dialog.confirm(TITLE, msg, {cancel: false}) - .then(applyNames); - }; - - setTimeout(wraptile(init, callback)); - - return EditNames; -}; + Menu = supermenu(element, options, menuData); + Menu.show(position.x, position.y); +} + +module.exports.isChanged = isChanged; + +function isChanged() { + const editor = CloudCmd.Edit.getEditor(); + const msg = 'Apply new names?'; + + if (!editor.isChanged()) + return; + + Dialog.confirm(TITLE, msg, {cancel: false}) + .then(applyNames); +} diff --git a/client/modules/edit.js b/client/modules/edit.js index a7e56ac1..95c57fb9 100644 --- a/client/modules/edit.js +++ b/client/modules/edit.js @@ -10,6 +10,9 @@ const {MAX_FILE_SIZE: maxSize} = require('../../common/cloudfunc'); const {time, timeEnd} = require('../../common/util'); const Name = 'Edit'; + +CloudCmd[Name] = exports; + const EditorName = CloudCmd.config('editor'); let Loading = true; diff --git a/client/modules/help.js b/client/modules/help.js index d783985e..5e859115 100644 --- a/client/modules/help.js +++ b/client/modules/help.js @@ -2,6 +2,8 @@ /* global CloudCmd */ +CloudCmd.Help = exports; + const Images = require('../dom/images'); module.exports.init = () => { diff --git a/client/modules/konsole.js b/client/modules/konsole.js index 0d34a316..60b44d9a 100644 --- a/client/modules/konsole.js +++ b/client/modules/konsole.js @@ -5,6 +5,8 @@ /* global DOM */ /* global Console */ +CloudCmd.Konsole = exports; + const exec = require('execon'); const {promisify} = require('es6-promisify'); const currify = require('currify/legacy'); diff --git a/client/modules/markdown.js b/client/modules/markdown.js index 14f65a45..40968f58 100644 --- a/client/modules/markdown.js +++ b/client/modules/markdown.js @@ -1,6 +1,8 @@ 'use strict'; -/*global CloudCmd */ +/* global CloudCmd */ + +CloudCmd.Markdown = exports; const Images = require('../dom/images'); const load = require('../dom/load'); diff --git a/client/modules/menu.js b/client/modules/menu.js index d6f46c57..5dd21437 100644 --- a/client/modules/menu.js +++ b/client/modules/menu.js @@ -27,6 +27,8 @@ let MenuContextFile; module.exports.ENABLED = false; +CloudCmd.Menu = exports; + module.exports.init = () => { const {isAuth, menuDataFile} = getFileMenuData(); diff --git a/client/modules/operation/index.js b/client/modules/operation/index.js index 4aa315d8..2495804f 100644 --- a/client/modules/operation/index.js +++ b/client/modules/operation/index.js @@ -5,7 +5,6 @@ 'use strict'; - const currify = require('currify/legacy'); const wraptile = require('wraptile/legacy'); const {promisify} = require('es6-promisify'); @@ -23,6 +22,8 @@ const getNextCurrentName = require('./get-next-current-name'); const removeQuery = (a) => a.replace(/\?.*/, ''); const Name = 'Operation'; +CloudCmd[Name] = exports; + const { TITLE, config, diff --git a/client/modules/terminal.js b/client/modules/terminal.js index 131a94f4..7cd79d3c 100644 --- a/client/modules/terminal.js +++ b/client/modules/terminal.js @@ -16,6 +16,8 @@ const TITLE = 'Terminal'; const {Dialog} = DOM; const {Key} = CloudCmd; +CloudCmd.Terminal = exports; + let Element; let Loaded; let Terminal; diff --git a/client/modules/upload.js b/client/modules/upload.js index 6daab2e8..1d7e083d 100644 --- a/client/modules/upload.js +++ b/client/modules/upload.js @@ -2,6 +2,8 @@ 'use strict'; +CloudCmd.Upload = exports; + const load = require('../dom/load'); const Files = require('../dom/files'); const Images = require('../dom/images'); diff --git a/client/modules/view.js b/client/modules/view.js index 571a4f3f..3787be3a 100644 --- a/client/modules/view.js +++ b/client/modules/view.js @@ -24,13 +24,14 @@ const lifo = currify((fn, el, cb, name) => fn(name, el, cb)); const addEvent = lifo(Events.add); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); -//module.exports = exec.bind(); module.exports.show = show; module.exports.hide = hide; let Loading = false; const Name = 'View'; +CloudCmd[Name] = module.exports; + const Info = DOM.CurrentInfo; const Key = CloudCmd.Key; const basename = (a) => a.split('/').pop(); diff --git a/client/sw/register.spec.js b/client/sw/register.spec.js index 9a83d341..f86c0869 100644 --- a/client/sw/register.spec.js +++ b/client/sw/register.spec.js @@ -99,12 +99,12 @@ test('sw: register: registerSW', async (t) => { registerSW, } = mock.reRequire('./register'); - await registerSW(); + await registerSW('hello'); global.location = location; global.navigator = navigator; - t.ok(register.calledWith(), 'should call register'); + t.ok(register.calledWith('/hello/sw.js'), 'should call register'); t.end(); }); diff --git a/client/sw/sw.js b/client/sw/sw.js index f7dc23c1..1b1ca80c 100644 --- a/client/sw/sw.js +++ b/client/sw/sw.js @@ -1,18 +1,16 @@ 'use strict'; -const preval = require('preval.macro'); +const codegen = require('codegen.macro'); const tryToCatch = require('try-to-catch/legacy'); const currify = require('currify/legacy'); const isDev = process.env.NODE_ENV === 'development'; -const tryWrap = (a) => (...b) => tryToCatch(a, ...b); - const wait = currify((f, e) => e.waitUntil(f())); const respondWith = currify((f, e) => e.respondWith(f(e))); const getPathName = (url) => new URL(url).pathname; -const date = preval`module.exports = Date()`; +const date = codegen`module.exports = '"' + Date() + '"'`; const NAME = `cloudcmd: ${date}`; const isGet = (a) => a.method === 'GET'; @@ -69,7 +67,7 @@ async function onFetch(event) { if (!isDev && response) return response; - const [e, resp] = await tryToRequest(request); + const [e, resp] = await tryToCatch(fetch, request.clone()); if (e) return console.error(e, response, pathname); @@ -96,28 +94,3 @@ async function addToCache(request, response) { return cache.put(request, response); } -async function tryToRequest(req) { - const {url} = req; - const path = url.replace(`${location.origin}/`, ''); - const get = tryWrap(fetch); - const prefix = getPrefix(); - - if (prefix && /^dist/.test(path)) - return await get(createRequest(`${prefix}${path}`)); - - return await get(req.clone()); -} - -function getPrefix() { - const { - href, - origin, - } = location; - - const prefix = href - .replace(origin, '') - .replace('sw.js', ''); - - return prefix; -} - diff --git a/package.json b/package.json index 3556bd04..53861ec3 100644 --- a/package.json +++ b/package.json @@ -167,6 +167,7 @@ "babel-plugin-macros": "^2.2.1", "clean-css-loader": "^1.0.1", "clear-module": "^2.1.0", + "codegen.macro": "^2.0.0", "coveralls": "^3.0.0", "css-loader": "^0.28.4", "domtokenlist-shim": "^1.2.0", @@ -193,7 +194,6 @@ "nyc": "^12.0.1", "philip": "^2.0.0", "place": "^1.1.4", - "preval.macro": "^1.0.2", "readjson": "^1.1.3", "redrun": "^6.0.0", "request": "^2.76.0",