From 1f34d2d6440ba8a8b025459e75b7079ec358b2af Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 26 Sep 2018 16:44:52 +0100 Subject: [PATCH] Kill off olm-loader Now that Olm needs to be inited asynchronously anyway, we can just pass the options to Olm.init(), and as long as we do that before we start the js-sdk, we're all good. This will means the olm js is now part of the main bundle but since it's now just a wrapper around the wasm, this is probably faster. Also add the directwatch flag to olm.wasm because otherwise it doesn't seem to copy the file in watch mode... --- scripts/copy-res.js | 2 +- src/vector/index.js | 16 ++++++++++++++++ src/vector/olm-loader.js | 39 --------------------------------------- webpack.config.js | 18 ------------------ 4 files changed, 17 insertions(+), 58 deletions(-) delete mode 100644 src/vector/olm-loader.js diff --git a/scripts/copy-res.js b/scripts/copy-res.js index 43b77688..dc55cb1a 100755 --- a/scripts/copy-res.js +++ b/scripts/copy-res.js @@ -60,7 +60,7 @@ const COPY_LIST = [ // XXX: This is tied quite heavily to the matching olm.js so it really should be // in the bundle dir with the js to avoid caching issues giving us wasm that // doesn't match our js, but I cannot find any way to get webpack to do this. - ["node_modules/olm/olm.wasm", "webapp"], + ["node_modules/olm/olm.wasm", "webapp", { directwatch: 1 }], ["./config.json", "webapp", { directwatch: 1 }], ]; diff --git a/src/vector/index.js b/src/vector/index.js index ff6b1285..e9452f65 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -54,6 +54,8 @@ import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/Setting import Tinter from 'matrix-react-sdk/lib/Tinter'; import SdkConfig from "matrix-react-sdk/lib/SdkConfig"; +import Olm from 'olm'; + import rageshake from "matrix-react-sdk/lib/rageshake/rageshake"; import CallHandler from 'matrix-react-sdk/lib/CallHandler'; @@ -227,6 +229,20 @@ async function loadApp() { window.addEventListener('hashchange', onHashChange); + /* Start loading Olm. Note that we *don't* wait for this to load: the + * js-sdk will also call this and actually wait on the promise before it + * tries to use the library. It can be loading its wasm while the rest of + * the app loads though. + * + * We also need to tell the Olm js to look for its wasm file at the same + * level as index.html. It really should be in the same place as the js, + * ie. in the bundle directory, to avoid caching issues, but as far as I + * can tell this is completely impossible with webpack. + */ + Olm.init({ + locateFile: () => 'olm.wasm', + }); + await loadLanguage(); const fragparts = parseQsFromFragment(window.location); diff --git a/src/vector/olm-loader.js b/src/vector/olm-loader.js deleted file mode 100644 index 29c83653..00000000 --- a/src/vector/olm-loader.js +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright 2016 OpenMarket Ltd -Copyright 2018 New Vector Ltd - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -import olm from 'olm/olm.js'; - -/* a very thin shim for loading olm.js: just sets the global OLM_OPTIONS and - * requires the actual olm.js library. - * - * olm.js reads global.OLM_OPTIONS and defines global.Olm. The latter is fine for us, - * but we need to prepare the former. - * - * We can't use webpack's definePlugin to do this, because we tell webpack not - * to parse olm.js. We also can't put this code in index.js, because olm and - * index.js are loaded in parallel, and we need to make sure OLM_OPTIONS is set - * before olm.js is loaded. - */ - -/* Tell the Olm js to look for its wasm file at the same level as index.html. - * It really should be in the same place as the js, ie. in the bundle directory, - * to avoid caching issues, but as far as I can tell this is completely impossible - * with webpack. - */ -global.OLM_OPTIONS = { - locateFile: () => 'olm.wasm', -}; diff --git a/webpack.config.js b/webpack.config.js index 122facb2..e8f20d29 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,15 +15,6 @@ module.exports = { "mobileguide": "./src/vector/mobile_guide/index.js", - // We ship olm.js as a separate lump of javascript. This makes it get - // loaded via a separate