├── .github └── workflows │ └── publish-to-npm.yml ├── .gitignore ├── .prettierrc.yaml ├── LICENSE ├── README.md ├── banner.png ├── example ├── index.html ├── my-crate │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ ├── lib.rs │ │ └── utils.rs ├── package.json ├── src │ └── index.ts └── vite.config.ts ├── npm-crate-example ├── index.html ├── package.json ├── src │ └── index.ts └── vite.config.ts ├── package.json ├── pnpm-lock.yaml ├── src └── index.ts ├── tea.yaml ├── tsconfig.esm.json └── tsconfig.json /.github/workflows/publish-to-npm.yml: -------------------------------------------------------------------------------- 1 | name: Publish to NPM 2 | on: 3 | release: 4 | types: [created] 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Checkout 10 | uses: actions/checkout@v2 11 | - name: Setup Node 12 | uses: actions/setup-node@v2 13 | with: 14 | node-version: '16.x' 15 | registry-url: 'https://registry.npmjs.org' 16 | - name: Install dependencies and build 17 | run: npm install && npm run build 18 | - name: Publish package on NPM 19 | run: npm publish 20 | env: 21 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | esm 4 | yarn.lock 5 | example/my-crate/target -------------------------------------------------------------------------------- /.prettierrc.yaml: -------------------------------------------------------------------------------- 1 | semi: true 2 | tabWidth: 2 3 | singleQuote: true 4 | printWidth: 80 5 | trailingComma: none 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Nshen 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | vite + wasm pic 3 |

4 | 5 |

