Skip to content

Commit 24f28bc

Browse files
hobofandevongovett
authored andcommitted
Add support for Cargo workspaces in Rust integration (#1488)
1 parent dc10531 commit 24f28bc

7 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/assets/RustAsset.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ class RustAsset extends Asset {
152152
await exec('cargo', args, {cwd: cargoDir});
153153

154154
// Get output file paths
155-
let outDir = path.join(cargoDir, 'target', RUST_TARGET, 'release');
155+
let [stdout] = await exec('cargo', ['metadata', '--format-version', '1'], {
156+
cwd: cargoDir
157+
});
158+
const cargoMetadata = JSON.parse(stdout);
159+
const cargoTargetDir = cargoMetadata.target_directory;
160+
let outDir = path.join(cargoTargetDir, RUST_TARGET, 'release');
156161

157162
// Rust converts '-' to '_' when outputting files.
158163
let rustName = cargoConfig.package.name.replace(/-/g, '_');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../.eslintrc.json",
3+
"parserOptions": {
4+
"sourceType": "module"
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"member"
4+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "member"
3+
version = "0.1.0"
4+
authors = ["josealbizures <albizures3601@gmail.com>"]
5+
6+
[dependencies]
7+
8+
[lib]
9+
crate-type = ["cdylib"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = import('./lib.rs').then(function ({add}) {
2+
return add(2, 3);
3+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub fn add(a: i32, b: i32) -> i32 {
3+
return a + b
4+
}

test/rust.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ describe('rust', function() {
133133
assert.equal(res, 5);
134134
});
135135

136+
it('should generate a wasm file from a rust file in cargo workspace', async function() {
137+
this.timeout(500000);
138+
let b = await bundle(
139+
__dirname + '/integration/rust-cargo-workspace/member/src/index.js'
140+
);
141+
142+
await assertBundleTree(b, {
143+
name: 'index.js',
144+
assets: [
145+
'bundle-loader.js',
146+
'bundle-url.js',
147+
'index.js',
148+
'wasm-loader.js'
149+
],
150+
childBundles: [
151+
{
152+
type: 'map'
153+
},
154+
{
155+
type: 'wasm',
156+
assets: ['lib.rs'],
157+
childBundles: []
158+
}
159+
]
160+
});
161+
162+
var res = await run(b);
163+
assert.equal(res, 5);
164+
});
165+
136166
it('should use wasm-gc to minify output', async function() {
137167
this.timeout(500000);
138168

0 commit comments

Comments
 (0)