automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.77k stars 467 forks source link

rollup can't find wasm exports #524

Open BLukassen opened 1 year ago

BLukassen commented 1 year ago

I can't get automerge running in browser with rollup.

none of the exports from the wasm file will be recognized -> [name] is not exported by node_modules/@automerge/automerge-wasm/bundler/automerge_wasm_bg.wasm e.g.: automerge_generateSyncMessage is not exported by ...

what is missing for rollup

src/index.mjs

import * as Automerge from "@automerge/automerge";
export default () => {
    let doc = Automerge.init();
    let actorId = Automerge.getActorId(doc);
    console.log("Automerge", actorId);
}

automerge.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Automerge</title>
</head>
<body>
<h1>Automerge</h1>

<script type="module">
    import main from "./dist/browser/src/index.js";
    main();
</script>
</body>
</html>

rollup.config.js

import { nodeResolve } from '@rollup/plugin-node-resolve';
import { wasm }        from '@rollup/plugin-wasm';
import commonjs        from '@rollup/plugin-commonjs';

export default {
    input: 'src/index.mjs',
    output: {
        sourcemap: true,
        preserveModules: true,
        dir: 'dist/browser',
        format: 'es'
    },
    plugins: [
        commonjs(),
        nodeResolve({
            browser: true,
            extensions: [".js", ".mjs", ".ts", ".wasm"],
            exportConditions: ["module"],
            mainFields: ["main", "module", "browser"]
        }),
        wasm({
            targetEnv: "browser"
        }),
    ]
};

alexjg commented 1 year ago

This looks to me like an issue with Rollup's wasm plugin rather than automerge specifically. Although I appreciate that the state of wasm module support in the JS ecosystem is quite frustrating at the moment.