6 | 7 | 🦀 Vite plugin for rust [wasm-pack](https://github.com/rustwasm/wasm-pack), it's simple. 8 | 9 |

10 | 11 |

12 | 13 | [![npm](https://img.shields.io/npm/v/vite-plugin-wasm-pack.svg)](https://www.npmjs.com/package/vite-plugin-wasm-pack) 14 | [![npm](https://img.shields.io/npm/dt/vite-plugin-wasm-pack)](https://www.npmjs.com/package/vite-plugin-wasm-pack) 15 | [![npm](https://img.shields.io/github/license/nshen/vite-plugin-wasm-pack)](https://www.npmjs.com/package/vite-plugin-wasm-pack) 16 | 17 |

18 | 19 | ## Quick start 20 | 21 | Make sure [wasm-pack](https://github.com/rustwasm/wasm-pack) installed correctly. 22 | 23 | Clone this repo or download the [zip file](https://github.com/nshen/vite-plugin-wasm-pack/archive/refs/heads/main.zip), extract the example folder. 24 | 25 | ``` 26 | example 27 | | 28 | |-- my-crate # rust project folder, there is a Cargo.toml in it 29 | |-- src # front end source code 30 | | |-- index.ts # entry point 31 | |-- index.html # html entry 32 | |-- vite.config.ts # vite config file 33 | |__ package.json # npm config file 34 | ``` 35 | 36 | Install npm develop dependencies, **in example folder** run: 37 | 38 | ```bash 39 | yarn install 40 | # or 41 | # npm install 42 | ``` 43 | 44 | After that you can build `rust project` to `WebAassembly` by using `wasm-pack`. 45 | 46 | ```bash 47 | wasm-pack build ./my-crate --target web 48 | ``` 49 | 50 | Now the `my-crate` module is ready, start vite dev server. 51 | 52 | ```bash 53 | yarn dev 54 | or 55 | #npm run dev 56 | ``` 57 | 58 | Done, if below is showing. 59 | 60 | ```bash 61 | vite v2.6.5 dev server running at: 62 | 63 | > Local: http://localhost:3000/ 64 | 65 | ready in 169ms. 66 | ``` 67 | 68 | --- 69 | 70 | ## Install manually 71 | 72 | ```bash 73 | yarn add vite vite-plugin-wasm-pack -D 74 | # or 75 | # npm i vite vite-plugin-wasm-pack vite -D 76 | ``` 77 | 78 | ## Usage 79 | 80 | Add this plugin to `vite.config.ts` 81 | 82 | ```js 83 | import { defineConfig } from 'vite'; 84 | import wasmPack from 'vite-plugin-wasm-pack'; 85 | 86 | export default defineConfig({ 87 | // pass your local crate path to the plugin 88 | plugins: [wasmPack('./my-crate')] 89 | }); 90 | ``` 91 | 92 | Add script to `package.json` 93 | 94 | ```json 95 | "scripts": { 96 | "dev": "vite", 97 | "build": "vite build" 98 | } 99 | ``` 100 | 101 | ⚠ **Don't forget to build your [wasm-pack](https://github.com/rustwasm/wasm-pack) crate first!** 102 | 103 | ```bash 104 | wasm-pack build ./my-crate --target web 105 | ``` 106 | 107 | Tips: you can add a `wasm` script to `package.json` like this: 108 | 109 | ```json 110 | "scripts": { 111 | "wasm": "wasm-pack build ./my-crate --target web", 112 | "dev": "yarn wasm && vite", 113 | "build": "vite build" 114 | } 115 | ``` 116 | 117 | Then, run: 118 | 119 | ```bash 120 | yarn dev 121 | ``` 122 | 123 | This will start dev server, and install `my-crate` that you built earlier. 124 | 125 | ## Use wasm-pack package installed via npm 126 | 127 | If you want use a package from npm that built with wasm-pack, like this one [test-npm-crate](https://www.npmjs.com/package/test-npm-crate) 128 | 129 | you have to pass the package name to the second param of our plugin. 130 | 131 | `wasmPack(['./my-local-crate'],['test-npm-crate'])` 132 | 133 | full example is in [./npm-crate-example](./npm-crate-example) folder. 134 | 135 | notice, we only support package build with `--target web` for now , if a package you use is built without `--target web`, you should rebuild it. 136 | 137 | like this example [[Photon-vite]](http://github.nshen.net/photon-vite/) [[source]](https://github.com/nshen/photon-vite) 138 | 139 | ## Cache Problem 140 | 141 | I'm not sure if anybody have met the problem that after modified your package, your package don't update. 142 | 143 | That's becasue vite pre bundling your package, you can follow [vite guide]( 144 | https://vitejs.dev/guide/dep-pre-bundling.html) to solve the problem. 145 | 146 | If you met the problem , or any problems. feel free to [create an issue](https://github.com/nshen/vite-plugin-wasm-pack/issues), let me see if I can do something to help you, thanks. 147 | 148 | ## Examples 149 | 150 | - Quick starter is in [./example](./example) folder. 151 | - Game of life [[demo]](http://github.nshen.net/vite-wasm-game-of-life/dist/) [[source]](https://github.com/nshen/vite-wasm-game-of-life) 152 | - Photon-vite [[demo]](http://github.nshen.net/photon-vite/) [[source]](https://github.com/nshen/photon-vite) 153 | 154 | 155 | ## License 156 | 157 | MIT, see [the license file](./LICENSE) 158 | -------------------------------------------------------------------------------- /banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nshen/vite-plugin-wasm-pack/5e626b9d387b9e9df87712479df2eb5110af02f7/banner.png -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | vite-plugin-wasm-pack example 8 | 9 | 10 | 11 |

vite-plugin-wasm-pack example

12 |

Can you see an alert window?

13 | 14 | 15 | -------------------------------------------------------------------------------- /example/my-crate/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.7.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" 8 | 9 | [[package]] 10 | name = "cfg-if" 11 | version = "0.1.10" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 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 = "console_error_panic_hook" 23 | version = "0.1.6" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211" 26 | dependencies = [ 27 | "cfg-if 0.1.10", 28 | "wasm-bindgen", 29 | ] 30 | 31 | [[package]] 32 | name = "js-sys" 33 | version = "0.3.51" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" 36 | dependencies = [ 37 | "wasm-bindgen", 38 | ] 39 | 40 | [[package]] 41 | name = "lazy_static" 42 | version = "1.4.0" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 45 | 46 | [[package]] 47 | name = "libc" 48 | version = "0.2.97" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" 51 | 52 | [[package]] 53 | name = "log" 54 | version = "0.4.14" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" 57 | dependencies = [ 58 | "cfg-if 1.0.0", 59 | ] 60 | 61 | [[package]] 62 | name = "memory_units" 63 | version = "0.4.0" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" 66 | 67 | [[package]] 68 | name = "my-crate" 69 | version = "0.1.0" 70 | dependencies = [ 71 | "cfg-if 1.0.0", 72 | "console_error_panic_hook", 73 | "wasm-bindgen", 74 | "wasm-bindgen-test", 75 | "wee_alloc", 76 | ] 77 | 78 | [[package]] 79 | name = "proc-macro2" 80 | version = "1.0.27" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" 83 | dependencies = [ 84 | "unicode-xid", 85 | ] 86 | 87 | [[package]] 88 | name = "quote" 89 | version = "1.0.9" 90 | source = "registry+https://github.com/rust-lang/crates.io-index" 91 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 92 | dependencies = [ 93 | "proc-macro2", 94 | ] 95 | 96 | [[package]] 97 | name = "scoped-tls" 98 | version = "1.0.0" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" 101 | 102 | [[package]] 103 | name = "syn" 104 | version = "1.0.73" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" 107 | dependencies = [ 108 | "proc-macro2", 109 | "quote", 110 | "unicode-xid", 111 | ] 112 | 113 | [[package]] 114 | name = "unicode-xid" 115 | version = "0.2.2" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 118 | 119 | [[package]] 120 | name = "wasm-bindgen" 121 | version = "0.2.74" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" 124 | dependencies = [ 125 | "cfg-if 1.0.0", 126 | "wasm-bindgen-macro", 127 | ] 128 | 129 | [[package]] 130 | name = "wasm-bindgen-backend" 131 | version = "0.2.74" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" 134 | dependencies = [ 135 | "bumpalo", 136 | "lazy_static", 137 | "log", 138 | "proc-macro2", 139 | "quote", 140 | "syn", 141 | "wasm-bindgen-shared", 142 | ] 143 | 144 | [[package]] 145 | name = "wasm-bindgen-futures" 146 | version = "0.4.24" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | checksum = "5fba7978c679d53ce2d0ac80c8c175840feb849a161664365d1287b41f2e67f1" 149 | dependencies = [ 150 | "cfg-if 1.0.0", 151 | "js-sys", 152 | "wasm-bindgen", 153 | "web-sys", 154 | ] 155 | 156 | [[package]] 157 | name = "wasm-bindgen-macro" 158 | version = "0.2.74" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" 161 | dependencies = [ 162 | "quote", 163 | "wasm-bindgen-macro-support", 164 | ] 165 | 166 | [[package]] 167 | name = "wasm-bindgen-macro-support" 168 | version = "0.2.74" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" 171 | dependencies = [ 172 | "proc-macro2", 173 | "quote", 174 | "syn", 175 | "wasm-bindgen-backend", 176 | "wasm-bindgen-shared", 177 | ] 178 | 179 | [[package]] 180 | name = "wasm-bindgen-shared" 181 | version = "0.2.74" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" 184 | 185 | [[package]] 186 | name = "wasm-bindgen-test" 187 | version = "0.3.24" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | checksum = "8cab416a9b970464c2882ed92d55b0c33046b08e0bdc9d59b3b718acd4e1bae8" 190 | dependencies = [ 191 | "console_error_panic_hook", 192 | "js-sys", 193 | "scoped-tls", 194 | "wasm-bindgen", 195 | "wasm-bindgen-futures", 196 | "wasm-bindgen-test-macro", 197 | ] 198 | 199 | [[package]] 200 | name = "wasm-bindgen-test-macro" 201 | version = "0.3.24" 202 | source = "registry+https://github.com/rust-lang/crates.io-index" 203 | checksum = "dd4543fc6cf3541ef0d98bf720104cc6bd856d7eba449fd2aa365ef4fed0e782" 204 | dependencies = [ 205 | "proc-macro2", 206 | "quote", 207 | ] 208 | 209 | [[package]] 210 | name = "web-sys" 211 | version = "0.3.51" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" 214 | dependencies = [ 215 | "js-sys", 216 | "wasm-bindgen", 217 | ] 218 | 219 | [[package]] 220 | name = "wee_alloc" 221 | version = "0.4.5" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" 224 | dependencies = [ 225 | "cfg-if 0.1.10", 226 | "libc", 227 | "memory_units", 228 | "winapi", 229 | ] 230 | 231 | [[package]] 232 | name = "winapi" 233 | version = "0.3.9" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 236 | dependencies = [ 237 | "winapi-i686-pc-windows-gnu", 238 | "winapi-x86_64-pc-windows-gnu", 239 | ] 240 | 241 | [[package]] 242 | name = "winapi-i686-pc-windows-gnu" 243 | version = "0.4.0" 244 | source = "registry+https://github.com/rust-lang/crates.io-index" 245 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 246 | 247 | [[package]] 248 | name = "winapi-x86_64-pc-windows-gnu" 249 | version = "0.4.0" 250 | source = "registry+https://github.com/rust-lang/crates.io-index" 251 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 252 | -------------------------------------------------------------------------------- /example/my-crate/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "my-crate" 3 | version = "0.1.0" 4 | authors = ["Nshen "] 5 | edition = "2018" 6 | 7 | [lib] 8 | crate-type = ["cdylib", "rlib"] 9 | 10 | [dependencies] 11 | cfg-if = "1.0.0" 12 | wasm-bindgen = "0.2.63" 13 | 14 | # The `console_error_panic_hook` crate provides better debugging of panics by 15 | # logging them with `console.error`. This is great for development, but requires 16 | # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for 17 | # code size when deploying. 18 | console_error_panic_hook = { version = "0.1.6", optional = true } 19 | 20 | # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size 21 | # compared to the default allocator's ~10K. It is slower than the default 22 | # allocator, however. 23 | # 24 | # Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. 25 | wee_alloc = { version = "0.4.5", optional = true } 26 | 27 | [dev-dependencies] 28 | wasm-bindgen-test = "0.3.13" 29 | 30 | [profile.release] 31 | # Tell `rustc` to optimize for small code size. 32 | opt-level = "s" 33 | 34 | [features] 35 | # default = ["console_error_panic_hook" ,"wee_alloc"] 36 | default = ["console_error_panic_hook"] 37 | -------------------------------------------------------------------------------- /example/my-crate/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate cfg_if; 2 | extern crate wasm_bindgen; 3 | 4 | mod utils; 5 | 6 | use cfg_if::cfg_if; 7 | use wasm_bindgen::prelude::*; 8 | 9 | cfg_if! { 10 | if #[cfg(feature = "wee_alloc")] { 11 | extern crate wee_alloc; 12 | #[global_allocator] 13 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 14 | } 15 | } 16 | 17 | #[wasm_bindgen] 18 | extern "C" { 19 | fn alert(s: &str); 20 | } 21 | 22 | #[wasm_bindgen] 23 | pub fn greet(name: &str) { 24 | alert(&format!("Hello,{}!", name)); 25 | } 26 | -------------------------------------------------------------------------------- /example/my-crate/src/utils.rs: -------------------------------------------------------------------------------- 1 | pub fn set_panic_hook() { 2 | // When the `console_error_panic_hook` feature is enabled, we can call the 3 | // `set_panic_hook` function at least once during initialization, and then 4 | // we will get better error messages if our code ever panics. 5 | // 6 | // For more details see 7 | // https://github.com/rustwasm/console_error_panic_hook#readme 8 | #[cfg(feature = "console_error_panic_hook")] 9 | console_error_panic_hook::set_once(); 10 | } 11 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-plugin-wasm-pack-example", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "devDependencies": { 7 | "vite": "^2.6.14", 8 | "vite-plugin-wasm-pack": "^0.1.9" 9 | }, 10 | "scripts": { 11 | "wasm": "wasm-pack build ./my-crate --target web", 12 | "dev": "vite", 13 | "build": "vite build" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/src/index.ts: -------------------------------------------------------------------------------- 1 | //@ts-ignore 2 | import init, { greet } from 'my-crate'; 3 | // Don't worry if vscode told you can't find my-crate 4 | // It's because you're using a local crate 5 | // after yarn dev, wasm-pack plugin will install my-crate for you 6 | 7 | init().then(() => { 8 | console.log('init wasm-pack'); 9 | greet('from vite!'); 10 | }); 11 | -------------------------------------------------------------------------------- /example/vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import wasmPack from 'vite-plugin-wasm-pack'; 3 | 4 | export default defineConfig({ 5 | build: { 6 | minify: false 7 | }, 8 | plugins: [wasmPack(['./my-crate'])] 9 | }); 10 | -------------------------------------------------------------------------------- /npm-crate-example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | vite-plugin-wasm-pack example 8 | 9 | 10 | 11 |

vite-plugin-wasm-pack example

12 |

Can you see an alert window?

13 | 14 | 15 | -------------------------------------------------------------------------------- /npm-crate-example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-crate-example", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "devDependencies": { 7 | "vite": "^2.6.14", 8 | "vite-plugin-wasm-pack": "^0.1.9" 9 | }, 10 | "scripts": { 11 | "dev": "vite", 12 | "build": "vite build" 13 | }, 14 | "dependencies": { 15 | "test-npm-crate": "^0.3.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /npm-crate-example/src/index.ts: -------------------------------------------------------------------------------- 1 | import init, { greet } from 'test-npm-crate'; 2 | 3 | init().then(() => { 4 | greet(); 5 | }); 6 | -------------------------------------------------------------------------------- /npm-crate-example/vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import wasmPack from 'vite-plugin-wasm-pack'; 3 | 4 | export default defineConfig({ 5 | build: { 6 | minify: false 7 | }, 8 | // use test-npm-crate from https://www.npmjs.com/package/test-npm-crate 9 | // we have no local crate, so leave the first param an empty array 10 | plugins: [wasmPack([], ['test-npm-crate'])] 11 | }); 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-plugin-wasm-pack", 3 | "version": "0.1.12", 4 | "description": "Vite plugin for rust using wasm-pack 🦀", 5 | "main": "dist/index.js", 6 | "module": "esm/index.js", 7 | "types": "dist/index.d.ts", 8 | "files": [ 9 | "dist" 10 | ], 11 | "scripts": { 12 | "build": "tsc && tsc -p tsconfig.esm.json", 13 | "dev": "tsc -w" 14 | }, 15 | "keywords": [ 16 | "wasm-pack", 17 | "wasm-bindgen", 18 | "vite-plugin", 19 | "webassembly", 20 | "wasm", 21 | "rust", 22 | "vite", 23 | "cargo" 24 | ], 25 | "author": "nshen ", 26 | "license": "MIT", 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/nshen/vite-plugin-wasm-pack" 30 | }, 31 | "bugs": { 32 | "url": "https://github.com/nshen/vite-plugin-wasm-pack/issues" 33 | }, 34 | "devDependencies": { 35 | "@tsconfig/node12": "^1.0.9", 36 | "@types/fs-extra": "^9.0.13", 37 | "@types/node": "^12.0.0", 38 | "prettier": "^2.4.1", 39 | "typescript": "^4.4.4", 40 | "vite": "2.6.14" 41 | }, 42 | "dependencies": { 43 | "chalk": "^4.1.2", 44 | "fs-extra": "^10.0.0", 45 | "narrowing": "^1.4.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /pnpm-lock.yaml: -------------------------------------------------------------------------------- 1 | lockfileVersion: 5.3 2 | 3 | specifiers: 4 | '@tsconfig/node12': ^1.0.9 5 | '@types/fs-extra': ^9.0.13 6 | '@types/node': ^12.0.0 7 | chalk: ^4.1.2 8 | fs-extra: ^10.0.0 9 | narrowing: ^1.4.0 10 | prettier: ^2.4.1 11 | typescript: ^4.4.4 12 | vite: 2.6.14 13 | 14 | dependencies: 15 | chalk: 4.1.2 16 | fs-extra: 10.0.1 17 | narrowing: 1.4.0 18 | 19 | devDependencies: 20 | '@tsconfig/node12': 1.0.9 21 | '@types/fs-extra': 9.0.13 22 | '@types/node': 12.20.47 23 | prettier: 2.6.2 24 | typescript: 4.6.3 25 | vite: 2.6.14 26 | 27 | packages: 28 | 29 | /@tsconfig/node12/1.0.9: 30 | resolution: {integrity: sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==} 31 | dev: true 32 | 33 | /@types/fs-extra/9.0.13: 34 | resolution: {integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==} 35 | dependencies: 36 | '@types/node': 12.20.47 37 | dev: true 38 | 39 | /@types/node/12.20.47: 40 | resolution: {integrity: sha512-BzcaRsnFuznzOItW1WpQrDHM7plAa7GIDMZ6b5pnMbkqEtM/6WCOhvZar39oeMQP79gwvFUWjjptE7/KGcNqFg==} 41 | dev: true 42 | 43 | /ansi-styles/4.3.0: 44 | resolution: {integrity: sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==} 45 | engines: {node: '>=8'} 46 | dependencies: 47 | color-convert: 2.0.1 48 | dev: false 49 | 50 | /chalk/4.1.2: 51 | resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} 52 | engines: {node: '>=10'} 53 | dependencies: 54 | ansi-styles: 4.3.0 55 | supports-color: 7.2.0 56 | dev: false 57 | 58 | /color-convert/2.0.1: 59 | resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==} 60 | engines: {node: '>=7.0.0'} 61 | dependencies: 62 | color-name: 1.1.4 63 | dev: false 64 | 65 | /color-name/1.1.4: 66 | resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} 67 | dev: false 68 | 69 | /esbuild-android-arm64/0.13.15: 70 | resolution: {integrity: sha512-m602nft/XXeO8YQPUDVoHfjyRVPdPgjyyXOxZ44MK/agewFFkPa8tUo6lAzSWh5Ui5PB4KR9UIFTSBKh/RrCmg==} 71 | cpu: [arm64] 72 | os: [android] 73 | requiresBuild: true 74 | dev: true 75 | optional: true 76 | 77 | /esbuild-darwin-64/0.13.15: 78 | resolution: {integrity: sha512-ihOQRGs2yyp7t5bArCwnvn2Atr6X4axqPpEdCFPVp7iUj4cVSdisgvEKdNR7yH3JDjW6aQDw40iQFoTqejqxvQ==} 79 | cpu: [x64] 80 | os: [darwin] 81 | requiresBuild: true 82 | dev: true 83 | optional: true 84 | 85 | /esbuild-darwin-arm64/0.13.15: 86 | resolution: {integrity: sha512-i1FZssTVxUqNlJ6cBTj5YQj4imWy3m49RZRnHhLpefFIh0To05ow9DTrXROTE1urGTQCloFUXTX8QfGJy1P8dQ==} 87 | cpu: [arm64] 88 | os: [darwin] 89 | requiresBuild: true 90 | dev: true 91 | optional: true 92 | 93 | /esbuild-freebsd-64/0.13.15: 94 | resolution: {integrity: sha512-G3dLBXUI6lC6Z09/x+WtXBXbOYQZ0E8TDBqvn7aMaOCzryJs8LyVXKY4CPnHFXZAbSwkCbqiPuSQ1+HhrNk7EA==} 95 | cpu: [x64] 96 | os: [freebsd] 97 | requiresBuild: true 98 | dev: true 99 | optional: true 100 | 101 | /esbuild-freebsd-arm64/0.13.15: 102 | resolution: {integrity: sha512-KJx0fzEDf1uhNOZQStV4ujg30WlnwqUASaGSFPhznLM/bbheu9HhqZ6mJJZM32lkyfGJikw0jg7v3S0oAvtvQQ==} 103 | cpu: [arm64] 104 | os: [freebsd] 105 | requiresBuild: true 106 | dev: true 107 | optional: true 108 | 109 | /esbuild-linux-32/0.13.15: 110 | resolution: {integrity: sha512-ZvTBPk0YWCLMCXiFmD5EUtB30zIPvC5Itxz0mdTu/xZBbbHJftQgLWY49wEPSn2T/TxahYCRDWun5smRa0Tu+g==} 111 | cpu: [ia32] 112 | os: [linux] 113 | requiresBuild: true 114 | dev: true 115 | optional: true 116 | 117 | /esbuild-linux-64/0.13.15: 118 | resolution: {integrity: sha512-eCKzkNSLywNeQTRBxJRQ0jxRCl2YWdMB3+PkWFo2BBQYC5mISLIVIjThNtn6HUNqua1pnvgP5xX0nHbZbPj5oA==} 119 | cpu: [x64] 120 | os: [linux] 121 | requiresBuild: true 122 | dev: true 123 | optional: true 124 | 125 | /esbuild-linux-arm/0.13.15: 126 | resolution: {integrity: sha512-wUHttDi/ol0tD8ZgUMDH8Ef7IbDX+/UsWJOXaAyTdkT7Yy9ZBqPg8bgB/Dn3CZ9SBpNieozrPRHm0BGww7W/jA==} 127 | cpu: [arm] 128 | os: [linux] 129 | requiresBuild: true 130 | dev: true 131 | optional: true 132 | 133 | /esbuild-linux-arm64/0.13.15: 134 | resolution: {integrity: sha512-bYpuUlN6qYU9slzr/ltyLTR9YTBS7qUDymO8SV7kjeNext61OdmqFAzuVZom+OLW1HPHseBfJ/JfdSlx8oTUoA==} 135 | cpu: [arm64] 136 | os: [linux] 137 | requiresBuild: true 138 | dev: true 139 | optional: true 140 | 141 | /esbuild-linux-mips64le/0.13.15: 142 | resolution: {integrity: sha512-KlVjIG828uFPyJkO/8gKwy9RbXhCEUeFsCGOJBepUlpa7G8/SeZgncUEz/tOOUJTcWMTmFMtdd3GElGyAtbSWg==} 143 | cpu: [mips64el] 144 | os: [linux] 145 | requiresBuild: true 146 | dev: true 147 | optional: true 148 | 149 | /esbuild-linux-ppc64le/0.13.15: 150 | resolution: {integrity: sha512-h6gYF+OsaqEuBjeesTBtUPw0bmiDu7eAeuc2OEH9S6mV9/jPhPdhOWzdeshb0BskRZxPhxPOjqZ+/OqLcxQwEQ==} 151 | cpu: [ppc64] 152 | os: [linux] 153 | requiresBuild: true 154 | dev: true 155 | optional: true 156 | 157 | /esbuild-netbsd-64/0.13.15: 158 | resolution: {integrity: sha512-3+yE9emwoevLMyvu+iR3rsa+Xwhie7ZEHMGDQ6dkqP/ndFzRHkobHUKTe+NCApSqG5ce2z4rFu+NX/UHnxlh3w==} 159 | cpu: [x64] 160 | os: [netbsd] 161 | requiresBuild: true 162 | dev: true 163 | optional: true 164 | 165 | /esbuild-openbsd-64/0.13.15: 166 | resolution: {integrity: sha512-wTfvtwYJYAFL1fSs8yHIdf5GEE4NkbtbXtjLWjM3Cw8mmQKqsg8kTiqJ9NJQe5NX/5Qlo7Xd9r1yKMMkHllp5g==} 167 | cpu: [x64] 168 | os: [openbsd] 169 | requiresBuild: true 170 | dev: true 171 | optional: true 172 | 173 | /esbuild-sunos-64/0.13.15: 174 | resolution: {integrity: sha512-lbivT9Bx3t1iWWrSnGyBP9ODriEvWDRiweAs69vI+miJoeKwHWOComSRukttbuzjZ8r1q0mQJ8Z7yUsDJ3hKdw==} 175 | cpu: [x64] 176 | os: [sunos] 177 | requiresBuild: true 178 | dev: true 179 | optional: true 180 | 181 | /esbuild-windows-32/0.13.15: 182 | resolution: {integrity: sha512-fDMEf2g3SsJ599MBr50cY5ve5lP1wyVwTe6aLJsM01KtxyKkB4UT+fc5MXQFn3RLrAIAZOG+tHC+yXObpSn7Nw==} 183 | cpu: [ia32] 184 | os: [win32] 185 | requiresBuild: true 186 | dev: true 187 | optional: true 188 | 189 | /esbuild-windows-64/0.13.15: 190 | resolution: {integrity: sha512-9aMsPRGDWCd3bGjUIKG/ZOJPKsiztlxl/Q3C1XDswO6eNX/Jtwu4M+jb6YDH9hRSUflQWX0XKAfWzgy5Wk54JQ==} 191 | cpu: [x64] 192 | os: [win32] 193 | requiresBuild: true 194 | dev: true 195 | optional: true 196 | 197 | /esbuild-windows-arm64/0.13.15: 198 | resolution: {integrity: sha512-zzvyCVVpbwQQATaf3IG8mu1IwGEiDxKkYUdA4FpoCHi1KtPa13jeScYDjlW0Qh+ebWzpKfR2ZwvqAQkSWNcKjA==} 199 | cpu: [arm64] 200 | os: [win32] 201 | requiresBuild: true 202 | dev: true 203 | optional: true 204 | 205 | /esbuild/0.13.15: 206 | resolution: {integrity: sha512-raCxt02HBKv8RJxE8vkTSCXGIyKHdEdGfUmiYb8wnabnaEmHzyW7DCHb5tEN0xU8ryqg5xw54mcwnYkC4x3AIw==} 207 | hasBin: true 208 | requiresBuild: true 209 | optionalDependencies: 210 | esbuild-android-arm64: 0.13.15 211 | esbuild-darwin-64: 0.13.15 212 | esbuild-darwin-arm64: 0.13.15 213 | esbuild-freebsd-64: 0.13.15 214 | esbuild-freebsd-arm64: 0.13.15 215 | esbuild-linux-32: 0.13.15 216 | esbuild-linux-64: 0.13.15 217 | esbuild-linux-arm: 0.13.15 218 | esbuild-linux-arm64: 0.13.15 219 | esbuild-linux-mips64le: 0.13.15 220 | esbuild-linux-ppc64le: 0.13.15 221 | esbuild-netbsd-64: 0.13.15 222 | esbuild-openbsd-64: 0.13.15 223 | esbuild-sunos-64: 0.13.15 224 | esbuild-windows-32: 0.13.15 225 | esbuild-windows-64: 0.13.15 226 | esbuild-windows-arm64: 0.13.15 227 | dev: true 228 | 229 | /fs-extra/10.0.1: 230 | resolution: {integrity: sha512-NbdoVMZso2Lsrn/QwLXOy6rm0ufY2zEOKCDzJR/0kBsb0E6qed0P3iYK+Ath3BfvXEeu4JhEtXLgILx5psUfag==} 231 | engines: {node: '>=12'} 232 | dependencies: 233 | graceful-fs: 4.2.10 234 | jsonfile: 6.1.0 235 | universalify: 2.0.0 236 | dev: false 237 | 238 | /fsevents/2.3.2: 239 | resolution: {integrity: sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==} 240 | engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0} 241 | os: [darwin] 242 | requiresBuild: true 243 | dev: true 244 | optional: true 245 | 246 | /function-bind/1.1.1: 247 | resolution: {integrity: sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==} 248 | dev: true 249 | 250 | /graceful-fs/4.2.10: 251 | resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==} 252 | dev: false 253 | 254 | /has-flag/4.0.0: 255 | resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} 256 | engines: {node: '>=8'} 257 | dev: false 258 | 259 | /has/1.0.3: 260 | resolution: {integrity: sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==} 261 | engines: {node: '>= 0.4.0'} 262 | dependencies: 263 | function-bind: 1.1.1 264 | dev: true 265 | 266 | /is-core-module/2.8.1: 267 | resolution: {integrity: sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==} 268 | dependencies: 269 | has: 1.0.3 270 | dev: true 271 | 272 | /jsonfile/6.1.0: 273 | resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} 274 | dependencies: 275 | universalify: 2.0.0 276 | optionalDependencies: 277 | graceful-fs: 4.2.10 278 | dev: false 279 | 280 | /nanoid/3.3.2: 281 | resolution: {integrity: sha512-CuHBogktKwpm5g2sRgv83jEy2ijFzBwMoYA60orPDR7ynsLijJDqgsi4RDGj3OJpy3Ieb+LYwiRmIOGyytgITA==} 282 | engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} 283 | hasBin: true 284 | dev: true 285 | 286 | /narrowing/1.4.0: 287 | resolution: {integrity: sha512-TYkWGGMWqyiEhTx0KdGtf+dukZAc1fcY0sOpXWOFxvdBXsVpfdtYUnsfZtpPax3REgD7BxcBWkGByWgYrSkC9A==} 288 | dev: false 289 | 290 | /path-parse/1.0.7: 291 | resolution: {integrity: sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==} 292 | dev: true 293 | 294 | /picocolors/1.0.0: 295 | resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} 296 | dev: true 297 | 298 | /postcss/8.4.12: 299 | resolution: {integrity: sha512-lg6eITwYe9v6Hr5CncVbK70SoioNQIq81nsaG86ev5hAidQvmOeETBqs7jm43K2F5/Ley3ytDtriImV6TpNiSg==} 300 | engines: {node: ^10 || ^12 || >=14} 301 | dependencies: 302 | nanoid: 3.3.2 303 | picocolors: 1.0.0 304 | source-map-js: 1.0.2 305 | dev: true 306 | 307 | /prettier/2.6.2: 308 | resolution: {integrity: sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==} 309 | engines: {node: '>=10.13.0'} 310 | hasBin: true 311 | dev: true 312 | 313 | /resolve/1.22.0: 314 | resolution: {integrity: sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw==} 315 | hasBin: true 316 | dependencies: 317 | is-core-module: 2.8.1 318 | path-parse: 1.0.7 319 | supports-preserve-symlinks-flag: 1.0.0 320 | dev: true 321 | 322 | /rollup/2.70.1: 323 | resolution: {integrity: sha512-CRYsI5EuzLbXdxC6RnYhOuRdtz4bhejPMSWjsFLfVM/7w/85n2szZv6yExqUXsBdz5KT8eoubeyDUDjhLHEslA==} 324 | engines: {node: '>=10.0.0'} 325 | hasBin: true 326 | optionalDependencies: 327 | fsevents: 2.3.2 328 | dev: true 329 | 330 | /source-map-js/1.0.2: 331 | resolution: {integrity: sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==} 332 | engines: {node: '>=0.10.0'} 333 | dev: true 334 | 335 | /supports-color/7.2.0: 336 | resolution: {integrity: sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==} 337 | engines: {node: '>=8'} 338 | dependencies: 339 | has-flag: 4.0.0 340 | dev: false 341 | 342 | /supports-preserve-symlinks-flag/1.0.0: 343 | resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==} 344 | engines: {node: '>= 0.4'} 345 | dev: true 346 | 347 | /typescript/4.6.3: 348 | resolution: {integrity: sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==} 349 | engines: {node: '>=4.2.0'} 350 | hasBin: true 351 | dev: true 352 | 353 | /universalify/2.0.0: 354 | resolution: {integrity: sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==} 355 | engines: {node: '>= 10.0.0'} 356 | dev: false 357 | 358 | /vite/2.6.14: 359 | resolution: {integrity: sha512-2HA9xGyi+EhY2MXo0+A2dRsqsAG3eFNEVIo12olkWhOmc8LfiM+eMdrXf+Ruje9gdXgvSqjLI9freec1RUM5EA==} 360 | engines: {node: '>=12.2.0'} 361 | hasBin: true 362 | peerDependencies: 363 | less: '*' 364 | sass: '*' 365 | stylus: '*' 366 | peerDependenciesMeta: 367 | less: 368 | optional: true 369 | sass: 370 | optional: true 371 | stylus: 372 | optional: true 373 | dependencies: 374 | esbuild: 0.13.15 375 | postcss: 8.4.12 376 | resolve: 1.22.0 377 | rollup: 2.70.1 378 | optionalDependencies: 379 | fsevents: 2.3.2 380 | dev: true 381 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import chalk from 'chalk'; 2 | import fs from 'fs-extra'; 3 | import { isString } from 'narrowing'; 4 | import path from 'path'; 5 | import { PluginOption } from 'vite'; 6 | 7 | /** 8 | * return a Vite plugin for handling wasm-pack crate 9 | * 10 | * only use local crate 11 | * 12 | * import wasmPack from 'vite-plugin-wasm-pack'; 13 | * 14 | * plugins: [wasmPack(['./my-local-crate'])] 15 | * 16 | * only use npm crate, leave the first param to an empty array 17 | * 18 | * plugins: [wasmPack([],['test-npm-crate'])] 19 | * 20 | * use both local and npm crate 21 | * 22 | * plugins: [wasmPack(['./my-local-crate'],['test-npm-crate'])] 23 | * 24 | * @param crates local crates paths, if you only use crates from npm, leave an empty array here. 25 | * @param moduleCrates crates names from npm 26 | */ 27 | function vitePluginWasmPack( 28 | crates: string[] | string, 29 | moduleCrates?: string[] | string 30 | ): PluginOption { 31 | const prefix = '@vite-plugin-wasm-pack@'; 32 | const pkg = 'pkg'; // default folder of wasm-pack module 33 | let config_base: string; 34 | let config_assetsDir: string; 35 | const cratePaths: string[] = isString(crates) ? [crates] : crates; 36 | const modulePaths: string[] = !moduleCrates 37 | ? [] 38 | : isString(moduleCrates) 39 | ? [moduleCrates] 40 | : moduleCrates; 41 | // from ../../my-crate -> my_crate_bg.wasm 42 | const wasmFilename = (cratePath: string) => { 43 | return path.basename(cratePath).replace(/\-/g, '_') + '_bg.wasm'; 44 | }; 45 | type CrateType = { path: string; isNodeModule: boolean }; 46 | // wasmfileName : CrateType 47 | const wasmMap = new Map(); 48 | // 'my_crate_bg.wasm': {path:'../../my_crate/pkg/my_crate_bg.wasm', isNodeModule: false} 49 | cratePaths.forEach((cratePath) => { 50 | const wasmFile = wasmFilename(cratePath); 51 | wasmMap.set(wasmFile, { 52 | path: path.join(cratePath, pkg, wasmFile), 53 | isNodeModule: false 54 | }); 55 | }); 56 | // 'my_crate_bg.wasm': { path: 'node_modules/my_crate/my_crate_bg.wasm', isNodeModule: true } 57 | modulePaths.forEach((cratePath) => { 58 | const wasmFile = wasmFilename(cratePath); 59 | const wasmDirectory = path.dirname(require.resolve(cratePath)); 60 | wasmMap.set(wasmFile, { 61 | path: path.join(wasmDirectory, wasmFile), 62 | isNodeModule: true 63 | }); 64 | }); 65 | 66 | return { 67 | name: 'vite-plugin-wasm-pack', 68 | enforce: 'pre', 69 | configResolved(resolvedConfig) { 70 | config_base = resolvedConfig.base; 71 | config_assetsDir = resolvedConfig.build.assetsDir; 72 | }, 73 | 74 | resolveId(id: string) { 75 | for (let i = 0; i < cratePaths.length; i++) { 76 | if (path.basename(cratePaths[i]) === id) return prefix + id; 77 | } 78 | return null; 79 | }, 80 | 81 | async load(id: string) { 82 | if (id.indexOf(prefix) === 0) { 83 | id = id.replace(prefix, ''); 84 | const modulejs = path.join( 85 | './node_modules', 86 | id, 87 | id.replace(/\-/g, '_') + '.js' 88 | ); 89 | const code = await fs.promises.readFile(modulejs, { 90 | encoding: 'utf-8' 91 | }); 92 | return code; 93 | } 94 | }, 95 | 96 | async buildStart(_inputOptions) { 97 | const prepareBuild = async (cratePath: string, isNodeModule: boolean) => { 98 | const pkgPath = isNodeModule 99 | ? path.dirname(require.resolve(cratePath)) 100 | : path.join(cratePath, pkg); 101 | const crateName = path.basename(cratePath); 102 | if (!fs.existsSync(pkgPath)) { 103 | if (isNodeModule) { 104 | console.error( 105 | chalk.bold.red('Error: ') + 106 | `Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red( 107 | `npm install ${cratePath}` 108 | )} first` 109 | ); 110 | } else { 111 | console.error( 112 | chalk.bold.red('Error: ') + 113 | `Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red( 114 | `wasm-pack build ${cratePath} --target web` 115 | )} first` 116 | ); 117 | } 118 | } 119 | if (!isNodeModule) { 120 | // copy pkg generated by wasm-pack to node_modules 121 | try { 122 | await fs.copy(pkgPath, path.join('node_modules', crateName)); 123 | } catch (error) { 124 | this.error(`copy crates failed: ${error}`); 125 | } 126 | } 127 | // replace default load path with '/assets/xxx.wasm' 128 | const jsName = crateName.replace(/\-/g, '_') + '.js'; 129 | 130 | /** 131 | * if use node module and name is '@group/test' 132 | * cratePath === '@group/test' 133 | * crateName === 'test' 134 | */ 135 | 136 | let jsPath = path.join('./node_modules', crateName, jsName); 137 | if (isNodeModule) { 138 | jsPath = path.join(pkgPath, jsName); 139 | } 140 | const regex = /input = new URL\('(.+)'.+;/g; 141 | let code = fs.readFileSync(path.resolve(jsPath), { encoding: 'utf-8' }); 142 | code = code.replace(regex, (_match, group1) => { 143 | return `input = "${path.posix.join( 144 | config_base, 145 | config_assetsDir, 146 | group1 147 | )}"`; 148 | }); 149 | fs.writeFileSync(jsPath, code); 150 | }; 151 | 152 | for await (const cratePath of cratePaths) { 153 | await prepareBuild(cratePath, false); 154 | } 155 | 156 | for await (const cratePath of modulePaths) { 157 | await prepareBuild(cratePath, true); 158 | } 159 | }, 160 | 161 | configureServer({ middlewares }) { 162 | // send 'root/pkg/xxx.wasm' file to user 163 | middlewares.use((req, res, next) => { 164 | if (isString(req.url)) { 165 | const basename = path.basename(req.url); 166 | res.setHeader( 167 | 'Cache-Control', 168 | 'no-cache, no-store, must-revalidate' 169 | ); 170 | const entry = wasmMap.get(basename); 171 | if (basename.endsWith('.wasm') && entry) { 172 | res.writeHead(200, { 'Content-Type': 'application/wasm' }); 173 | fs.createReadStream(entry.path).pipe(res); 174 | } else { 175 | next(); 176 | } 177 | } 178 | }); 179 | }, 180 | 181 | buildEnd() { 182 | // copy xxx.wasm files to /assets/xxx.wasm 183 | wasmMap.forEach((crate, fileName) => { 184 | this.emitFile({ 185 | type: 'asset', 186 | fileName: `assets/${fileName}`, 187 | source: fs.readFileSync(crate.path) 188 | }); 189 | }); 190 | } 191 | }; 192 | } 193 | 194 | export default vitePluginWasmPack; 195 | 196 | // https://github.com/sveltejs/vite-plugin-svelte/issues/214 197 | if (typeof module !== 'undefined') { 198 | module.exports = vitePluginWasmPack; 199 | vitePluginWasmPack.default = vitePluginWasmPack; 200 | } 201 | -------------------------------------------------------------------------------- /tea.yaml: -------------------------------------------------------------------------------- 1 | # https://tea.xyz/what-is-this-file 2 | --- 3 | version: 1.0.0 4 | codeOwners: 5 | - '0x5763B690102Cd5167Ad247a598a13018B84fF77f' 6 | quorum: 1 7 | -------------------------------------------------------------------------------- /tsconfig.esm.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig", 3 | "compilerOptions": { 4 | "module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 5 | "outDir": "./esm" /* Redirect output structure to the directory. */ 6 | } 7 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "include": [ 4 | "src" 5 | ], 6 | "compilerOptions": { 7 | "outDir": "dist", 8 | "declaration": true, 9 | "esModuleInterop": true, 10 | "moduleResolution": "node", 11 | // "removeComments": true 12 | }, 13 | } 14 | --------------------------------------------------------------------------------