diff --git a/.gitignore b/.gitignore index d63465b..4caba0a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ package-lock.json yarn-error.log /typescript /example-multi +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 9e14f76..92d08ef 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,11 @@ rust({ // Allows you to customize the behavior for loading the .wasm file, this is for advanced users only! importHook: function (path) { return JSON.stringify(path); }, + + // Allows you to define a custom path to wasm-pack. + // If the path starts with '~' it will be converted to `os.homedir()` + // eg. "~/.cargo/bin/wasm-pack -> "/Users/user_name/.cargo/bin/wasm-pack" + wasmPackPath: "node_modules/.bin/wasm-pack" }) ``` diff --git a/example/Cargo.toml b/example/Cargo.toml index ec6dc37..e10014e 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -15,5 +15,5 @@ console_error_panic_hook = "0.1.6" [dependencies.web-sys] version = "0.3.35" features = [ - "console", -] + "console" +] \ No newline at end of file diff --git a/example/package.json b/example/package.json index 9176644..4e60993 100644 --- a/example/package.json +++ b/example/package.json @@ -11,4 +11,4 @@ "rimraf": "^3.0.2", "rollup": "^1.31.0" } -} +} \ No newline at end of file diff --git a/index.js b/index.js index 1e10cc4..5a52719 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const $path = require("path"); const $child = require("child_process"); const $toml = require("toml"); const $rimraf = require("rimraf"); +const $os = require("os"); const { createFilter } = require("rollup-pluginutils"); @@ -128,6 +129,21 @@ async function get_target_dir(dir) { //return JSON.parse(metadata).target_directory; } +function wasm_pack_path(options) { + if (options.wasmPackPath !== undefined) { + if (typeof (options.wasmPackPath) === "string") { + // Quick hack to allow use of "~" for home directory? + return (options.wasmPackPath.startsWith("~") ? options.wasmPackPath.replace("~", $os.homedir()) : options.wasmPackPath); + } else { + throw new Error("'wasmPackPath' must be a string") + } + } else if (process.platform === "win32") { + // TODO pretty hacky, but needed to make it work on Windows + return "wasm-pack.cmd" + } else { + return "wasm-pack" + } +} async function wasm_pack(cx, state, dir, source, id, options) { const target_dir = await get_target_dir(dir); @@ -150,8 +166,7 @@ async function wasm_pack(cx, state, dir, source, id, options) { "--", ].concat(options.cargoArgs); - // TODO pretty hacky, but needed to make it work on Windows - const command = (process.platform === "win32" ? "wasm-pack.cmd" : "wasm-pack"); + const command = wasm_pack_path(options); try { // TODO what if it tries to build the same crate multiple times ? diff --git a/package.json b/package.json index 26003d4..70d4759 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "glob": "^7.1.6", "rimraf": "^3.0.0", "rollup-pluginutils": "^2.8.2", - "toml": "^3.0.0", + "toml": "^3.0.0" + }, + "optionalDependencies": { "wasm-pack": "^0.10.0" } -} +} \ No newline at end of file