Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI/CD + cargo workspace #8

Closed
lencx opened this issue Apr 15, 2021 · 19 comments
Closed

CI/CD + cargo workspace #8

lencx opened this issue Apr 15, 2021 · 19 comments
Labels
feat New feature or request v1 v1.x.x

Comments

@lencx
Copy link
Member

lencx commented Apr 15, 2021

Let's discuss it here.

#5 (comment)
#3 (comment)

@lencx lencx added the feat New feature or request label Apr 15, 2021
@audacioustux
Copy link

audacioustux commented Apr 15, 2021

about cargo workspace: I'm using yarn workspace btw, so if I add wasm-crates/**/pkg in yarn workspaces, and wasm-crates/* in cargo workspace... then run wasm pack on all cargo crates, this should work I guess... not sure. but this is what I need

@lencx
Copy link
Member Author

lencx commented Apr 15, 2021

Does wasm-crates/**/pkg mean this?

wasm-pack build --out-dir

// vite.config.ts

// ...
ViteRsw({
  root: 'custom/path', // rust crates
  mode: "release",
  // support two ways at the same time
  crates: [
   'rsw-hey', // way 1
	// outDir: --out-dir
	// name: npm package
+	{ outDir: 'custom/path', name: 'rsw-hello' }, // way 2
	// ...
  ],
})

@audacioustux
Copy link

umm kinda... but specifying outDir manually for multiple wasm crates would be a bit of pain I guess

@lencx
Copy link
Member Author

lencx commented Apr 15, 2021

Support two ways at the same time.

@kjagiello
Copy link

As a workaround in CI/CD, you can build the package yourself. See here. Not ideal, but does the trick for now.

@audacioustux
Copy link

audacioustux commented Apr 16, 2021

using yarn workspace + cargo workspace + a bit of scripting(in rust for fun) to run wasm-pack on all wasm crates https://github.com/audacioustux/audacioustux/blob/main/crates/scripts/src/main.rs
this is obviously not a generic solution for all. but I'll stick with it for now

lencx added a commit that referenced this issue May 4, 2021
@lencx
Copy link
Member Author

lencx commented May 9, 2021

@kjagiello
Copy link

@audacioustux @kjagiello

rsw-node
remote deployment project

Great! Will give it a try and let you know the results.

@lencx
Copy link
Member Author

lencx commented May 9, 2021

You can use the rsw command alone, and will no longer rely on the vite plug-in system. But there are many restrictions, the build will use the basic configuration.

For example: .rswrc.json

{
  "root": ".", // project root path
  "crate:": [
  	"@rsw/hello", // ✅
  	{"name": "rsw-hello", "outDir": "custom/path"} // ✅
  ]
}

https://github.com/lencx/vite-plugin-rsw#remote-deployment-project
https://github.com/lencx/rsw-node#get-started
WeChat34fbef037c409c48f686180c1103f13b

@husayt
Copy link

husayt commented Jul 5, 2021

I have followed all instructions, however when do rsw && vite build I don't get wasm files in dist folder. I have Windows and it's a Vue project.

Thanks

@lencx
Copy link
Member Author

lencx commented Jul 5, 2021

@husayt Please make sure you have installed rsw-node and configured .rswrc.json

Can refer to this project: https://github.com/lencx/learn-wasm

@husayt
Copy link

husayt commented Jul 5, 2021

@lencx yes, I have done that as well. In dev all works. But when I build there are no wasm files in dist folder.

@lencx
Copy link
Member Author

lencx commented Jul 5, 2021

@husayt Can you take a screenshot of rsw && vite build? Or provide a simple demo project.

@husayt
Copy link

husayt commented Jul 5, 2021

image
Wasm files are created in rsw target folder, but they are not compied to dist folder.

.rswrc.json

{
    "root": ".",
    "crates": ["rsw-test","@rsw/hey"]
  }

and in vite.config.ts

...
ViteRsw({
      root: ".",
      crates: [
        "@rsw/hey",
        "rsw-test",
      ],
    }),
...

@lencx
Copy link
Member Author

