Skip to content

[WIP] Add WASM Support #6049

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
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/react-scripts/config/jest/wasmTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @remove-on-eject-begin
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @remove-on-eject-end
'use strict';

const path = require('path');

// This is a custom Jest transformer for WASM imports.
// http://facebook.github.io/jest/docs/en/webpack.html

module.exports = {
process(src, filename) {
const assetFilename = JSON.stringify(path.basename(filename));

return `
const fs = require('fs');
const buffer = fs.readFileSync(${assetFilename});
const module = new WebAssembly.Module(buffer);
const instance = new WebAssembly.Instance(module);

module.exports = {
__esmodule: true,
default: instance.exports,
}`;
},
};
1 change: 1 addition & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const moduleFileExtensions = [
'json',
'web.jsx',
'jsx',
'wasm',
];

// Resolve file paths in the same order as webpack
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ module.exports = function(webpackEnv) {
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
exclude: [/\.(js|mjs|jsx|ts|tsx|wasm)$/, /\.html$/, /\.json$/],
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,11 @@ describe('Integration', () => {
);
doc.defaultView.close();
});

it('wasm inclusion', async () => {
const doc = await initDOM('wasm-inclusion');

expect(doc.getElementById('wasm-inclusion').textContent).toBe('11');
});
});
});
5 changes: 5 additions & 0 deletions packages/react-scripts/fixtures/kitchensink/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ class App extends Component {
this.setFeature(f.default)
);
break;
case 'wasm-inclusion':
import('./features/webpack/WasmInclusion').then(f =>
this.setFeature(f.default)
);
break;
default:
throw new Error(`Missing feature "${feature}"`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import('./assets/add.wasm')
.then(console.log)
.catch(console.log);

export default () => <span id="wasm-inclusion" />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import WasmInclusion from './WasmInclusion';

describe('wasm inclusion', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<WasmInclusion />, div);
});
});
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack": "4.23.0",
"webpack-dev-server": "3.1.9",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.3"
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/scripts/utils/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': resolve(
'config/jest/fileTransform.js'
),
'^.+\\.wasm': resolve('config/jest/wasmTransform.js'),
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$',
Expand Down