/
rust.js
210 lines (185 loc) Β· 4.86 KB
/
rust.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const assert = require('assert');
const {bundle, bundler, run, assertBundleTree} = require('./utils');
const fs = require('../src/utils/fs');
const commandExists = require('command-exists');
describe('rust', function() {
if (typeof WebAssembly === 'undefined' || !commandExists.sync('rustup')) {
// eslint-disable-next-line no-console
console.log(
'Skipping Rust tests. Install https://www.rustup.rs/ to run them.'
);
return;
}
it('should generate a wasm file from a rust file with rustc with --target=browser', async function() {
this.timeout(500000);
let b = await bundle(__dirname + '/integration/rust/index.js');
await assertBundleTree(b, {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'wasm',
assets: ['add.rs'],
childBundles: []
},
{
type: 'map'
}
]
});
var res = await await run(b);
assert.equal(res, 5);
// not minified
assert((await fs.stat(Array.from(b.childBundles)[0].name)).size > 500);
});
it('should generate a wasm file from a rust file with rustc with --target=node', async function() {
this.timeout(500000);
let b = await bundle(__dirname + '/integration/rust/index.js', {
target: 'node'
});
await assertBundleTree(b, {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'wasm',
assets: ['add.rs'],
childBundles: []
},
{
type: 'map'
}
]
});
var res = await run(b);
assert.equal(res, 5);
// not minified
assert((await fs.stat(Array.from(b.childBundles)[0].name)).size > 500);
});
it('should support rust files with dependencies via rustc', async function() {
this.timeout(500000);
let b = bundler(__dirname + '/integration/rust-deps/index.js');
let bundle = await b.bundle();
await assertBundleTree(bundle, {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'map'
},
{
type: 'wasm',
assets: ['test.rs'],
childBundles: []
}
]
});
var res = await run(bundle);
assert.equal(res, 10);
});
it('should generate a wasm file from a rust file with cargo', async function() {
this.timeout(500000);
let b = await bundle(__dirname + '/integration/rust-cargo/src/index.js');
await assertBundleTree(b, {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'map'
},
{
type: 'wasm',
assets: ['lib.rs'],
childBundles: []
}
]
});
var res = await run(b);
assert.equal(res, 5);
});
it('should generate a wasm file from a rust file in cargo workspace', async function() {
this.timeout(500000);
let b = await bundle(
__dirname + '/integration/rust-cargo-workspace/member/src/index.js'
);
await assertBundleTree(b, {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'map'
},
{
type: 'wasm',
assets: ['lib.rs'],
childBundles: []
}
]
});
var res = await run(b);
assert.equal(res, 5);
});
it('should use wasm-gc to minify output', async function() {
this.timeout(500000);
// Store the size of not minified bundle in order to test it against
// the size of minified one.
let b = await bundle(__dirname + '/integration/rust/index.js', {
minify: false,
sourceMaps: false
});
const size = (await fs.stat(Array.from(b.childBundles)[0].name)).size;
let bMinified = await bundle(__dirname + '/integration/rust/index.js', {
minify: true,
sourceMaps: false
});
const bundleTree = {
name: 'index.js',
assets: [
'bundle-loader.js',
'bundle-url.js',
'index.js',
'wasm-loader.js'
],
childBundles: [
{
type: 'wasm',
assets: ['add.rs'],
childBundles: []
}
]
};
await assertBundleTree(b, bundleTree);
await assertBundleTree(bMinified, bundleTree);
var res = await run(bMinified);
assert.equal(res, 5);
const sizeMinified = (await fs.stat(
Array.from(bMinified.childBundles)[0].name
)).size;
assert(sizeMinified < size);
});
});