From b6fda8865faf3e1857f6eca18d82176af224da18 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 5 Oct 2021 17:51:10 +0200 Subject: [PATCH] make all dependencies use vite and remove post-install script / lib dir --- package.json | 5 +- scripts/post-install.js | 132 ------------------ .../session/room/timeline/deserialize.js | 2 +- src/matrix/e2ee/Account.js | 2 +- src/matrix/e2ee/common.js | 2 +- src/mocks/Storage.ts | 3 +- src/platform/web/LegacyPlatform.js | 2 +- src/platform/web/dom/Crypto.js | 2 +- src/platform/web/legacy-polyfill.js | 2 +- src/platform/web/utils/Base58.js | 2 +- src/platform/web/utils/Base64.js | 2 +- src/platform/web/worker/polyfill.js | 2 +- vite.config.js | 6 + 13 files changed, 19 insertions(+), 145 deletions(-) delete mode 100644 scripts/post-install.js diff --git a/package.json b/package.json index 562b6c7b..edf2e673 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,9 @@ "lint": "eslint --cache src/", "lint-ts": "eslint src/ -c .ts-eslintrc.js --ext .ts", "lint-ci": "eslint src/", - "test": "impunity --entry-point src/platform/web/main.js --force-esm-dirs lib/ src/", + "test": "impunity --entry-point src/platform/web/main.js --force-esm-dirs src/", "start": "snowpack dev --port 3000", - "build": "node --experimental-modules scripts/build.mjs", - "postinstall": "node ./scripts/post-install.js" + "build": "node --experimental-modules scripts/build.mjs" }, "repository": { "type": "git", diff --git a/scripts/post-install.js b/scripts/post-install.js deleted file mode 100644 index 39ccf06a..00000000 --- a/scripts/post-install.js +++ /dev/null @@ -1,132 +0,0 @@ -/* -Copyright 2020 The Matrix.org Foundation C.I.C. - -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. -*/ - -const fsRoot = require("fs"); -const fs = fsRoot.promises; -const path = require("path"); -const { rollup } = require('rollup'); -const { fileURLToPath } = require('url'); -const { dirname } = require('path'); -// needed to translate commonjs modules to esm -const commonjs = require('@rollup/plugin-commonjs'); -const json = require('@rollup/plugin-json'); -const { nodeResolve } = require('@rollup/plugin-node-resolve'); - -const projectDir = path.join(__dirname, "../"); - -async function removeDirIfExists(targetDir) { - try { - await fs.rmdir(targetDir, {recursive: true}); - } catch (err) { - if (err.code !== "ENOENT") { - throw err; - } - } -} - -/** function used to resolve common-js require calls below. */ -function packageIterator(request, start, defaultIterator) { - // this is just working for bs58, would need to tune it further for other dependencies - if (request === "safe-buffer") { - return [path.join(projectDir, "/scripts/package-overrides/safe-buffer")]; - } else if (request === "buffer/") { - return [path.join(projectDir, "/scripts/package-overrides/buffer")]; - } else { - return defaultIterator(); - } -} - -async function commonjsToESM(src, dst) { - // create js bundle - const bundle = await rollup({ - treeshake: {moduleSideEffects: false}, - input: src, - plugins: [commonjs(), json(), nodeResolve({ - browser: true, - preferBuiltins: false, - customResolveOptions: {packageIterator} - })] - }); - const {output} = await bundle.generate({ - format: 'es' - }); - const code = output[0].code; - await fs.writeFile(dst, code, "utf8"); -} - -async function populateLib() { - const libDir = path.join(projectDir, "lib/"); - await removeDirIfExists(libDir); - await fs.mkdir(libDir); - const olmSrcDir = path.dirname(require.resolve("@matrix-org/olm")); - const olmDstDir = path.join(libDir, "olm/"); - await fs.mkdir(olmDstDir); - for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) { - await fs.copyFile(path.join(olmSrcDir, file), path.join(olmDstDir, file)); - } - // transpile node-html-parser to esm - await fs.mkdir(path.join(libDir, "node-html-parser/")); - await commonjsToESM( - require.resolve('node-html-parser/dist/index.js'), - path.join(libDir, "node-html-parser/index.js") - ); - // transpile another-json to esm - await fs.mkdir(path.join(libDir, "another-json/")); - await commonjsToESM( - require.resolve('another-json/another-json.js'), - path.join(libDir, "another-json/index.js") - ); - // transpile bs58 to esm - await fs.mkdir(path.join(libDir, "bs58/")); - await commonjsToESM( - require.resolve('bs58/index.js'), - path.join(libDir, "bs58/index.js") - ); - // transpile base64-arraybuffer to esm - await fs.mkdir(path.join(libDir, "base64-arraybuffer/")); - await commonjsToESM( - require.resolve('base64-arraybuffer/lib/base64-arraybuffer.js'), - path.join(libDir, "base64-arraybuffer/index.js") - ); - // this probably should no go in here, we can just import "aes-js" from legacy-extras.js - // as that file is never loaded from a browser - - // transpile aesjs to esm - await fs.mkdir(path.join(libDir, "aes-js/")); - await commonjsToESM( - require.resolve('aes-js/index.js'), - path.join(libDir, "aes-js/index.js") - ); - // es6-promise is already written as an es module, - // but it does need to be babelified, and current we don't babelify - // anything in node_modules in the build script, so make a bundle that - // is conveniently not placed in node_modules rather than symlinking. - await fs.mkdir(path.join(libDir, "es6-promise/")); - await commonjsToESM( - require.resolve('es6-promise/lib/es6-promise/promise.js'), - path.join(libDir, "es6-promise/index.js") - ); - // fake-indexeddb, used for tests (but unresolvable bare imports also makes the build complain) - // and might want to use it for in-memory storage too, although we probably do ts->es6 with esm - // directly rather than ts->es5->es6 as we do now. The bundle is 240K currently. - await fs.mkdir(path.join(libDir, "fake-indexeddb/")); - await commonjsToESM( - path.join(projectDir, "/scripts/package-overrides/fake-indexeddb.js"), - path.join(libDir, "fake-indexeddb/index.js") - ); -} - -populateLib(); diff --git a/src/domain/session/room/timeline/deserialize.js b/src/domain/session/room/timeline/deserialize.js index ce690732..ecddb5b7 100644 --- a/src/domain/session/room/timeline/deserialize.js +++ b/src/domain/session/room/timeline/deserialize.js @@ -352,7 +352,7 @@ export function parseHTMLBody(platform, mediaRepository, allowReplies, html) { return new MessageBody(html, parts); } -import parse from '../../../../../lib/node-html-parser/index.js'; +import parse from "node-html-parser"; export function tests() { class HTMLParseResult { diff --git a/src/matrix/e2ee/Account.js b/src/matrix/e2ee/Account.js index 13792ddc..ac651819 100644 --- a/src/matrix/e2ee/Account.js +++ b/src/matrix/e2ee/Account.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import anotherjson from "../../../lib/another-json/index.js"; +import anotherjson from "another-json"; import {SESSION_E2EE_KEY_PREFIX, OLM_ALGORITHM, MEGOLM_ALGORITHM} from "./common.js"; // use common prefix so it's easy to clear properties that are not e2ee related during session clear diff --git a/src/matrix/e2ee/common.js b/src/matrix/e2ee/common.js index fa970236..90252b15 100644 --- a/src/matrix/e2ee/common.js +++ b/src/matrix/e2ee/common.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import anotherjson from "../../../lib/another-json/index.js"; +import anotherjson from "another-json"; import {createEnum} from "../../utils/enum.js"; export const DecryptionSource = createEnum("Sync", "Timeline", "Retry"); diff --git a/src/mocks/Storage.ts b/src/mocks/Storage.ts index 5dba796a..0d8db7f0 100644 --- a/src/mocks/Storage.ts +++ b/src/mocks/Storage.ts @@ -14,7 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {FDBFactory, FDBKeyRange} from "../../lib/fake-indexeddb/index.js"; +import {FDBFactory} from "fake-indexeddb/lib/FDBFactory.js"; +import {FDBKeyRange} from "fake-indexeddb/lib/FDBKeyRange.js"; import {StorageFactory} from "../matrix/storage/idb/StorageFactory"; import {IDOMStorage} from "../matrix/storage/idb/types"; import {Storage} from "../matrix/storage/idb/Storage"; diff --git a/src/platform/web/LegacyPlatform.js b/src/platform/web/LegacyPlatform.js index e6bf7774..624600ff 100644 --- a/src/platform/web/LegacyPlatform.js +++ b/src/platform/web/LegacyPlatform.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import aesjs from "../../../lib/aes-js/index.js"; +import aesjs from "aes-js"; import {hkdf} from "../../utils/crypto/hkdf.js"; import {Platform as ModernPlatform} from "./Platform.js"; diff --git a/src/platform/web/dom/Crypto.js b/src/platform/web/dom/Crypto.js index b28897fa..441daf31 100644 --- a/src/platform/web/dom/Crypto.js +++ b/src/platform/web/dom/Crypto.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import base64 from "../../../../lib/base64-arraybuffer/index.js"; +import base64 from "base64-arraybuffer"; // turn IE11 result into promise function subtleCryptoResult(promiseOrOp, method) { diff --git a/src/platform/web/legacy-polyfill.js b/src/platform/web/legacy-polyfill.js index 45628f2c..e3dd8fb0 100644 --- a/src/platform/web/legacy-polyfill.js +++ b/src/platform/web/legacy-polyfill.js @@ -15,7 +15,7 @@ limitations under the License. */ // polyfills needed for IE11 -import Promise from "../../../lib/es6-promise/index.js"; +import Promise from "es6-promise/lib/es6-promise/promise.js"; import {checkNeedsSyncPromise} from "../../matrix/storage/idb/utils"; if (typeof window.Promise === "undefined") { diff --git a/src/platform/web/utils/Base58.js b/src/platform/web/utils/Base58.js index ebe4a969..b3f0bca7 100644 --- a/src/platform/web/utils/Base58.js +++ b/src/platform/web/utils/Base58.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import bs58 from "../../../../lib/bs58/index.js"; +import bs58 from "bs58"; export class Base58 { encode(buffer) { diff --git a/src/platform/web/utils/Base64.js b/src/platform/web/utils/Base64.js index 6c114fe6..83b5ccc5 100644 --- a/src/platform/web/utils/Base64.js +++ b/src/platform/web/utils/Base64.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import base64 from "../../../../lib/base64-arraybuffer/index.js"; +import base64 from "base64-arraybuffer"; export class Base64 { encodeUnpadded(buffer) { diff --git a/src/platform/web/worker/polyfill.js b/src/platform/web/worker/polyfill.js index d7f4b0d2..3c1f60e5 100644 --- a/src/platform/web/worker/polyfill.js +++ b/src/platform/web/worker/polyfill.js @@ -19,7 +19,7 @@ limitations under the License. // just enough to run olm, have promises and async/await // load this first just in case anything else depends on it -import Promise from "../../../../lib/es6-promise/index.js"; +import Promise from "es6-promise/lib/es6-promise/promise.js"; // not calling checkNeedsSyncPromise from here as we don't do any idb in the worker, // mainly because IE doesn't handle multiple concurrent connections well self.Promise = Promise; diff --git a/vite.config.js b/vite.config.js index d27d8a3a..450b8d40 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,5 +3,11 @@ export default { root: "src/platform/web", server: { hmr: false + }, + resolve: { + alias: { + "safe-buffer": "./scripts/package-overrides/safe-buffer/index.js", + "buffer": "./scripts/package-overrides/buffer/index.js" + } } };