-
-
Save eyecatchup/194e47dc872c43f179f8777b34e31a30 to your computer and use it in GitHub Desktop.
webpack-emscripten-wasm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <emscripten.h> | |
| EMSCRIPTEN_KEEPALIVE | |
| int fib(int n) { | |
| int i, t, a = 0, b = 1; | |
| for (i = 0; i < n; i++) { | |
| t = a + b; | |
| a = b; | |
| b = t; | |
| } | |
| return b; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <script src="./dist/bundle.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fibonacci from './fibonacci.js'; | |
| import fibonacciModule from './fibonacci.wasm'; | |
| const module = fibonacci({ | |
| locateFile(path) { | |
| if(path.endsWith('.wasm')) { | |
| return fibonacciModule; | |
| } | |
| return path; | |
| } | |
| }); | |
| module.onRuntimeInitialized = () => { | |
| console.log(module._fib(12)); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "lol", | |
| "scripts": { | |
| "build:codec": "docker run --rm -v $(pwd):/src trzeci/emscripten emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]' -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME=\"fibonacci\"' -o ./fibonacci.js fibonacci.c", | |
| "build:bundle": "webpack", | |
| "build": "npm run build:codec && npm run build:bundle", | |
| "serve": "http-server", | |
| "start": "npm run build && npm run serve" | |
| }, | |
| "devDependencies": { | |
| "exports-loader": "^0.7.0", | |
| "file-loader": "^1.1.11", | |
| "http-server": "^0.11.1", | |
| "webpack": "^4.8.3", | |
| "webpack-cli": "^2.1.3" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const webpack = require('webpack'); | |
| const path = require('path'); | |
| module.exports = { | |
| mode: "development", | |
| context: path.resolve(__dirname, '.'), | |
| entry: "./index.js", | |
| output: { | |
| path: path.resolve(__dirname, 'dist'), | |
| filename: "bundle.js" | |
| }, | |
| module: { | |
| defaultRules: [ | |
| { | |
| type: "javascript/auto", | |
| resolve: {} | |
| }, | |
| ], | |
| rules: [ | |
| { | |
| test: /fibonacci\.js$/, | |
| loader: "exports-loader" | |
| }, | |
| { | |
| test: /fibonacci\.wasm$/, | |
| loader: "file-loader", | |
| options: { | |
| publicPath: "dist/" | |
| } | |
| } | |
| ] | |
| }, | |
| // This is necessary due to the fact that emscripten puts both Node and web code into one file. The node part uses Node’s `fs` module to load the wasm file. | |
| // Issue: https://github.com/kripken/emscripten/issues/6542. | |
| plugins: [ | |
| new webpack.IgnorePlugin(/(fs)/), | |
| ] | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment