From 0ca2a45358b1fc66c1dc7ef4443049b7c8240d52 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Sun, 3 May 2020 17:25:02 +0200 Subject: [PATCH] Put webpack in charge of language file copying and loading And HTTP in charge of caching. --- app/cache.js | 126 ---------------------------------------------- app/file.js | 40 --------------- app/loc.js | 39 +------------- tools/build | 8 --- webpack.config.js | 1 + 5 files changed, 3 insertions(+), 211 deletions(-) delete mode 100644 app/cache.js delete mode 100644 app/file.js delete mode 100755 tools/build diff --git a/app/cache.js b/app/cache.js deleted file mode 100644 index c50e1bf..0000000 --- a/app/cache.js +++ /dev/null @@ -1,126 +0,0 @@ - -export /*abstract */class Cache { - /** - * @param {string} key - * @return {boolean} - * @author svartoyg - */ - /*protected */has(key) { - throw (new Error('not implemented')); - } - - /** - * @param {string} key - * @return {any} - * @author svartoyg - */ - /*protected */fetch(key) { - throw (new Error('not implemented')); - } - - /** - * @param {string} key - * @param {any} value - * @author svartoyg - */ - /*protected */store(key, value) { - throw (new Error('not implemented')); - } - - /** - * @param {string} key - * @param {()=>Promise} retrieve - * @return {Promise} - * @author svartoyg - */ - /*public */async get(key, retrieve) { - if (this.has(key)) { - const value = this.fetch(key); - return Promise.resolve(value); - } else { - const value = await retrieve(); - this.store(key, value); - return Promise.resolve(value); - } - } -} - - -/** - * @author svartoyg - */ -class CacheNone extends Cache { - /** - * @author svartoyg - */ - /*public */constructor() { - super(); - } - - /** - * @author svartoyg - */ - /*protected */has(key) { - return false; - } - - /** - * @author svartoyg - */ - /*protected */fetch(key) { - throw (new Error('not possible')); - } - - /** - * @author svartoyg - */ - /*protected */store(key, value) { - } -} - - -/** - * @author svartoyg - */ -export class CacheLocalstorage extends Cache { - /** - * @param {string} [corner] for separating the cache instance from others - * @author svartoyg - */ - /*public */constructor(corner = null) { - super(); - this.corner = corner; - } - - /** - * @author svartoyg - */ - /*private */augmentKey(key) { - return ((this.corner === null) ? key : (this.corner + '/' + key)); - } - - /** - * @author svartoyg - */ - /*protected */has(key) { - return (window.localStorage.getItem(this.augmentKey(key)) !== null); - } - - /** - * @author svartoyg - */ - /*protected */fetch(key) { - const valueRaw = window.localStorage.getItem(this.augmentKey(key)); - const value = JSON.parse(valueRaw); - return value; - } - - /** - * @author svartoyg - */ - /*protected */store(key, value) { - const valueRaw = JSON.stringify(value); - window.localStorage.setItem(this.augmentKey(key), valueRaw); - } -} - diff --git a/app/file.js b/app/file.js deleted file mode 100644 index 07c8665..0000000 --- a/app/file.js +++ /dev/null @@ -1,40 +0,0 @@ - -/** - * @param {string} path - * @return Promise - * @todo use Util.fetch instead? - * @author svartoyg - */ -export async function read (path) { - return ( - new Promise( - (resolve, reject) => { - let request = new XMLHttpRequest(); - request.open('GET', '/' + path, true); - request.onreadystatechange = () => { - switch (request.readyState) { - case XMLHttpRequest.DONE: { - switch (request.status) { - case 0: { - reject(new Error('XMLHttpRequest failed')); - break; - } - default: { - resolve(request.responseText); - break; - } - } - break; - } - default: { - console.warn('unhandled readyState "' + request.readyState + '"'); - break; - } - } - }; - request.send(null); - } - ) - ); -} - diff --git a/app/loc.js b/app/loc.js index 0c2d9d7..a1a6015 100644 --- a/app/loc.js +++ b/app/loc.js @@ -1,17 +1,3 @@ -import {CacheLocalstorage} from './cache'; -import {read as fileRead} from './file'; -// import {Util} from 'util'; - - -/** - * the relative path to the directory containing the JSON localization files - * - * @var {string} - * @author svartoyg - */ -var _directory = 'loc'; - - /** * the default language to use * @@ -30,13 +16,6 @@ var _languageDefault = null; var _languageFallback = null; -/** - * @var {Cache} - * @author svartoyg - */ -var _cache = null; - - /** * two level map with ISO-639-1 code as first key and translation id as second key * @@ -56,20 +35,7 @@ async function retrieveData (language) { if (regexp.exec(language) === null) { return Promise.reject(new Error('invalid language code "' + language + '"')); } else { - const path = (_directory + '/' + language + '.json'); - let content; - try { - content = await fileRead(path); - } catch (exception) { - return Promise.reject(new Error('could not load localization data for language "' + language + '": ' + error.toString())); - } - let data; - try { - data = JSON.parse(content); - } catch (exception) { - return Promise.reject(new Error('invalid JSON localization data for language "' + language + '": ' + exception.toString())); - } - return Promise.resolve(data); + return (await import(`../loc/${language}.json`)).default } } @@ -80,7 +46,6 @@ async function retrieveData (language) { * @author svartoyg */ export async function initialize (languageDefault, languageFallback = 'en') { - _cache = new CacheLocalstorage('loc'); _languageFallback = languageFallback; _languageDefault = languageDefault; for (const language of [_languageFallback, _languageDefault]) { @@ -88,7 +53,7 @@ export async function initialize (languageDefault, languageFallback = 'en') { console.log('--', 'loading localization data for language "' + language + '" ...'); let data; try { - data = await _cache.get(language, () => retrieveData(language)); + data = await retrieveData(language); } catch (exception) { console.warn(exception.toString()); } diff --git a/tools/build b/tools/build deleted file mode 100755 index 0430b43..0000000 --- a/tools/build +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env sh - -npm run build - -## loc -mkdir -p dist/loc -cp -ruv loc/* dist/loc/ - diff --git a/webpack.config.js b/webpack.config.js index 1e47676..17c9000 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,6 +15,7 @@ module.exports = { devtool: "cheap-source-map", output: { path: path.join(__dirname, 'dist'), + chunkFilename: '[chunkhash].js', filename: '[name].js' }, module: {