Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions packages/wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,31 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
return null;
},

load(id) {
load(id, loadOptions?: { rawId?: string; attributes?: Record<string, string> }) {
if (id === HELPERS_ID) {
return getHelpersModule(targetEnv);
}

if (!filter(id)) {
const rawId = (loadOptions && loadOptions.rawId) || id;
if (!filter(rawId)) {
return null;
}

if (!/\.wasm$/.test(id)) {
if (!/\.wasm$/.test(rawId)) {
return null;
}
this.addWatchFile(id);
this.addWatchFile(rawId);

return Promise.all([fs.promises.stat(id), fs.promises.readFile(id)]).then(
return Promise.all([fs.promises.stat(rawId), fs.promises.readFile(rawId)]).then(
([stats, buffer]) => {
if (targetEnv === 'auto-inline') {
return buffer.toString('binary');
}

if ((maxFileSize && stats.size > maxFileSize) || maxFileSize === 0) {
const hash = createHash('sha1').update(buffer).digest('hex').substr(0, 16);
const ext = path.extname(id);
const name = path.basename(id, ext);
const ext = path.extname(rawId);
const name = path.basename(rawId, ext);

const outputFileName = fileName
.replace(/\[hash\]/g, hash)
Expand All @@ -66,8 +67,8 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
const publicFilepath = `${publicPath}${outputFileName}`;

// only copy if the file is not marked `sync`, `sync` files are always inlined
if (syncFiles.indexOf(id) === -1) {
copies[id] = {
if (syncFiles.indexOf(rawId) === -1) {
copies[rawId] = {
filename: outputFileName,
publicFilepath,
buffer
Expand All @@ -80,14 +81,19 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
);
},

transform(code, id) {
if (!filter(id)) {
transform(
code,
id,
transformOptions?: { rawId?: string; attributes?: Record<string, string> }
) {
const rawId = (transformOptions && transformOptions.rawId) || id;
if (!filter(rawId)) {
return null;
}

if (code && /\.wasm$/.test(id)) {
const isSync = syncFiles.indexOf(id) !== -1;
const publicFilepath = copies[id] ? `'${copies[id].publicFilepath}'` : null;
if (code && /\.wasm$/.test(rawId)) {
const isSync = syncFiles.indexOf(rawId) !== -1;
const publicFilepath = copies[rawId] ? `'${copies[rawId].publicFilepath}'` : null;
let src;

if (publicFilepath === null) {
Expand Down
8 changes: 8 additions & 0 deletions packages/wasm/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const testBundle = async (t, bundle) => {
const func = new AsyncFunction('t', `let result;\n\n${code}\n\nreturn result;`);
return func(t);
};
test('loads and transforms wasm files imported with import attributes', async () => {
const rawId = join(DIRNAME, 'fixtures/sample.wasm');
const id = `${rawId}?attributes=${encodeURIComponent(JSON.stringify({ type: 'webassembly' }))}`;
const plugin = wasmPlugin();
const binary = await plugin.load.call({ addWatchFile() {} }, id, { rawId });
const { code } = plugin.transform.call({}, binary, id, { rawId });
expect(code).toContain('_loadWasmModule(0, null, ');
Comment on lines +33 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Other loading paths lack coverage

The new test only checks default inline loading. It never reaches the changed copies[rawId], filename, or syncFiles branches, so mistakes there can pass unnoticed. Add attributed-import cases with maxFileSize: 0 and with sync set. Check that the first emits and references the expected asset, while the second stays inline and generates a synchronous loader.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

});
test('async compiling', async () => {
expect.assertions(2);
const bundle = await rollup({
Expand Down
Loading