commit 2e91bf53cea553a6db5eb9ef46109388923fe849
parent ac0f6cec7b8fe5ba1e7c831c876bebea3b558912
Author: ms <ms@taler.net>
Date: Thu, 3 Feb 2022 10:54:15 +0100
mocking 'window' to development server
Diffstat:
7 files changed, 154 insertions(+), 2 deletions(-)
diff --git a/packages/bank/mocks/json-server/db.json b/packages/bank/mocks/json-server/db.json
@@ -0,0 +1,6 @@
+{
+ "register": {
+ "{\"hey\": \"you\"}": ""
+ },
+ "transactions": {}
+}
+\ No newline at end of file
diff --git a/packages/bank/mocks/json-server/routes.json b/packages/bank/mocks/json-server/routes.json
@@ -0,0 +1,4 @@
+{
+ "/access-api/testing/register": "/register",
+ "/access-api/accounts/:accountName/transactions\\?page=:pageNumber": "/transactions"
+}
diff --git a/packages/bank/mocks/window.js b/packages/bank/mocks/window.js
@@ -0,0 +1,24 @@
+Object.defineProperty(window, 'localStorage', {
+ value: {
+ store: {},
+ getItem: function(key) {
+ return this.store[key];
+ },
+ setItem: function(key, value) {
+ return this.store[key] = value;
+ },
+ clear: function() {
+ this.store = {};
+ }
+ }
+});
+
+Object.defineProperty(window, 'location', {
+ value: {
+ origin: "http://localhost:5000",
+ search: "",
+ pathname: "/demobanks/default",
+ }
+})
+
+export default window;
diff --git a/packages/bank/package.json b/packages/bank/package.json
@@ -4,8 +4,8 @@
"version": "0.1.0",
"license": "MIT",
"scripts": {
- "dev": "preact watch --port ${PORT:=8080} --no-sw --no-esm",
- "build": "preact build --no-sw --no-esm -c preact.single-config.js --dest single && sh remove-link-stylesheet.sh",
+ "dev": "preact watch --port ${PORT:=8080} --no-sw --no-esm -c preact.mock.js",
+ "build": "preact build --no-sw --no-esm -c preact.single-config.js --dest build && sh remove-link-stylesheet.sh",
"serve": "sirv build --port ${PORT:=8080} --cors --single",
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
"test": "jest ./tests",
diff --git a/packages/bank/preact.mock.js b/packages/bank/preact.mock.js
@@ -0,0 +1,47 @@
+/*
+ This file is part of GNU Taler
+ (C) 2021 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+/**
+*
+* @author Sebastian Javier Marchano (sebasjm)
+*/
+
+import { DefinePlugin, ProvidePlugin } from 'webpack';
+
+import pack from './package.json';
+import * as cp from 'child_process';
+
+const commitHash = cp.execSync('git rev-parse --short HEAD').toString();
+import path from 'path';
+
+export default {
+ webpack(config, env, helpers) {
+ // Ensure that process.env will not be undefined at runtime.
+ config.node.process = 'mock'
+
+ // Add __VERSION__ to be use in the html.
+ config.plugins.push(
+ new DefinePlugin({
+ 'process.env.__VERSION__': JSON.stringify(env.isProd ? pack.version : `dev-${commitHash}`) ,
+ }),
+ // 'window' gets mocked to point at a running euFin instance.
+ new ProvidePlugin({window: path.resolve("mocks/window")})
+ );
+
+ let { index } = helpers.getPluginsByName(config, 'WebpackFixStyleOnlyEntriesPlugin')[0]
+ config.plugins.splice(index, 1)
+ }
+}
diff --git a/packages/bank/preact.single-config.js b/packages/bank/preact.single-config.js
@@ -0,0 +1,62 @@
+/*
+ This file is part of GNU Taler
+ (C) 2021 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+/**
+*
+* @author Sebastian Javier Marchano (sebasjm)
+*/
+
+import defaultConfig from './preact.config'
+
+export default {
+ webpack(config, env, helpers, options) {
+ defaultConfig.webpack(config, env, helpers, options)
+
+ //1. check no file is under /routers or /component/{routers,async} to prevent async components
+ // https://github.com/preactjs/preact-cli#route-based-code-splitting
+
+ //2. remove devtools to prevent sourcemaps
+ config.devtool = false
+
+ //3. change assetLoader to load assets inline
+ const loaders = helpers.getLoaders(config)
+ const assetsLoader = loaders.find(lo => lo.rule.test.test('something.woff'))
+ if (assetsLoader) {
+ assetsLoader.rule.use = 'base64-inline-loader'
+ assetsLoader.rule.loader = undefined
+ }
+
+ //4. remove critters
+ //critters remove the css bundle from htmlWebpackPlugin.files.css
+ //for now, pushing all the content into the html is enough
+ const crittersWrapper = helpers.getPluginsByName(config, 'Critters')
+ if (crittersWrapper && crittersWrapper.length > 0) {
+ const [{ index }] = crittersWrapper
+ config.plugins.splice(index, 1)
+ }
+
+ //5. remove favicon from src/assets
+
+ //6. remove performance hints since we now that this is going to be big
+ if (config.performance) {
+ config.performance.hints = false
+ }
+
+ //7. template.html should have a favicon and add js/css content
+
+ //last, after building remove the mysterious link to stylesheet with remove-link-stylesheet.sh
+ }
+}
diff --git a/packages/bank/remove-link-stylesheet.sh b/packages/bank/remove-link-stylesheet.sh
@@ -0,0 +1,8 @@
+# This script has been placed in the public domain.
+
+FILE=$(ls build/bundle.*.css)
+BUNDLE=${FILE#build}
+grep -q '<link href="'$BUNDLE'" rel="stylesheet">' build/index.html || { echo bundle $BUNDLE not found in index.html; exit 1; }
+echo -n Removing link from index.html ...
+sed 's_<link href="'$BUNDLE'" rel="stylesheet">__' -i build/index.html
+echo done