-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Using JS Module inside Webpack Project #700
Comments
|
@xtuc I think I wasn't clear. I am using the |
If I reduce my webpack project down to just |
Main project - src/index.ts: import('../../dmg-01-js/pkg/dmg_01_js').then(dmg => console.log(dmg)) dmg_01_js: import * as wasm from './dmg_01_js_bg';
function foo() {
wasm.add(0,0)
} webpack config: module.exports = {
entry: "./src/index.tsx",
output: {
filename: "cpu.js",
path: __dirname + "/dist",
// Export this as a library that can be accessed from plain JS
},
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json", ".wasm"]
},
mode: 'development',
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
}; |
Since the index uses a dynamic import, your bundle is splited and everything can be synchronous then. The example you posted seems fine to me, I'm just wondering if TypeScript would transform the Also we are currently working on a better Webpack integration, see rustwasm/rust-webpack-template#15. It would be great if you can make any feedback on it. |
@xtuc you might be right. I switched to js and it worked. I can try to find out what the typescript loader is doing |
Yeah that would be very helpful for us |
Thanks for your exchanges ! I found something interesting: So after that I have add
And remove any loader in webpack. So now it will load "raw" files. |
This issue has been stale for a bit and I think it's largely about webpack issues so I'm going to close it here on this repo, but if there's wasm-bindgen issues please let us know! |
I am also getting this error:
If this is a 'webpack issue' that means that people can't use Rust wasm in popular webpack based libraries such as React and Vue.js. This would seem to be a big deal and rather than merit closing the issue, I think it probably should be addressed. Thank you. |
For the record, the way I currently work around this is by having one JavaScript file that exposes a function that simply imports the wasm module. This file is built using a babel loader and is not touched by the TypeScript compiler. I then can include that JavaScript file in my TypeScript. As I've said before, this seems to be an issue with the TypeScript compiler - I'm not sure exactly what it's doing, but it seems to be messing up how imports work. @patientplatypus it doesn't seem like you're using TypeScript but having the same issue. Are you using the |
I've posted the pieces for a pure-TypeScript soluton on issue #43 using |
@emk thanks! Would you mind adding it to our Readme for future users? |
@xtuc It might be a good idea to turn this into a fully-working example project, hook it up to CI as part of #922, and then add instructions to the README with a link to the actual working project. Otherwise, I fear that's it's too easy to leave out steps or to regress. Webpack + TypeScript + WebAssembly can be pretty fragile. I'd be happy to take a shot at this sometime this coming weekend. |
Yes, that's a good point. Should we commit a tsc configuration in the template? Could you please send a PR :) |
@xtuc It would be possible to update the template to use TypeScript, but it would involve the following changes:
Would we really want to do (1)? Or would it be better to have a separate template for TypeScript projects? There's already a |
Hi, I'm running into this problem too, but I'm a beginner and don't know where to start, sorry. Could someone help me please? My workflow is very simple, as follows:
What do I need to do? I don't want to publish to an npm registry or anything. The "npm link" method, which worked in the "book" example, gave me the error described above:
I then tried to follow your explanations above and have now managed to import the module like so: Questions:
|
@sandromario I think you're running into webpack/webpack#6615 where wasm instantiation is required to be asynchronous, which makes it a bit more difficult to expose and use at the bundler level, leading to this current limitation |
|
I know this is an old issue but cant seem to see what the solution is? |
For others experiencing this, I had this line in a typescript file:
Which was triggering the I created a javascript file called
And then added an import at the top of the original typescript file:
And changed the original line like so: - const stitch = await import("../pkg/index.js").catch(console.error);
+ const stitch = await import_wasm().catch(console.error); I also had to add:
To a And that resolved my issue. |
Just wanted to share a similar solution building off of @michaeljones's excellent answer. I wanted to ensure that my wasm module would be typed, which the below solution does: index.ts import { import_wasm } from './import_wasm';
(async () => {
const module = await import_wasm();
module.greet();
})(); import_wasm.js export function import_wasm() {
return import("../pkg/index.js");
} import_wasm.d.ts export const import_wasm: () => Promise<typeof import('../pkg/index')>; |
if you are deploy to nginx , just set the mime-type,like this:
https://platform.uno/docs/articles/how-to-host-a-webassembly-app.html |
This is probably a webpack issue, but I wanted to confirm here.
Using a wasm-bindgen I built a library that I want to include as a dependency in another webpack based project. I added a package.json file to the output from wasm-bindgen and set the generated js file as the main file. Finally, I set the directory where my generated files and this package.json are located as the location in my webpack project's package.json.
Inside of my webpack project I import this library using the async
import()
since wasm file's can't be imported synchronously. However, I still get the error:I'm not sure why I'm getting this issue as I am using an async
import()
to import my wasm-bindgen generated lib. Any ideas? Does this seem like a webpack issue to you?The text was updated successfully, but these errors were encountered: