From 0c00e70c84dbd8b15659b0f35df3279aabd673c8 Mon Sep 17 00:00:00 2001 From: Paul Dorehill Date: Tue, 7 Dec 2021 11:09:22 +1100 Subject: [PATCH 1/3] Add abilty to run on apple silicon --- .gitignore | 1 + example/Cargo.toml | 4 +--- example/package.json | 4 ++-- index.js | 21 ++++++++++++++++++--- package.json | 3 +++ 5 files changed, 25 insertions(+), 8 deletions(-) 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/example/Cargo.toml b/example/Cargo.toml index ec6dc37..37ef81c 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -14,6 +14,4 @@ console_error_panic_hook = "0.1.6" [dependencies.web-sys] version = "0.3.35" -features = [ - "console", -] +features = ["console"] diff --git a/example/package.json b/example/package.json index 9176644..3bb2e84 100644 --- a/example/package.json +++ b/example/package.json @@ -7,8 +7,8 @@ "build": "rimraf dist/js && rollup --config" }, "devDependencies": { - "@wasm-tool/rollup-plugin-rust": "^1.0.0", + "@wasm-tool/rollup-plugin-rust": "file:../", "rimraf": "^3.0.2", "rollup": "^1.31.0" } -} +} \ No newline at end of file diff --git a/index.js b/index.js index 1e10cc4..7374781 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,9 @@ 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"); - function posixPath(path) { return path.replace(/\\/g, $path.posix.sep); } @@ -128,6 +128,22 @@ async function get_target_dir(dir) { //return JSON.parse(metadata).target_directory; } +function wasm_pack_path() { + const arch = $os.arch(); + const platform = $os.platform(); + // TODO pretty hacky, but needed to make it work on Windows + if (platform === "win32") { + return "wasm-pack.cmd" + } else if (platform === "darwin" && arch === 'arm64') { + // This is to make it work on Apple Silicon. + // Requires the user has installed wasm-pack separately since the wasm-pack npm package will + // throw on macOS unless 'arch === "x64"' + const home = $os.homedir(); + return $path.join(home, ".cargo/bin/wasm-pack") + } 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(); try { // TODO what if it tries to build the same crate multiple times ? diff --git a/package.json b/package.json index 26003d4..f9a7a73 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,9 @@ "rimraf": "^3.0.0", "rollup-pluginutils": "^2.8.2", "toml": "^3.0.0", + "os": "^0.1.2" + }, + "optionalDependencies": { "wasm-pack": "^0.10.0" } } From 563d84fe7a7257677bc6e6ebf8b2a46420f5435d Mon Sep 17 00:00:00 2001 From: Paul Dorehill Date: Wed, 8 Dec 2021 12:22:48 +1100 Subject: [PATCH 2/3] add wasmPackPath to options --- README.md | 4 ++++ example/package.json | 2 +- index.js | 24 ++++++++++++------------ package.json | 4 ++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9e14f76..e048725 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,10 @@ 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. + // Use of a '~' prefix will be converted to `os.homedir()` + wasmPackPath: "~/.cargo/bin/wasm-pack" }) ``` diff --git a/example/package.json b/example/package.json index 3bb2e84..4e60993 100644 --- a/example/package.json +++ b/example/package.json @@ -7,7 +7,7 @@ "build": "rimraf dist/js && rollup --config" }, "devDependencies": { - "@wasm-tool/rollup-plugin-rust": "file:../", + "@wasm-tool/rollup-plugin-rust": "^1.0.0", "rimraf": "^3.0.2", "rollup": "^1.31.0" } diff --git a/index.js b/index.js index 7374781..5a52719 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const $rimraf = require("rimraf"); const $os = require("os"); const { createFilter } = require("rollup-pluginutils"); + function posixPath(path) { return path.replace(/\\/g, $path.posix.sep); } @@ -128,18 +129,17 @@ async function get_target_dir(dir) { //return JSON.parse(metadata).target_directory; } -function wasm_pack_path() { - const arch = $os.arch(); - const platform = $os.platform(); - // TODO pretty hacky, but needed to make it work on Windows - if (platform === "win32") { +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 if (platform === "darwin" && arch === 'arm64') { - // This is to make it work on Apple Silicon. - // Requires the user has installed wasm-pack separately since the wasm-pack npm package will - // throw on macOS unless 'arch === "x64"' - const home = $os.homedir(); - return $path.join(home, ".cargo/bin/wasm-pack") } else { return "wasm-pack" } @@ -166,7 +166,7 @@ async function wasm_pack(cx, state, dir, source, id, options) { "--", ].concat(options.cargoArgs); - const command = wasm_pack_path(); + 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 f9a7a73..47c7de8 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@wasm-tool/rollup-plugin-rust", "author": "Pauan ", "description": "Rollup plugin for bundling and importing Rust crates.", - "version": "1.0.7", + "version": "1.0.8", "license": "MIT", "repository": "github:wasm-tool/rollup-plugin-rust", "homepage": "https://github.com/wasm-tool/rollup-plugin-rust#readme", @@ -31,4 +31,4 @@ "optionalDependencies": { "wasm-pack": "^0.10.0" } -} +} \ No newline at end of file From ab68bc7d6fc00c08b9b04e421e84c2174685dcf7 Mon Sep 17 00:00:00 2001 From: Paul Dorehill Date: Tue, 14 Dec 2021 08:42:33 +1100 Subject: [PATCH 3/3] fix readme & dependancies --- README.md | 5 +++-- example/Cargo.toml | 4 +++- package.json | 5 ++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e048725..92d08ef 100644 --- a/README.md +++ b/README.md @@ -136,8 +136,9 @@ rust({ importHook: function (path) { return JSON.stringify(path); }, // Allows you to define a custom path to wasm-pack. - // Use of a '~' prefix will be converted to `os.homedir()` - wasmPackPath: "~/.cargo/bin/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 37ef81c..e10014e 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -14,4 +14,6 @@ console_error_panic_hook = "0.1.6" [dependencies.web-sys] version = "0.3.35" -features = ["console"] +features = [ + "console" +] \ No newline at end of file diff --git a/package.json b/package.json index 47c7de8..70d4759 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@wasm-tool/rollup-plugin-rust", "author": "Pauan ", "description": "Rollup plugin for bundling and importing Rust crates.", - "version": "1.0.8", + "version": "1.0.7", "license": "MIT", "repository": "github:wasm-tool/rollup-plugin-rust", "homepage": "https://github.com/wasm-tool/rollup-plugin-rust#readme", @@ -25,8 +25,7 @@ "glob": "^7.1.6", "rimraf": "^3.0.0", "rollup-pluginutils": "^2.8.2", - "toml": "^3.0.0", - "os": "^0.1.2" + "toml": "^3.0.0" }, "optionalDependencies": { "wasm-pack": "^0.10.0"