Skip to content

Commit 6e11447

Browse files
authored
Merge pull request #32 from cwheel/webpack_4_es6_import
ES6 style imports for bindgen Webpack 4
2 parents a3facce + 27ab84e commit 6e11447

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

examples/advanced-wasm-bindgen-webpack4-native-wasm/src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { wasmBooted, hello_world } from '../../advanced-wasm-bindgen/src/lib.rs';
2+
13
const firstNameInput = document.getElementById('firstName');
24
const lastNameInput = document.getElementById('lastName');
35
const outputElement = document.getElementById('output');
46

57
const update = () => {
68
const firstName = firstNameInput.value;
79
const lastName = lastNameInput.value;
8-
import('../../advanced-wasm-bindgen/src/lib.rs').then(({hello_world}) => outputElement.textContent = hello_world(firstName, lastName));
10+
wasmBooted.then(() => outputElement.textContent = hello_world(firstName, lastName));
911
};
1012

1113
firstNameInput.addEventListener('input', update);

examples/advanced-wasm-bindgen-webpack4-native-wasm/webpack.config.js

-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ module.exports = {
3030
{
3131
test: /\.rs$/,
3232
use: [
33-
{
34-
loader: 'babel-loader',
35-
options: {
36-
compact: true,
37-
}
38-
},
3933
{
4034
loader: 'rust-native-wasm-loader',
4135
options: {

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ const DEFAULT_OPTIONS = {
2020

2121
const SUPPORTED_WASM_BINDGEN_VERSION = '^0.2';
2222
const SUPPORTED_CARGO_WEB_VERSION = '^0.6.9';
23+
const MIN_WEBPACK_NATIVE_WASM_VERSION = '^4.0.0';
2324

2425
const loadWasmBindgen = async function (self, {release, target, wasmBindgen}, srcDir) {
2526
const wasmBindgenVersion = await clapVersion('wasm-bindgen', srcDir);
27+
const webpackVersion = await clapVersion('npx webpack', srcDir);
2628

2729
if (!semver.satisfies(wasmBindgenVersion, SUPPORTED_WASM_BINDGEN_VERSION)) {
2830
throw new BuildError(
@@ -85,6 +87,13 @@ export const wasmBooted: Promise<boolean> = wasm.booted
8587
`;
8688
}
8789
return contents;
90+
} else if (!wasmBindgen.wasm2es6js && semver.satisfies(webpackVersion, MIN_WEBPACK_NATIVE_WASM_VERSION)) {
91+
const jsRequest = loaderUtils.stringifyRequest(self, suffixlessPath + '.js');
92+
93+
return `module.exports.wasmBooted = import(${jsRequest}).then(wasmModule => {
94+
const keys = Object.keys(wasmModule);
95+
for (let key of keys) module.exports[key] = wasmModule[key];
96+
})`;
8897
} else {
8998
const jsRequest = loaderUtils.stringifyRequest(self, suffixlessPath + '.js');
9099
let contents = '';

0 commit comments

Comments
 (0)