Skip to content

Passthrough wasm files to webpack internal loaders #7911

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

Closed

Conversation

pmrcunha
Copy link

@pmrcunha pmrcunha commented Oct 31, 2019

Closes #4912.

Hi!
I was unable to load a .wasm file in an app generated with CRA, since the file-loader was catching it and throwing an error.

After ejecting, I realized that a minor change would allow the wasm files to be handled by webpack's internal loaders. This PR adds that change to CRA, so that wasm can be used without ejecting.

I tested my changes manually, by creating an app with CRA, linking my version of react-scripts, running the app normally, and then running the app loading an hello-world wasm project.

Hope this is useful!

// by webpacks internal loaders.
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
exclude: [/\.(js|mjs|jsx|ts|tsx|wasm)$/, /\.html$/, /\.json$/],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! If we were to make this change, perhaps we should add the extension to

const moduleFileExtensions = [

Presumably you could then import ./foo which could resolves ./foo.wasm. Webpack appears to support this behavior by default: https://webpack.js.org/configuration/resolve/#resolveextensions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! I made and tested the change you suggested.
Also, here's a test repo, for reference: https://github.com/pmrcunha/cra-wasm-test

@ianschmitz
Copy link
Contributor

ianschmitz commented Nov 1, 2019

It's not quite clear to me if this is fully supported on webpack's side. It sounds like it's still very much experimental. For example this PR: webpack/webpack#9415 as well as https://github.com/webpack/webpack/blob/master/examples/wasm-simple/webpack.config.js seem to indicate setting experiments properties. Reading the default config logic seems to confirm as well: https://github.com/webpack/webpack/blob/3518ad590f353d53011bdeb466130783821a168f/lib/WebpackOptionsDefaulter.js#L180-L187.

@pmrcunha
Copy link
Author

pmrcunha commented Nov 2, 2019

I think you're right: https://github.com/webpack/changelog-v5/blob/master/README.md#experiments

I'm quite new to wasm myself, so my idea was more that CRA could allow people to experiment with wasm without ejecting, by letting the wasm files through, not so much to fully support it. I totally get it if you feel that that could be risky though.

@ianschmitz ianschmitz removed this from the 3.3 milestone Nov 3, 2019
@ianschmitz
Copy link
Contributor

Thanks for the link! This line in that change log is interesting:

Note that this also means .mjs support and WebAssembly support are now disabled by default.

If that's the case, it's not likely that we will want to enable this under the experiments option when webpack 5 is released, in which case this PR would no longer function correctly. When they consider the feature stable we would likely re-evaluate and include support from our end.

I'm going to close this for now, but please do let us know when it becomes stable in webpack and we'll do our best to support it!

@ianschmitz ianschmitz closed this Nov 3, 2019
@lock lock bot locked and limited conversation to collaborators Nov 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@next react-scripts(webpack.config.js) to resolve .wasm extension?
3 participants