Skip to content

Commit 860161b

Browse files
authored
Fix the rust crypto import in esm environments. (#4445)
* Configure babel to fix the rust import in esm environments. * Add lockfile changes. * Cleanup rust-crypto import and babel config.
1 parent 0c9d82e commit 860161b

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

babel.config.cjs

+17
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,22 @@ module.exports = {
3131
"@babel/plugin-transform-object-rest-spread",
3232
"@babel/plugin-syntax-dynamic-import",
3333
"@babel/plugin-transform-runtime",
34+
[
35+
"search-and-replace",
36+
{
37+
// Since rewriteImportExtensions doesn't work on dynamic imports (yet), we need to manually replace
38+
// the dynamic rust-crypto import.
39+
// (see https://github.com/babel/babel/issues/16750)
40+
rules:
41+
process.env.NODE_ENV !== "test"
42+
? [
43+
{
44+
search: "./rust-crypto/index.ts",
45+
replace: "./rust-crypto/index.js",
46+
},
47+
]
48+
: [],
49+
},
50+
],
3451
],
3552
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"@typescript-eslint/eslint-plugin": "^7.0.0",
9292
"@typescript-eslint/parser": "^7.0.0",
9393
"babel-jest": "^29.0.0",
94+
"babel-plugin-search-and-replace": "^1.1.1",
9495
"debug": "^4.3.4",
9596
"eslint": "8.57.0",
9697
"eslint-config-google": "^0.14.0",

src/client.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2254,9 +2254,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
22542254
// importing rust-crypto will download the webassembly, so we delay it until we know it will be
22552255
// needed.
22562256
this.logger.debug("Downloading Rust crypto library");
2257-
// blocked on https://github.com/matrix-org/matrix-js-sdk/issues/4392 / https://github.com/babel/babel/issues/16750
2258-
// eslint-disable-next-line node/file-extension-in-import
2259-
const RustCrypto = await import("./rust-crypto");
2257+
const RustCrypto = await import("./rust-crypto/index.ts");
22602258

22612259
const rustCrypto = await RustCrypto.initRustCrypto({
22622260
logger: this.logger,

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,11 @@ babel-plugin-polyfill-regenerator@^0.6.1:
22782278
dependencies:
22792279
"@babel/helper-define-polyfill-provider" "^0.6.2"
22802280

2281+
babel-plugin-search-and-replace@^1.1.1:
2282+
version "1.1.1"
2283+
resolved "https://registry.yarnpkg.com/babel-plugin-search-and-replace/-/babel-plugin-search-and-replace-1.1.1.tgz#2e5b4488e41d9eba1c220651b1a9b350fdf10915"
2284+
integrity sha512-fjP2VTF3mxxOUnc96mdK22llH92A6gu7A5AFapJmgnqsQi3bqLduIRP0FpA2r5vRZOYPpnX4rE5izQlpsMBjSA==
2285+
22812286
babel-preset-current-node-syntax@^1.0.0:
22822287
version "1.0.1"
22832288
resolved "https://registry.yarnpkg.com/babel-preset-current-node-syntax/-/babel-preset-current-node-syntax-1.0.1.tgz#b4399239b89b2a011f9ddbe3e4f401fc40cff73b"

0 commit comments

Comments
 (0)