Commit abe3c290 authored by Julien Enselme's avatar Julien Enselme
Browse files

feat(sw): rely on webpack to generate the ServiceWorker

parent 228a6614
/sw.js
/node_modules
/dist
/public
......
......@@ -19,7 +19,6 @@
import { inject } from "aurelia-framework";
import { Store } from "aurelia-store";
import { AssetSource } from "../services/assets";
import routes from "./routes";
import * as commonActions from "./actions/common";
import * as lobbyActions from "./actions/lobby";
......@@ -52,7 +51,6 @@ export class Layout {
};
sw.preloadBundles("game");
AssetSource.preloadAssets("game");
}
configureRouter(config, router) {
......
......@@ -17,8 +17,6 @@
* along with Arena of Titans. If not, see <http://www.gnu.org/licenses/>.
*/
import * as LogManager from "aurelia-logging";
// Animations images
import blackAssassinAnimation from "../../assets/game/animation/assassination-black.png";
import blueAssassinAnimation from "../../assets/game/animation/assassination-blue.png";
......@@ -149,8 +147,6 @@ import towerBlue from "../../assets/game/cards/trumps/tower-blue.png";
import towerRed from "../../assets/game/cards/trumps/tower-red.png";
import towerYellow from "../../assets/game/cards/trumps/tower-yellow.png";
const logger = LogManager.getLogger("AssetSource");
const animations = {
"assassination-black": blackAssassinAnimation,
"assassination-blue": blueAssassinAnimation,
......@@ -352,38 +348,6 @@ export class AssetSource {
return trumps[ImageName.forTrump(trump)];
}
static preloadAssets(kind) {
// Don't try to preload images when testing the application.
if (!window.caches) {
return;
}
// const cacheName = `${kind}Images`;
// const assetsToPreload = Object.values(assetsList).filter(assetSrc =>
// assetSrc.includes(kind),
// );
//
// logger.debug("Preloading assets:", assetsToPreload);
// this._preloadFiles(cacheName, assetsToPreload);
}
static _preloadFiles(cacheName, filesList) {
caches.open(cacheName).then(cache => cache.addAll(filesList));
caches.open(cacheName).then(cache =>
cache.keys().then(cacheContent => {
const requestAndUrls = cacheContent
.map(request => [request, request.url])
.map(([request, url]) => [request, new URL(url).pathname]);
requestAndUrls.forEach(([request, url]) => {
if (!filesList.includes(url)) {
logger.debug(`Deleting request ${request.url}`);
cache.delete(request);
}
});
}),
);
}
}
export class ImageClass {
......
/*
* Copyright (C) 2015-2020 by Last Run Contributors.
* Copyright (C) 2015-2020 by Arena of Titans Contributors.
*
* This file is part of Arena of Titans.
*
......@@ -18,7 +18,8 @@
*
*/
const bundlesList = "{{ bundlesList }}".split(",");
// This comes from serviceworker-webpack-plugin.
const allAssets = serviceWorkerOption.assets; // eslint-disable-line no-undef
function log(text) {
console.log(`[SW] ${text}`); // eslint-disable-line no-console
......@@ -58,10 +59,8 @@ self.addEventListener("message", event => {
return;
}
const cacheName = `${kind}Bundles`;
const bundlesToPreload = Object.values(bundlesList).filter(bundleSrc =>
bundleSrc.includes(kind),
);
const cacheName = `${kind}Assets`;
const bundlesToPreload = Object.values(allAssets).filter(bundleSrc => bundleSrc.includes(kind));
debug(`Preading bundles: ${bundlesToPreload}`);
addAll(cacheName, bundlesToPreload);
});
......
......@@ -127,14 +127,6 @@
"output": "dist",
"port": "8282"
},
"templates": {
"displayName": "Templates",
"id": "templates",
"sources": {
"index": "templates/index.html",
"sw": "templates/sw.js"
}
},
"transpiler": {
"displayName": "Babel",
"fileExtension": ".js",
......
......@@ -27,6 +27,7 @@ const project = require("./aurelia_project/aurelia.json");
const { AureliaPlugin, ModuleDependenciesPlugin } = require("aurelia-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const ServiceWorkerWebpackPlugin = require("serviceworker-webpack-plugin");
// config helpers:
const ensureArray = config => (config && (Array.isArray(config) ? config : [config])) || [];
......@@ -336,6 +337,10 @@ module.exports = ({ environment } = {}, { extractCss, analyze, tests, hmr, port,
piwikId: environmentToPiwikId[environment],
},
}),
new ServiceWorkerWebpackPlugin({
entry: "./app/sw.js",
minimize: isProductionLikeBuild,
}),
// ref: https://webpack.js.org/plugins/mini-css-extract-plugin/
...when(
extractCss,
......
......@@ -11106,7 +11106,7 @@ regenerator-runtime@^0.11.0:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==
 
regenerator-runtime@^0.13.3, regenerator-runtime@^0.13.4:
regenerator-runtime@^0.13.4:
version "0.13.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz#d878a1d094b4306d10b9096484b33ebd55e26697"
integrity sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==
......@@ -11766,6 +11766,13 @@ serve-static@1.14.1:
parseurl "~1.3.3"
send "0.17.1"
 
serviceworker-webpack-plugin@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/serviceworker-webpack-plugin/-/serviceworker-webpack-plugin-1.0.1.tgz#481863288487e92da01d49745336c72ef8a6136b"
integrity sha512-VgDEkZ3pA0HajsRaWtl5w6bLxAXx0Y+4dm7YeTcIxVmvC9YXvstex38HOBDuYETeDS5fUlBy/47gC0QYBrG0nw==
dependencies:
minimatch "^3.0.4"
set-blocking@^2.0.0, set-blocking@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment