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

Tracking issue for Parcel compatibility #182

Closed
mvlabat opened this issue May 1, 2018 · 20 comments
Closed

Tracking issue for Parcel compatibility #182

mvlabat opened this issue May 1, 2018 · 20 comments

Comments

@mvlabat
Copy link
Contributor

mvlabat commented May 1, 2018

Hi! This is not a bug report (for now I think there isn't any), but just a support request. I wasn't sure it was right to open the issue in this repository, but it seems to me that here I have more chances to get some help. )

I'm trying to get "greet" example (from readme) working with Parcel.

On Rust side everything is compiled successfully. I run wasm-bindgen target/wasm32-unknown-unknown/debug/js_hello_world.wasm --out-dir . command and then import the generated js file like this:

import { greet } from "./js_hello_world.js";

After that I run parcel to build everything as is. But when opening the page in browser I get the following error in console:

Uncaught Error: Cannot find module './js_hello_world_bg'

Parcel expects js_hello_world_bg file to have js extension, but not wasm. Modifying import statement appending wasm to the module path fixes the error, but then I get another one:

Uncaught (in promise) TypeError: WebAssembly Instantiation: Imports argument must be present and must be an object

I don't know how to fix this one. I would be grateful for any help. Thank you!

@alexcrichton
Copy link
Contributor

Thanks for the report! I think this is similar to #154 in that parcel doesn't have enough support for wasm at the moment to get this natively working (unfortunately)

@alexcrichton
Copy link
Contributor

Hm although actually I'm gonna leave this open to track parcel support!

@alexcrichton alexcrichton reopened this May 1, 2018
@xtuc
Copy link
Member

xtuc commented May 8, 2018

FYI parcel-bundler/parcel#1325

@alexcrichton alexcrichton changed the title Can't make it work with Parcel Tracking issue for Parcel compatibility May 21, 2018
@catsigma
Copy link

catsigma commented Sep 14, 2018

I've made a plugin to solve this issue.
https://www.npmjs.com/package/parcel-plugin-wasmbindgen

It works for me.

(Currently the package name is parcel-plugin-wasm.rs)

@fitzgen
Copy link
Member

fitzgen commented Sep 14, 2018

@catsigma where is the source located? I don't see a repo link from the npm page

@catsigma
Copy link

catsigma commented Sep 15, 2018

@fitzgen https://github.com/catsigma/parcel-plugin-wasm.rs

@fitzgen
Copy link
Member

fitzgen commented Sep 18, 2018

@catsigma this is a nice start! For us to promote the plugin, I think we would want:

@catsigma
Copy link

@fitzgen thanks, I'll mod the plugin according to your tips.

@sendilkumarn
Copy link
Member

sendilkumarn commented Sep 22, 2018

@catsigma this looks great, can we rename the plugin to parcel-plugin-rustwasm (if it is okay)

let me know if you need any help.

@catsigma
Copy link

@sendilkumarn
yes, I'd love to.
I'm going to let it support both wasm-pack and cargo & wasm-bindgen(if wasm-pack is missing).

@catsigma
Copy link

catsigma commented Sep 23, 2018

@sendilkumarn the name parcel-plugin-rustwasm has been taken. I'm using the name wasm.rs.
github: https://github.com/catsigma/parcel-plugin-wasm.rs
npm: https://www.npmjs.com/package/parcel-plugin-wasm.rs

And it supports both wasm-pack and wasm-bindgen-cli now.

@sendilkumarn
Copy link
Member

@catsigma a great work, thanks 👍

@sendilkumarn
Copy link
Member

@catsigma check this out.

@fitzgen will transfer the repo once the repo is created.

@catsigma
Copy link

catsigma commented Sep 23, 2018 via email

@alexcrichton
Copy link
Contributor

I'm gonna close this in favor of https://github.com/rustwasm/rust-parcel-template now, new issues should go there!

@0xcaff
Copy link

0xcaff commented Dec 15, 2018

This issue should be re-opened as parcel doesn't work with the generated stub and the way rust-parcel-template works doesn't allow for using the typescript typings.

@mvlabat
Copy link
Contributor Author

mvlabat commented Dec 16, 2018

@0xcaff hello! What problem do you have exactly? And could you clarify what do you mean by "doesn't allow for using the typescript typing"? I didn't work with typescript, but my JS project works great at the moment.

Would be glad to help!

@0xcaff
Copy link

0xcaff commented Dec 16, 2018

Hey @mvlabat !

When using rust-parcel-template, with an import like the following:

import module from '../crate/Cargo.toml'

typescript doesn't know the type of module. It compiles just fine because parcel's typescript compilation doesn't seem to fail on type errors, but all of the tooling reports errors (tsc --noEmit, IDEs) about the type not being known.

Usually, you can tell typescript to use certain types for a module with ambient module declarations, but ambient module declarations don't seem to work with relative paths.

wasm-bindgen already generates the typings, but typescript needs to be told about them outside of the build step so development tools work.

The way that probably fits this use case best is building with wasm-pack and publishing to NPM, but the imported NPM module doesn't work with parcel (even with parcel-plugin-wasm.rs).

@skyne98
Copy link

skyne98 commented Apr 30, 2019

@0xcaff, for now there is a work-around I found.

  • Create an index.d.ts file where your index.ts (or index.js) is
  • Add following to it
declare module "*.toml" {
    const value: typeof import('../crate/pkg/rust_parcel');
    export default value;
}
  • Change ../crate/pkg/rust_parcel to fit your folder structure and file names
  • Profit, you now have types!

@sliminality
Copy link

sliminality commented Aug 21, 2020

@skyne98, what do you mean by this?

Change ../crate/pkg/rust_parcel to fit your folder structure and file names

I'm having the same problem, hoping to resolve! When I add the index.d.ts as you describe, I get the following error:

import wasm from "../crate/Cargo.toml"
src/index.ts:2:18 - error TS2792: Cannot find module '../crate/Cargo.toml'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

sliminality added a commit to sliminality/raidtomi that referenced this issue Oct 23, 2020
God, this was terrible to figure out.

Parcel is lovely and magical, but its module resolution was a bit too
magical for TypeScript. In `index.ts`, Parcel references the Rust crate
by importing the `Cargo.toml` file:

```ts
import wasm from "../crate/Cargo.toml"
```

Parcel understands this, but TypeScript doesn't and freaks out:

```
src/index.ts:2:18 - error TS2792: Cannot find module '../crate/Cargo.toml'.
Did you mean to set the 'moduleResolution' option to 'node', or to add
aliases to the 'paths' option?
```

I mean, no I didn't, but thanks for trying. Anyways, there are two
issues:

- TS can't resolve the import _at all_ because it can't handle non-JS
- TS _definitely_ can't see the typings generated by wasm-bindgen

I played around with many permutations of `tsconfig.json` options,
ranging from `files`, `include`, `baseUrl`, `paths`, and so on. None of
them proved very helpful. In the end, here is what worked:

1. Heeding [this comment](rustwasm/wasm-bindgen#182 (comment)), define a wildcard module declaration (TS's way
of handling non-JS loaders) and use `typeof` to infer the types produced
by wasm-bindgen

    ```ts
    declare module "*.toml" {
        const value: typeof import("../crate/pkg/rust_parcel")
        export default value
    }
    ```
2. Import `index.d.ts` at the top of `index.ts`, as a dependency for the
`Cargo.toml` import:

    ```ts
    /// <reference path="./index.d.ts" />
    import wasm from "../crate/Cargo.toml"
    ```

I needed both steps 1 and 2 to get it to work. Step 1 alone works if
your `tsconfig.json` uses the `files` property, but it doesn't work if
you use `includes` with the same argument.

```json
{
    "files": [
        "./src/index.d.ts",
        "./src/index.ts"
    ]
}
```

Anyways, I now know more than I ever wanted to know about TypeScript.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants