Skip to content

Commit

Permalink
Fix splitbrain between .js being ESM & CJS inconsistently (#189)
Browse files Browse the repository at this point in the history
Fixes #188

no actual changes to the js/cjs/mjs files barring some ts annotations

Switches the package type to ESM as that is what wasm-pack is outputting
Convinces the wasm bindgen test to use an ESM runner
Uses explicit extensions on the CJS entrypoints

Co-authored-by: Richard van der Hoff <[email protected]>
  • Loading branch information
t3chguy and richvdh authored Jan 13, 2025
1 parent 0073b59 commit e4153ec
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
rules: {
camelcase: ["error", { properties: "never" }],
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# UNRELEASED

- Fix a problem, introduced in v12.0.0, when importing the published package as an ESM module, in which some files could be incorrectly interpreted as CommonJS, leading to syntax errors.
([#189](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/189))

# matrix-sdk-crypto-wasm v13.0.0

- Update matrix-rusk-sdk to `0.9.0`.
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

export * from "./pkg/matrix_sdk_crypto_wasm.d";
export * from "./pkg/matrix_sdk_crypto_wasm.d.ts";

/**
* Load the WebAssembly module in the background, if it has not already been loaded.
Expand Down
4 changes: 2 additions & 2 deletions node.js → node.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let initialised = false;
*
* It will throw if there is an attempt to load the module asynchronously running
*
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d", { with: { "resolution-mode": "require" } })}
*/
function loadModuleSync() {
if (modPromise) throw new Error("The WASM module is being loaded asynchronously but hasn't finished");
Expand All @@ -80,7 +80,7 @@ function loadModuleSync() {
/**
* Loads and instantiates the WASM module asynchronously
*
* @returns {Promise<typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")>}
* @returns {Promise<typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d", { with: { "resolution-mode": "require" } })>}
*/
async function loadModuleAsync() {
const bytes = await readFile(filename);
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ruma",
"nio"
],
"type": "module",
"exports": {
".": {
"matrix-org:wasm-esm": {
Expand All @@ -26,8 +27,8 @@
},
"require": {
"types": "./index.d.ts",
"node": "./node.js",
"default": "./index.js"
"node": "./node.cjs",
"default": "./index.cjs"
},
"import": {
"types": "./index.d.ts",
Expand All @@ -38,11 +39,11 @@
},
"files": [
"index.mjs",
"index.js",
"index.cjs",
"index-wasm-esm.mjs",
"index.d.ts",
"node.mjs",
"node.js",
"node.cjs",
"pkg/matrix_sdk_crypto_wasm.d.ts",
"pkg/matrix_sdk_crypto_wasm_bg.js",
"pkg/matrix_sdk_crypto_wasm_bg.cjs",
Expand Down
1 change: 1 addition & 0 deletions src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl From<RustShieldState> for ShieldState {
#[cfg(test)]
pub(crate) mod tests {
use wasm_bindgen_test::wasm_bindgen_test;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_node_experimental);

use super::EncryptionAlgorithm;

Expand Down
1 change: 1 addition & 0 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ pub(crate) mod tests {
use matrix_sdk_crypto::types::requests::KeysQueryRequest as OriginalKeysQueryRequest;
use serde_json::Value;
use wasm_bindgen_test::wasm_bindgen_test;
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_node_experimental);

use super::{KeysClaimRequest, KeysQueryRequest, KeysUploadRequest};

Expand Down

0 comments on commit e4153ec

Please sign in to comment.