lencx commented Jul 5, 2021

@husayt Please provide the following screenshot, this screenshot seems to be ok.

vite v2.3.8 building for production...
# output: screenshot

@husayt
Copy link

husayt commented Jul 5, 2021

This is what i see when I run the webserver:
image

During the build I see rsw files mentioned:
image

But no any wasm files at the end:
image

@lencx
Copy link
Member Author

lencx commented Jul 5, 2021

@husayt Can you send the file @rsw/hey/pkg/rsw~hey.js, because I don’t have a windows computer and I haven’t done path compatibility processing.

@husayt
Copy link

husayt commented Jul 5, 2021

here it is: @rsw/hey/pkg/rsw~hey.js

let wasm;

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachegetUint8Memory0 = null;
function getUint8Memory0() {
    if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
        cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachegetUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
    return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
    ? function (arg, view) {
    return cachedTextEncoder.encodeInto(arg, view);
}
    : function (arg, view) {
    const buf = cachedTextEncoder.encode(arg);
    view.set(buf);
    return {
        read: arg.length,
        written: buf.length
    };
});

function passStringToWasm0(arg, malloc, realloc) {

    if (realloc === undefined) {
        const buf = cachedTextEncoder.encode(arg);
        const ptr = malloc(buf.length);
        getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
        WASM_VECTOR_LEN = buf.length;
        return ptr;
    }

    let len = arg.length;
    let ptr = malloc(len);

    const mem = getUint8Memory0();

    let offset = 0;

    for (; offset < len; offset++) {
        const code = arg.charCodeAt(offset);
        if (code > 0x7F) break;
        mem[ptr + offset] = code;
    }

    if (offset !== len) {
        if (offset !== 0) {
            arg = arg.slice(offset);
        }
        ptr = realloc(ptr, len, len = offset + arg.length * 3);
        const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
        const ret = encodeString(arg, view);

        offset += ret.written;
    }

    WASM_VECTOR_LEN = offset;
    return ptr;
}
/**
* @param {string} name
*/
export function greet(name) {
    var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
    var len0 = WASM_VECTOR_LEN;
    wasm.greet(ptr0, len0);
}

async function load(module, imports) {
    if (typeof Response === 'function' && module instanceof Response) {
        if (typeof WebAssembly.instantiateStreaming === 'function') {
            try {
                return await WebAssembly.instantiateStreaming(module, imports);

            } catch (e) {
                if (module.headers.get('Content-Type') != 'application/wasm') {
                    console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

                } else {
                    throw e;
                }
            }
        }

        const bytes = await module.arrayBuffer();
        return await WebAssembly.instantiate(bytes, imports);

    } else {
        const instance = await WebAssembly.instantiate(module, imports);

        if (instance instanceof WebAssembly.Instance) {
            return { instance, module };

        } else {
            return instance;
        }
    }
}

async function init(input) {
    if (typeof input === 'undefined') {
        input = new URL('rsw~hey_bg.wasm', import.meta.url);
    }
    const imports = {};
    imports.wbg = {};
    imports.wbg.__wbg_alert_36567ccdaebdf23d = function(arg0, arg1) {
        alert(getStringFromWasm0(arg0, arg1));
    };

    if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
        input = fetch(input);
    }



    const { instance, module } = await load(await input, imports);

    wasm = instance.exports;
    init.__wbindgen_wasm_module = module;

    return wasm;
}

export default init;

@lencx lencx mentioned this issue Jul 5, 2021
@lencx
Copy link
Member Author

lencx commented Jul 5, 2021

@husayt We are discussing here (#12), it may be caused by windows path. You can now install the v1.6.4 version and try rsw && vite build again

@lencx lencx changed the title [feat]: CI/CD + cargo workspace CI/CD + cargo workspace Jul 6, 2021
@lencx lencx pinned this issue Jul 7, 2021
@lencx lencx closed this as completed Jul 7, 2021
@lencx lencx added the v1 v1.x.x label Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request v1 v1.x.x
Projects
None yet
Development

No branches or pull requests

4 participants