|
| 1 | +# WebAssembly and Rust Example |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +# Configuration |
| 6 | + |
| 7 | +## Rust Environment |
| 8 | + |
| 9 | +Before beginning ensure that the [Rust toolchain](https://www.rust-lang.org/tools/install) is installed and configured on your machine. |
| 10 | + |
| 11 | +Follow the instructions and the `rustup` installation process. Once completed, your shell will have access to `cargo` commands. |
| 12 | + |
| 13 | +> You **must** have the Rust toolchain installed in order to build the project |
| 14 | +
|
| 15 | +**Note:** For Windows users, the [Microsoft C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) must be installed for the Rust compiler to work properly. For more information, consult the [Rust documentation](https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-windows). |
| 16 | + |
| 17 | +### 1. Install [wasm-pack](https://github.com/rustwasm/wasm-pack) |
| 18 | + |
| 19 | +``` |
| 20 | +$ cargo install wasm-pack |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Test local Rust configuration and [wasm-pack](https://github.com/rustwasm/wasm-pack) installation |
| 24 | + |
| 25 | +``` |
| 26 | +$ yarn test # or cargo test && wasm-pack test --node |
| 27 | +``` |
| 28 | + |
| 29 | +## Node.js |
| 30 | + |
| 31 | +### 1. Install the Node.js dependencies: |
| 32 | + |
| 33 | +``` |
| 34 | +$ yarn install # or npm install |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Run plugin in watch or build mode |
| 38 | + |
| 39 | +``` |
| 40 | +$ yarn watch # or npm run watch |
| 41 | +
|
| 42 | +# OR |
| 43 | +
|
| 44 | +$ yarn build # or npm run build |
| 45 | +``` |
| 46 | + |
| 47 | +> You **must** run `build` prior to trying to use this plugin within Photoshop! |
| 48 | +
|
| 49 | +- `yarn watch` or `npm run watch` will build a development version of the plugin, and recompile everytime you make a change to the source files. The result is placed in `dist`. |
| 50 | +- `yarn build` or `npm run build` 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. |
| 51 | + |
| 52 | +**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`: |
| 53 | + |
| 54 | +``` |
| 55 | +import encodedRust from '../wasm/uxp.wasm'; |
| 56 | +``` |
| 57 | + |
| 58 | +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: |
| 59 | + |
| 60 | +``` |
| 61 | +$ yarn inlinewasm # generate JS file containing the inline WebAssembly data |
| 62 | +``` |
| 63 | + |
| 64 | +## Launching in Photoshop |
| 65 | + |
| 66 | +You can use the UXP Developer Tools to load the plugin into Photoshop. |
| 67 | + |
| 68 | +If the plugin hasn't already been added to your workspace in the UXP Developer Tools, you can add it by clicking "Add Plugin..." and selecting `dist/manifest.json`. **DO NOT** select the `manifest.json` file inside the `plugin` folder. |
| 69 | + |
| 70 | +Once added, you can load it into Photoshop by clicking the ••• button on the corresponding row, and clicking "Load". Switch to Photoshop and you should see the starter panels. |
| 71 | + |
| 72 | +## What this plugin does |
| 73 | + |
| 74 | +This plugin uses a basic Rust implementation to hold state for a counter component that is surfaced on the UI. It also uses the `wasm-bindgen` API to make use of JavaScript API methods in Rust, such as `console.log`. Finally, the plugin demonstrates a potential build process for leveraging Rust and WebAssembly in your UXP plugins. |
0 commit comments