From bc0ea20343667ce0281cc3c609c13cb9cbf928a6 Mon Sep 17 00:00:00 2001 From: Valere Fedronic Date: Fri, 4 Apr 2025 23:24:42 +0200 Subject: [PATCH] Default vite config to support using local build of crypto wasm (#3038) * Devex: Default vite config to support using local build of crypto wasm * use realpathSync and make it work with linked and not-linked We need to use sync because an async defineConfig fails for the embedded exports. * also allow just linking matrix-sdk-crypto-wasm (without js-sdk linking) --------- Co-authored-by: Timo Co-authored-by: Timo <16718859+toger5@users.noreply.github.com> Co-authored-by: Robin --- src/utils/matrix.ts | 2 +- vite.config.js | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/utils/matrix.ts b/src/utils/matrix.ts index a2530ecf..0a2b5c1a 100644 --- a/src/utils/matrix.ts +++ b/src/utils/matrix.ts @@ -337,7 +337,7 @@ export function getRelativeRoomUrl( } /** - * Perfom a network operation with retries on ConnectionError. + * Perform a network operation with retries on ConnectionError. * If the error is not retryable, or the max number of retries is reached, the error is rethrown. * Supports handling of matrix quotas. */ diff --git a/vite.config.js b/vite.config.js index 5aaee3ab..590f3c16 100644 --- a/vite.config.js +++ b/vite.config.js @@ -5,13 +5,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig, loadEnv, searchForWorkspaceRoot } from "vite"; import svgrPlugin from "vite-plugin-svgr"; import { createHtmlPlugin } from "vite-plugin-html"; import { codecovVitePlugin } from "@codecov/vite-plugin"; import { sentryVitePlugin } from "@sentry/vite-plugin"; import react from "@vitejs/plugin-react"; import basicSsl from "@vitejs/plugin-basic-ssl"; +import { realpathSync } from "fs"; // https://vitejs.dev/config/ export default defineConfig(({ mode, packageType }) => { @@ -64,9 +65,25 @@ export default defineConfig(({ mode, packageType }) => { ); } + // The crypto WASM module is imported dynamically. Since it's common + // for developers to use a linked copy of matrix-js-sdk or Rust + // crypto (which could reside anywhere on their file system), Vite + // needs to be told to recognize it as a legitimate file access. + const allow = [searchForWorkspaceRoot(process.cwd())]; + for (const path of [ + "node_modules/matrix-js-sdk/node_modules/@matrix-org/matrix-sdk-crypto-wasm", + "node_modules/@matrix-org/matrix-sdk-crypto-wasm", + ]) { + try { + allow.push(realpathSync(path)); + } catch {} + } + console.log("Allowed vite paths:", allow); + return { server: { port: 3000, + fs: { allow }, }, build: { sourcemap: true,