├── .npmignore ├── .gitignore ├── example ├── index.js ├── .gitignore ├── Cargo.toml ├── README.md ├── package.json ├── webpack.config.js ├── src │ └── lib.rs └── Cargo.lock ├── .prettierrc ├── plugin.d.ts ├── LICENSE ├── package.json ├── README.md └── plugin.js /.npmignore: -------------------------------------------------------------------------------- 1 | example 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json 3 | yarn.lock 4 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | import('./pkg').then((module) => { 2 | module.run() 3 | }) 4 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /target 3 | **/*.rs.bk 4 | node_modules/ 5 | pkg/ 6 | dist/ 7 | package-lock.json 8 | *.log 9 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "tabWidth": 4, 4 | "semi": false, 5 | "singleQuote": true 6 | } 7 | -------------------------------------------------------------------------------- /example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm-pack-test" 3 | version = "0.1.0" 4 | authors = ["Sven Sauleau "] 5 | 6 | [lib] 7 | crate-type = ["cdylib"] 8 | 9 | [dependencies] 10 | wasm-bindgen = "0.2.73" 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | Note that the binaries are available in `./node_modules/.bin`. 4 | 5 | ## Start Webpack 6 | 7 | In watch and development mode: 8 | 9 | ```sh 10 | npm start 11 | ``` 12 | 13 | ## Start the HTTP server 14 | 15 | ```sh 16 | npm run server 17 | ``` 18 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "start": "webpack -w", 4 | "server": "ws -d dist", 5 | "clean": "rimraf dist pkg target" 6 | }, 7 | "devDependencies": { 8 | "@wasm-tool/wasm-pack-plugin": "file:..", 9 | "html-webpack-plugin": "^5.3.1", 10 | "local-web-server": "^2.6.0", 11 | "rimraf": "^2.6.2", 12 | "webpack": "^5.30.0", 13 | "webpack-cli": "^4.6.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const HtmlWebpackPlugin = require('html-webpack-plugin') 3 | const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin') 4 | 5 | module.exports = { 6 | mode: 'development', 7 | entry: './index.js', 8 | output: { 9 | path: path.resolve(__dirname, 'dist'), 10 | filename: 'bundle.js', 11 | }, 12 | plugins: [ 13 | new HtmlWebpackPlugin(), 14 | 15 | new WasmPackPlugin({ 16 | crateDirectory: path.resolve(__dirname, '.'), 17 | }), 18 | ], 19 | experiments: { 20 | syncWebAssembly: true, 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /plugin.d.ts: -------------------------------------------------------------------------------- 1 | import { Compiler } from 'webpack' 2 | 3 | export interface WasmPackPluginOptions { 4 | crateDirectory: string 5 | args?: string 6 | extraArgs?: string 7 | forceWatch?: boolean 8 | forceMode?: 'development' | 'production' 9 | outDir?: string 10 | outName?: string 11 | watchDirectories?: string[] 12 | /** Controls plugin output verbosity. Defaults to 'info'. */ 13 | pluginLogLevel?: 'info' | 'error' 14 | } 15 | 16 | export default class WasmPackPlugin { 17 | constructor(options: WasmPackPluginOptions) 18 | 19 | /** Invocation point for webpack plugins. */ 20 | apply(compiler: Compiler): void 21 | } 22 | 23 | export = WasmPackPlugin 24 | 25 | declare module '@wasm-tool/wasm-pack-plugin' { 26 | export { WasmPackPluginOptions, WasmPackPlugin } 27 | export default WasmPackPlugin 28 | export = WasmPackPlugin 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sven Sauleau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@wasm-tool/wasm-pack-plugin", 3 | "version": "1.6.0", 4 | "description": "Webpack plugin for Rust", 5 | "main": "plugin.js", 6 | "types": "plugin.d.ts", 7 | "directories": { 8 | "example": "example" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1", 12 | "lint:fix": "prettier . --write" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/wasm-tool/wasm-pack-plugin.git" 17 | }, 18 | "keywords": [ 19 | "Rust", 20 | "wasm", 21 | "WebAssembly", 22 | "webpack" 23 | ], 24 | "author": "Sven Sauleau ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/wasm-tool/wasm-pack-plugin/issues" 28 | }, 29 | "homepage": "https://github.com/wasm-tool/wasm-pack-plugin#readme", 30 | "dependencies": { 31 | "chalk": "^2.4.1", 32 | "command-exists": "^1.2.7", 33 | "watchpack": "^2.1.1", 34 | "which": "^2.0.2" 35 | }, 36 | "devDependencies": { 37 | "@types/webpack": "*", 38 | "prettier": "^2.4.1" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /example/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate wasm_bindgen; 2 | 3 | use wasm_bindgen::prelude::*; 4 | 5 | // Definitions of the functionality available in JS, which wasm-bindgen will 6 | // generate shims for today (and eventually these should be near-0 cost!) 7 | // 8 | // These definitions need to be hand-written today but the current vision is 9 | // that we'll use WebIDL to generate this `extern` block into a crate which you 10 | // can link and import. There's a tracking issue for this at 11 | // https://github.com/rustwasm/wasm-bindgen/issues/42 12 | // 13 | // In the meantime these are written out by hand and correspond to the names and 14 | // signatures documented on MDN, for example 15 | #[wasm_bindgen] 16 | extern "C" { 17 | type HTMLDocument; 18 | static document: HTMLDocument; 19 | #[wasm_bindgen(method)] 20 | fn createElement(this: &HTMLDocument, tagName: &str) -> Element; 21 | #[wasm_bindgen(method, getter)] 22 | fn body(this: &HTMLDocument) -> Element; 23 | 24 | type Element; 25 | #[wasm_bindgen(method, setter = innerHTML)] 26 | fn set_inner_html(this: &Element, html: &str); 27 | #[wasm_bindgen(method, js_name = appendChild)] 28 | fn append_child(this: &Element, other: Element); 29 | } 30 | 31 | // Called by our JS entry point to run the example 32 | #[wasm_bindgen] 33 | pub fn run() { 34 | let val = document.createElement("p"); 35 | val.set_inner_html("Hello from Rust!"); 36 | document.body().append_child(val); 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @wasm-tool/wasm-pack-plugin 2 | 3 | > webpack plugin for Rust 4 | 5 | ## Installation 6 | 7 | With npm: 8 | 9 | ```sh 10 | npm install --save-dev @wasm-tool/wasm-pack-plugin 11 | ``` 12 | 13 | Or with Yarn: 14 | 15 | ```sh 16 | yarn add --dev @wasm-tool/wasm-pack-plugin 17 | ``` 18 | 19 | ### `wasm-pack` 20 | 21 | We expect `wasm-pack` to be in your `$PATH`. See [installation here](https://rustwasm.github.io/wasm-pack/installer). 22 | 23 | The minimum required `wasm-pack` version is `0.8.0` 24 | 25 | ## Linting 26 | 27 | This project uses the `prettier` with default configuration. Fo manually format the code run the `lint:fix` script. 28 | 29 | ## Usage 30 | 31 | Add the loader in your `webpack.config.js`: 32 | 33 | ```js 34 | const path = require('path') 35 | const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin') 36 | 37 | module.exports = { 38 | // ... 39 | 40 | plugins: [ 41 | new WasmPackPlugin({ 42 | crateDirectory: path.resolve(__dirname, 'crate'), 43 | 44 | // Check https://rustwasm.github.io/wasm-pack/book/commands/build.html for 45 | // the available set of arguments. 46 | // 47 | // Optional space delimited arguments to appear before the wasm-pack 48 | // command. Default arguments are `--verbose`. 49 | args: '--log-level warn', 50 | // Default arguments are `--typescript --target browser --mode normal`. 51 | extraArgs: '--no-typescript', 52 | 53 | // Optional array of absolute paths to directories, changes to which 54 | // will trigger the build. 55 | // watchDirectories: [ 56 | // path.resolve(__dirname, "another-crate/src") 57 | // ], 58 | 59 | // The same as the `--out-dir` option for `wasm-pack` 60 | // outDir: "pkg", 61 | 62 | // The same as the `--out-name` option for `wasm-pack` 63 | // outName: "index", 64 | 65 | // If defined, `forceWatch` will force activate/deactivate watch mode for 66 | // `.rs` files. 67 | // 68 | // The default (not set) aligns watch mode for `.rs` files to Webpack's 69 | // watch mode. 70 | // forceWatch: true, 71 | 72 | // If defined, `forceMode` will force the compilation mode for `wasm-pack` 73 | // 74 | // Possible values are `development` and `production`. 75 | // 76 | // the mode `development` makes `wasm-pack` build in `debug` mode. 77 | // the mode `production` makes `wasm-pack` build in `release` mode. 78 | // forceMode: "development", 79 | 80 | // Controls plugin output verbosity, either 'info' or 'error'. 81 | // Defaults to 'info'. 82 | // pluginLogLevel: 'info' 83 | }), 84 | ], 85 | 86 | // ... 87 | } 88 | ``` 89 | 90 | and then import your `pkg` folder from `wasm-pack`: 91 | 92 | ```js 93 | import('./path/to/your/pkg').then((module) => { 94 | module.run() 95 | }) 96 | ``` 97 | -------------------------------------------------------------------------------- /example/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "bumpalo" 5 | version = "3.6.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" 8 | 9 | [[package]] 10 | name = "cfg-if" 11 | version = "0.1.9" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" 14 | 15 | [[package]] 16 | name = "cfg-if" 17 | version = "1.0.0" 18 | source = "registry+https://github.com/rust-lang/crates.io-index" 19 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 20 | 21 | [[package]] 22 | name = "lazy_static" 23 | version = "1.3.0" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" 26 | 27 | [[package]] 28 | name = "log" 29 | version = "0.4.6" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" 32 | dependencies = [ 33 | "cfg-if 0.1.9", 34 | ] 35 | 36 | [[package]] 37 | name = "proc-macro2" 38 | version = "1.0.26" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" 41 | dependencies = [ 42 | "unicode-xid", 43 | ] 44 | 45 | [[package]] 46 | name = "quote" 47 | version = "1.0.9" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 50 | dependencies = [ 51 | "proc-macro2", 52 | ] 53 | 54 | [[package]] 55 | name = "syn" 56 | version = "1.0.68" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" 59 | dependencies = [ 60 | "proc-macro2", 61 | "quote", 62 | "unicode-xid", 63 | ] 64 | 65 | [[package]] 66 | name = "unicode-xid" 67 | version = "0.2.1" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 70 | 71 | [[package]] 72 | name = "wasm-bindgen" 73 | version = "0.2.73" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" 76 | dependencies = [ 77 | "cfg-if 1.0.0", 78 | "wasm-bindgen-macro", 79 | ] 80 | 81 | [[package]] 82 | name = "wasm-bindgen-backend" 83 | version = "0.2.73" 84 | source = "registry+https://github.com/rust-lang/crates.io-index" 85 | checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae" 86 | dependencies = [ 87 | "bumpalo", 88 | "lazy_static", 89 | "log", 90 | "proc-macro2", 91 | "quote", 92 | "syn", 93 | "wasm-bindgen-shared", 94 | ] 95 | 96 | [[package]] 97 | name = "wasm-bindgen-macro" 98 | version = "0.2.73" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f" 101 | dependencies = [ 102 | "quote", 103 | "wasm-bindgen-macro-support", 104 | ] 105 | 106 | [[package]] 107 | name = "wasm-bindgen-macro-support" 108 | version = "0.2.73" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" 111 | dependencies = [ 112 | "proc-macro2", 113 | "quote", 114 | "syn", 115 | "wasm-bindgen-backend", 116 | "wasm-bindgen-shared", 117 | ] 118 | 119 | [[package]] 120 | name = "wasm-bindgen-shared" 121 | version = "0.2.73" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489" 124 | 125 | [[package]] 126 | name = "wasm-pack-test" 127 | version = "0.1.0" 128 | dependencies = [ 129 | "wasm-bindgen", 130 | ] 131 | -------------------------------------------------------------------------------- /plugin.js: -------------------------------------------------------------------------------- 1 | const { spawn } = require('child_process') 2 | const fs = require('fs') 3 | const path = require('path') 4 | const commandExistsSync = require('command-exists').sync 5 | const chalk = require('chalk') 6 | const Watchpack = require('watchpack') 7 | const which = require('which') 8 | const { homedir } = require('os') 9 | 10 | const error = (msg) => console.error(chalk.bold.red(msg)) 11 | let info = (msg) => console.log(chalk.bold.blue(msg)) 12 | 13 | function findWasmPack() { 14 | // https://github.com/wasm-tool/wasm-pack-plugin/issues/58 15 | if (process.env['WASM_PACK_PATH'] !== undefined) { 16 | return process.env['WASM_PACK_PATH'] 17 | } 18 | 19 | const inPath = which.sync('wasm-pack', { nothrow: true }) 20 | if (inPath) { 21 | return inPath 22 | } 23 | 24 | const inCargo = path.join(homedir(), '.cargo', 'bin', 'wasm-pack') 25 | if (fs.existsSync(inCargo)) { 26 | return inCargo 27 | } 28 | } 29 | 30 | class WasmPackPlugin { 31 | constructor(options) { 32 | /** 33 | * In some cases Webpack will require the pkg entrypoint before it actually 34 | * exists. To mitigate that we are forcing a first compilation. 35 | * 36 | * See https://github.com/wasm-tool/wasm-pack-plugin/issues/15 37 | */ 38 | this._ranInitialCompilation = false 39 | this.crateDirectory = options.crateDirectory 40 | this.forceWatch = options.forceWatch 41 | this.forceMode = options.forceMode 42 | this.args = (options.args || '--verbose') 43 | .trim() 44 | .split(' ') 45 | .filter((x) => x) 46 | this.extraArgs = (options.extraArgs || '') 47 | .trim() 48 | .split(' ') 49 | .filter((x) => x) 50 | this.outDir = options.outDir || 'pkg' 51 | this.outName = options.outName || 'index' 52 | this.watchDirectories = (options.watchDirectories || []).concat( 53 | path.resolve(this.crateDirectory, 'src') 54 | ) 55 | this.watchFiles = [path.resolve(this.crateDirectory, 'Cargo.toml')] 56 | 57 | if (options.pluginLogLevel && options.pluginLogLevel !== 'info') { 58 | // The default value for pluginLogLevel is 'info'. If specified and it's 59 | // not 'info', don't log informational messages. If unspecified or 'info', 60 | // log as per usual. 61 | info = () => {} 62 | } 63 | 64 | this.wp = new Watchpack() 65 | this.isDebug = true 66 | this.error = null 67 | } 68 | 69 | apply(compiler) { 70 | this.isDebug = this.forceMode 71 | ? this.forceMode === 'development' 72 | : compiler.options.mode === 'development' 73 | 74 | // This fixes an error in Webpack where it cannot find 75 | // the `pkg/index.js` file if Rust compilation errors. 76 | this._makeEmpty() 77 | 78 | // force first compilation 79 | compiler.hooks.beforeCompile.tapPromise('WasmPackPlugin', () => { 80 | if (this._ranInitialCompilation === true) { 81 | return Promise.resolve() 82 | } 83 | 84 | this._ranInitialCompilation = true 85 | 86 | return this._checkWasmPack().then(() => { 87 | const shouldWatch = 88 | this.forceWatch || 89 | (this.forceWatch === undefined && compiler.watchMode) 90 | 91 | if (shouldWatch) { 92 | this.wp.watch({ 93 | files: this.watchFiles, 94 | directories: this.watchDirectories, 95 | startTime: Date.now() - 10000, 96 | }) 97 | 98 | this.wp.on('aggregated', () => { 99 | this._compile(true) 100 | }) 101 | } 102 | 103 | return this._compile(false) 104 | }) 105 | }) 106 | 107 | let first = true 108 | 109 | compiler.hooks.thisCompilation.tap('WasmPackPlugin', (compilation) => { 110 | // Super hacky, needed to workaround a bug in Webpack which causes 111 | // thisCompilation to be triggered twice on the first compilation. 112 | if (first) { 113 | first = false 114 | } else { 115 | // This is needed in order to gracefully handle errors in Webpack, 116 | // since Webpack has its own custom error system. 117 | if (this.error != null) { 118 | compilation.errors.push(this.error) 119 | } 120 | } 121 | }) 122 | } 123 | 124 | _makeEmpty() { 125 | try { 126 | fs.mkdirSync(this.outDir, {recursive: true}) 127 | } catch (e) { 128 | if (e.code !== 'EEXIST') { 129 | throw e 130 | } 131 | } 132 | 133 | fs.writeFileSync(path.join(this.outDir, this.outName + '.js'), '') 134 | } 135 | 136 | async _checkWasmPack() { 137 | info('🧐 Checking for wasm-pack...\n') 138 | 139 | const bin = findWasmPack() 140 | if (bin) { 141 | info('✅ wasm-pack is installed at ' + bin + '. \n') 142 | return true 143 | } 144 | 145 | info('ℹ️ Installing wasm-pack \n') 146 | 147 | if (commandExistsSync('npm')) { 148 | return runProcess('npm', ['install', '-g', 'wasm-pack'], {}) 149 | } else if (commandExistsSync('yarn')) { 150 | return runProcess('yarn', ['global', 'add', 'wasm-pack'], {}) 151 | } else { 152 | error( 153 | '⚠️ could not install wasm-pack, you must have yarn or npm installed' 154 | ) 155 | } 156 | return false 157 | } 158 | 159 | _compile(watching) { 160 | info( 161 | `ℹ️ Compiling your crate in ${ 162 | this.isDebug ? 'development' : 'release' 163 | } mode...\n` 164 | ) 165 | 166 | return fs.promises 167 | .stat(this.crateDirectory) 168 | .then((stats) => { 169 | if (!stats.isDirectory()) { 170 | throw new Error(`${this.crateDirectory} is not a directory`) 171 | } 172 | }) 173 | .then(() => { 174 | return spawnWasmPack({ 175 | outDir: this.outDir, 176 | outName: this.outName, 177 | isDebug: this.isDebug, 178 | cwd: this.crateDirectory, 179 | args: this.args, 180 | extraArgs: this.extraArgs, 181 | }) 182 | }) 183 | .then((detail) => { 184 | // This clears out the error when the compilation succeeds. 185 | this.error = null 186 | 187 | if (detail) { 188 | info(detail) 189 | } 190 | 191 | info('✅ Your crate has been correctly compiled\n') 192 | }) 193 | .catch((e) => { 194 | // Webpack has a custom error system, so we cannot return an 195 | // error directly, instead we need to trigger it later. 196 | this.error = e 197 | 198 | if (watching) { 199 | // This is to trigger a recompilation so it displays the error message 200 | this._makeEmpty() 201 | } 202 | }) 203 | } 204 | } 205 | 206 | function spawnWasmPack({ outDir, outName, isDebug, cwd, args, extraArgs }) { 207 | const bin = findWasmPack() 208 | 209 | const allArgs = [ 210 | ...args, 211 | 'build', 212 | '--out-dir', 213 | outDir, 214 | '--out-name', 215 | outName, 216 | ...(isDebug ? ['--dev'] : []), 217 | ...extraArgs, 218 | ] 219 | 220 | const options = { 221 | cwd, 222 | stdio: 'inherit', 223 | } 224 | 225 | return runProcess(bin, allArgs, options) 226 | } 227 | 228 | function runProcess(bin, args, options) { 229 | return new Promise((resolve, reject) => { 230 | const p = spawn(bin, args, options) 231 | 232 | p.on('close', (code) => { 233 | if (code === 0) { 234 | resolve() 235 | } else { 236 | reject(new Error('Rust compilation.')) 237 | } 238 | }) 239 | 240 | p.on('error', reject) 241 | }) 242 | } 243 | 244 | module.exports = WasmPackPlugin 245 | --------------------------------------------------------------------------------