Skip to content

Commit ef8c757

Browse files
committed
Fixed build process to include wasm-bindgen and wasm-pack, simplified package.json scripts and build process
1 parent 66caa3c commit ef8c757

File tree

11 files changed

+370
-44
lines changed

11 files changed

+370
-44
lines changed

wasm-rust-sample/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ dist/
33
target/
44
pkg/
55
wasm-pack.log
6-
uxp-wasm.js
6+
*.wasm.js

wasm-rust-sample/Cargo.lock

Lines changed: 199 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm-rust-sample/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ edition = "2018"
77

88
[lib]
99
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
js-sys = "0.3.52"
13+
wasm-bindgen = "0.2.75"
14+
web-sys = { version = "0.3.52", features = ["console"] }
15+
16+
[dev-dependencies]
17+
wasm-bindgen-test = "0.3.25"

wasm-rust-sample/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22

33
This plugin is a starting point for leveraging Rust and WebAssembly in your UXP plugins. It comes defined with most of the dependencies you need to get started. As this plugin does rely on the [Rust Programming Language](https://www.rust-lang.org/), an environment configured for Rust development will be required before this will be usable in Photoshop.
44

5-
**Note:** As part of working with the current limitations of UXP the Rust/WebAssembly build process within this plugin has _many_ unconventional ways of getting things done.
6-
7-
If you happen to be leveraging Rust/WebAssembly in a more traditional manner, please checkout tools such as [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen/) and [wasm-pack](https://github.com/rustwasm/wasm-pack) that the Rust development team are actively working on.
85

96
# Configuration
107

118
## Rust Environment
129

1310
Before beginning ensure that the [Rust toolchain](https://www.rust-lang.org/tools/install) is installed and configured on your machine.
1411

15-
Follow the instructions and the `rustup` installation. Once completed, your machine will be able to execute `cargo` commands.
12+
Follow the instructions and the `rustup` installation process. Once completed, your shell will have access to `cargo` commands.
13+
14+
> You **must** have the Rust toolchain installed in order to build the project
1615
17-
To test if Rust is configured properly before loading the project, run the basic tests in the project's root:
16+
### 1. Install [wasm-pack](https://github.com/rustwasm/wasm-pack)
1817

1918
```
20-
$ yarn test # or cargo test
19+
$ cargo install wasm-pack
2120
```
2221

23-
> You **must** have the Rust toolchain installed in order to build the project
22+
### 2. Test local Rust configuration and [wasm-pack](https://github.com/rustwasm/wasm-pack) installation
23+
24+
```
25+
$ yarn test # or cargo test && wasm-pack test
26+
```
2427

2528
## Node.js
2629

@@ -42,17 +45,16 @@ $ yarn build # or npm run build
4245
4346
This will build a production version of the plugin and place it in `dist`. It will not update every time you make a change to the source files.
4447

45-
**Note:** Since UXP does not have implicit access to `localhost` for leveraging a development server, this plugin uses inline WebAssembly to work properly. In `js/index.js` this file is included:
48+
**Note:** Since UXP does not have implicit access to `localhost` for leveraging a development server, this plugin uses inline WebAssembly to work properly. As such, you'll find the following import in `js/index.js`:
4649

4750
```
48-
import encodedRust from './uxp-wasm';
51+
import encodedRust from './uxp.wasm';
4952
```
5053

5154
When the build script is triggerred, this JS file with the inline WebAssembly is generated. Should you wish to generate this file yourself, execute the following commands:
5255

5356
```
54-
$ yarn wasm:build # compile the Rust crate to WebAssembly
55-
$ yarn wasm:inline # generate JS file containing the inline WebAssembly data
57+
$ yarn inlinewasm # generate JS file containing the inline WebAssembly data
5658
```
5759

5860
## Launching in Photoshop

wasm-rust-sample/js/index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { entrypoints } from 'uxp';
2-
import { Buffer } from 'buffer';
3-
import encodedRust from './uxp-wasm';
2+
import { decodeWebAssembly } from './utils';
3+
4+
import encodedRust from './uxp.wasm';
5+
import init, { run, add, multiply } from '../pkg/uxp_wasm.js';
46

57
entrypoints.setup({
68
plugin: {
@@ -10,20 +12,15 @@ entrypoints.setup({
1012
},
1113
});
1214

13-
// Decode from base64 to binary
14-
// js-inline-wasm can serve this as decoded, however atob() is not accessible in UXP
15-
let decodedRust = Buffer.from(encodedRust, 'base64').toString('binary');
16-
const len = decodedRust.length;
17-
const bytes = new Uint8Array(len);
15+
const main = async () => {
16+
const decodedRust = decodeWebAssembly(encodedRust);
1817

19-
// Collect byte array
20-
for (var i = 0; i < len; i++) {
21-
bytes[i] = decodedRust.charCodeAt(i);
22-
}
18+
// Manually pass WebAssembly to prevent `wasm-bindgen` from using `fetch()`
19+
await init(decodedRust);
20+
console.log(`2 + 2 = ${add(2, 2)}`);
21+
console.log(`2 * 2 = ${multiply(2, 2)}`);
22+
};
2323

24-
WebAssembly.instantiate(bytes, {})
25-
.then((obj) => {
26-
console.log(`1 + 1 = ${obj.instance.exports.add(1, 1)}`);
27-
console.log(`2 * 2 = ${obj.instance.exports.multiply(2, 2)}`);
28-
})
29-
.catch((e) => console.log(e));
24+
await main().catch((err) => {
25+
console.log(err);
26+
});

wasm-rust-sample/js/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Buffer } from 'buffer';
2+
3+
/**
4+
* Decodes WebAssembly from base64 to binary
5+
* js-inline-wasm can serve this as decoded, however the decoding method is deprecated from the Node.js API
6+
*/
7+
export const decodeWebAssembly = async (encodedWebAssembly) => {
8+
let decodedRust = Buffer.from(encodedWebAssembly, 'base64').toString(
9+
'binary'
10+
);
11+
12+
const len = decodedRust.length;
13+
const byteArray = new Uint8Array(len);
14+
15+
for (var i = 0; i < len; i++) {
16+
byteArray[i] = decodedRust.charCodeAt(i);
17+
}
18+
19+
return byteArray;
20+
};

wasm-rust-sample/package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44
"license": "none",
55
"private": true,
66
"scripts": {
7-
"build": "npm run compile && npm run pack",
8-
"pack": "webpack --mode development",
9-
"compile": "npm run wasm:build && npm run wasm:inline",
10-
"wasm:build": "cargo build --target wasm32-unknown-unknown --release",
11-
"wasm:inline": "npx crlf --set=LF node_modules/.bin/inlinewasm && inlinewasm target/wasm32-unknown-unknown/release/uxp_wasm_rust_sample.wasm -o js/uxp-wasm.js -t encoded",
12-
"test": "cargo test"
7+
"build": "rimraf dist && webpack --mode development",
8+
"inlinewasm": "npx crlf --set=LF node_modules/.bin/inlinewasm && inlinewasm pkg/uxp_wasm_bg.wasm -o js/uxp.wasm.js -t encoded",
9+
"test": "cargo test && wasm-pack test --node"
1310
},
1411
"devDependencies": {
1512
"@babel/core": "^7.14.6",
1613
"@babel/preset-env": "^7.14.7",
14+
"@wasm-tool/wasm-pack-plugin": "^1.5.0",
1715
"babel-loader": "^8.2.2",
16+
"babel-polyfill": "^6.26.0",
1817
"buffer": "^6.0.3",
1918
"copy-webpack-plugin": "^9.0.1",
2019
"css-loader": "^5.2.6",
2120
"js-inline-wasm": "^0.0.7",
21+
"rimraf": "^3.0.2",
2222
"style-loader": "^3.0.0",
2323
"text-encoding": "^0.7.0",
2424
"webpack": "^5.44.0",
2525
"webpack-cli": "^4.7.2",
26-
"webpack-dev-server": "^3.11.2"
26+
"webpack-dev-server": "^3.11.2",
27+
"webpack-shell-plugin-next": "^2.2.2"
2728
}
28-
}
29+
}

0 commit comments

Comments
 (0)