{"id":13718740,"url":"https://github.com/DrSensor/rollup-plugin-rust","last_synced_at":"2025-05-07T10:33:52.088Z","repository":{"id":32849892,"uuid":"144118692","full_name":"DrSensor/rollup-plugin-rust","owner":"DrSensor","description":"A rollup plugin that compile Rust code into WebAssembly modules","archived":false,"fork":false,"pushed_at":"2022-12-07T22:38:40.000Z","size":2067,"stargazers_count":41,"open_issues_count":30,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-05T01:33:05.510Z","etag":null,"topics":["my-experiment","rollup","rollup-plugin","rust","webassembly"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DrSensor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-09T07:48:51.000Z","updated_at":"2024-11-06T16:51:32.000Z","dependencies_parsed_at":"2023-01-14T22:25:54.410Z","dependency_job_id":null,"html_url":"https://github.com/DrSensor/rollup-plugin-rust","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frollup-plugin-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frollup-plugin-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frollup-plugin-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Frollup-plugin-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrSensor","download_url":"https://codeload.github.com/DrSensor/rollup-plugin-rust/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252859980,"owners_count":21815438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["my-experiment","rollup","rollup-plugin","rust","webassembly"],"created_at":"2024-08-03T01:00:36.844Z","updated_at":"2025-05-07T10:33:51.814Z","avatar_url":"https://github.com/DrSensor.png","language":"JavaScript","funding_links":[],"categories":["Plugins","Development Tools","webassembly"],"sub_categories":["Other File Imports","JavaScript Toolchains and Bundler Plugins"],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/rust-lang/rust\"\u003e\n    \u003cimg width=\"200\" height=\"200\" alt=\"binaryen logo\" src=\"https://www.rust-lang.org/logos/rust-logo-blk.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/rollup/rollup\"\u003e\n    \u003cimg width=\"200\" height=\"200\" alt=\"webpack logo\" src=\"https://rollupjs.org/logo.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![size][size]][size-url]\n[![npm][npm-download]][npm-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n[![cover][cover]][cover-url]\n\n\u003c!-- [![stencil][stencil]][stencil-url] --\u003e\n\n# rollup-plugin-rust\n\n\u003csup\u003e\u003csup\u003etl;dr -- see [examples](#examples)\u003c/sup\u003e\u003c/sup\u003e\n\nThis is a rollup plugin that loads Rust code so it can be interop with Javascript base project. Currently, the Rust code will be compiled as:\n\n- [x] WebAssembly module/instance\n- [ ] Node.js addon/ffi\n\n## Requirements\n\n\u003cul\u003e\n\u003cli\u003eNode v8 or later\u003c/li\u003e\n\u003cli\u003eRollup v0.64 or later\u003c/li\u003e\n\u003cli\u003e\u003cdetails\u003e\n\u003csummary\u003eRust v1.28.0 with wasm32-uknown-unknown installed\u003c/summary\u003e\n\n```console\nrustup default 1.28.0\nrustup target add wasm32-unknown-unknown\n```\n\n\u003c/details\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\nThis module requires a minimum of Node v8.9.0, Rollup v0.64.0, and Rust in [nightly channel][].\n\n## Getting Started\n\nTo begin, you'll need to install `rollup-plugin-rust`:\n\n```console\nnpm install rollup-plugin-rust --save-dev\n```\n\nThen add the plugin to your `rollup` config. For example:\n\n\u003cb\u003erollup.config.js\u003c/b\u003e\n\n```js\nimport rust from \"rollup-plugin-rust\";\n\nexport default [\n  {\n    input: \"src/main.js\",\n    output: {\n      file: \"dist.index.js\",\n      format: \"esm\"\n    },\n    plugins: [rust()]\n  }\n];\n```\n\n\u003cdetails\u003e\n\u003csummary\u003equick usage\u003c/summary\u003e\n\n\u003cb\u003elib.rs\u003c/b\u003e\n\n```rust\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    a + b\n}\n```\n\n\u003cb\u003eindex.js\u003c/b\u003e\n\n```js\nimport wasm from \"lib.rs\";\n\nexport async function increment(a) {\n  const { instance } = await wasm;\n  return instance.exports.add(1, a);\n}\n```\n\n\u003c/details\u003e\n\nAnd run `rollup` via your preferred method.\n\n## Options\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003eexport\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\nHow wasm code would be exported. This options is identical with [option `export` in webassembly-loader][webassembly-loader]. (see [examples](#examples))\n\n```js\n// in your rollup.config.js\n{\n  plugins: [rust({ export: \"instance\" })];\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003etarget\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `String`\n- Default: `wasm32-unknown-unknown`\n- Expected value: see [supported platform](https://forge.rust-lang.org/platform-support.html)\n\nThe Rust target to use. Currently it **only support [wasm related target](https://kripken.github.io/blog/binaryen/2018/04/18/rust-emscripten.html)**\n\n```js\n// in your rollup.config.js\n{\n  plugins: [rust({ target: \"wasm32-unknown-emscripten\" })];\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003erelease\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `Boolean`\n- Default: `true`\n\nWhether to compile the Rust code in debug or release mode.\n\n```js\n// in your rollup.config.js\n{\n  plugins: [rust({ release: false })]; // preserve debug symbol\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003einclude\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `Array\u003cstring\u003e` or `string`\n- Default: `['**/*.rs']`\n\nA single file, or array of files, to include when compiling.\n\n```js\n// in your rollup.config.js\n{\n  plugins: [\n    rust({\n      include: [\"src/**/*.rs\", \"test/**/*.rs\"]\n    })\n  ];\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003e\u003ccode\u003eexclude\u003c/code\u003e\u003c/b\u003e\u003c/summary\u003e\n\n- Type: `Array\u003cstring\u003e` or `string`\n- Default: `['node_modules/**', 'target/**']`\n\nA single file, or array of files, to exclude when linting.\n\n```js\n// in your rollup.config.js\n{\n  plugins: [\n    rust({\n      exclude: [\"**/node_modules/**\", \"**/target/**\", \"**/__caches__/**\"]\n    })\n  ];\n}\n```\n\n\u003c/details\u003e\n\n## Examples\n\nSee the test cases and example projects in [fixtures](./test/fixtures) and [examples](./examples/) for more insight.\n\n\u003cdetails\u003e\n\u003csummary\u003eTL;DR\u003c/summary\u003e\n\n### Given this Rust code\n\n\u003cb\u003elib.rs\u003c/b\u003e\n\n```rust\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    a + b\n}\n```\n\n\u003cb\u003eCargo.toml\u003c/b\u003e\n\n```toml\n[package]\nname = \"adder\"\nversion = \"0.1.0\"\nauthors = [\"Full Name \u003cemail@site.domain\u003e\"]\n\n[lib]\ncrate-type = [\"cdylib\"]\npath = \"lib.rs\"\n```\n\n### With options\n\n#### `{export: 'buffer'}`\n\n```js\nimport wasmCode from \"./lib.rs\";\n\nWebAssembly.compile(wasmCode).then(module =\u003e {\n  const instance = new WebAssembly.Instance(module);\n  console(instance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n#### `{export: 'module'}`\n\n```js\nimport wasmModule from \"./lib.rs\";\n\nconst instance = new WebAssembly.Instance(wasmModule);\nconsole(instance.exports.add(1, 2)); // 3\n```\n\n---\n\n#### `{export: 'instance'}`\n\n```js\nimport wasm from \"./lib.rs\";\n\nconsole(wasm.exports.add(1, 2)); // 3\n```\n\n---\n\n#### `{export: 'async'}`\n\n```rust\nextern {\n    fn hook(c: i32);\n}\n\n#[no_mangle]\npub fn add(a: i32, b: i32) -\u003e i32 {\n    hook(a + b)\n}\n```\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmInstantiate(importObject | undefined).then(({ instance, module }) =\u003e {\n  console(instance.exports.add(1, 2)); // 3\n\n  // create different instance, extra will be called in different environment\n  const differentInstance = new WebAssembly.Instance(module, {\n    env: {\n      hook: result =\u003e result * 2\n    }\n  });\n  console(differentInstance.exports.add(1, 2)); // 6\n});\n```\n\n---\n\n#### `{export: 'async-instance'}`\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmInstantiate(importObject | undefined).then(instance =\u003e {\n  console(instance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n#### `{export: 'async-module'}`\n\n```js\nimport wasmInstantiate from \"./lib.rs\";\n\nwasmCompile(importObject | undefined).then(module =\u003e {\n  const differentInstance = new WebAssembly.Instance(module);\n  console(differentInstance.exports.add(1, 2)); // 3\n});\n```\n\n---\n\n\u003c/details\u003e\n\n## You may also want to look at\n\n- [rs-jest](https://github.com/DrSensor/rs-jest)\n\n## Who use this?\n\n- [example-stencil-rust](https://github.com/DrSensor/example-stencil-rust)\n- [add yours 😉]\n\n## Contributing\n\n- [CONTRIBUTING.md](./.github/CONTRIBUTING.md) for how you can make contribution\n- [HACKING.md](./.github/HACKING.md) for technical details\n\n## Credits\n\n- [webassembly-loader](https://github.com/DrSensor/webassembly-loader)\n- [rust-native-wasm-loader](https://github.com/dflemstr/rust-native-wasm-loader)\n- [webpack-defaults](https://github.com/webpack-contrib/webpack-defaults)\n- [typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter)\n\n---\n\n## License\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FDrSensor%2Frollup-plugin-rust.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FDrSensor%2Frollup-plugin-rust?ref=badge_large)\n\n[webassembly-loader]: https://github.com/DrSensor/webassembly-loader#export\n[npm]: https://img.shields.io/npm/v/rollup-plugin-rust.svg\n[npm-url]: https://npmjs.com/package/rollup-plugin-rust\n[npm-download]: https://img.shields.io/npm/dm/rollup-plugin-rust.svg\n[deps]: https://david-dm.org/DrSensor/rollup-plugin-rust.svg\n[deps-url]: https://david-dm.org/DrSensor/rollup-plugin-rust\n[tests]: https://img.shields.io/circleci/project/github/DrSensor/rollup-plugin-rust.svg\n[tests-url]: https://circleci.com/gh/DrSensor/rollup-plugin-rust\n[stencil]: https://img.shields.io/travis/DrSensor/rollup-plugin-rust.svg?label=smoke%20stencil\n[stencil-url]: https://travis-ci.org/DrSensor/rollup-plugin-rust\n[cover]: https://codecov.io/gh/DrSensor/rollup-plugin-rust/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/DrSensor/rollup-plugin-rust\n[size]: https://packagephobia.now.sh/badge?p=rollup-plugin-rust\n[size-url]: https://packagephobia.now.sh/result?p=rollup-plugin-rust\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDrSensor%2Frollup-plugin-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDrSensor%2Frollup-plugin-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDrSensor%2Frollup-plugin-rust/lists"}