Skip to content

Commit

Permalink
Fix crash when calling emitFile for chunks without a name
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Aug 31, 2021
1 parent 32a42ec commit 0c62f25
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-planets-jam.md
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Fix crash when calling `emitFile` for chunks without a name
13 changes: 9 additions & 4 deletions packages/wmr/src/lib/rollup-plugin-container.js
Expand Up @@ -124,15 +124,20 @@ export function createPluginContainer(plugins, opts = {}) {
return mod.info;
},
emitFile(assetOrFile) {
const { type, name, fileName } = assetOrFile;
const source = assetOrFile.type === 'asset' && assetOrFile.source;
const { type, fileName } = assetOrFile;
const name = type === 'chunk' ? assetOrFile.name || assetOrFile.id : assetOrFile.name;
const source = type === 'asset' && assetOrFile.source;
const id = String(++ids);
const filename = fileName || generateFilename({ type, name, source, fileName });
files.set(id, { id, name, filename });
if (source) {
if (type === 'chunk') {

if (type === 'chunk') {
if (source) {
throw Error(`emitFile({ type:"chunk" }) cannot include a source`);
}

// TODO: We probably need to process the chunk manually from here
} else if (source) {
if (opts.writeFile) opts.writeFile(filename, source);
else fs.writeFile(filename, source);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/wmr/test/fixtures/plugin-emit/public/index.html
@@ -0,0 +1,2 @@
<h1>it doesn't work</h1>
<script type="module" src="index.js"></script>
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/plugin-emit/public/index.js
@@ -0,0 +1 @@
console.log('hey');
3 changes: 3 additions & 0 deletions packages/wmr/test/fixtures/plugin-emit/public/worker.js
@@ -0,0 +1,3 @@
addEventListener('message', () => {
postMessage('it works');
});
25 changes: 25 additions & 0 deletions packages/wmr/test/fixtures/plugin-emit/wmr.config.mjs
@@ -0,0 +1,25 @@
export default function foo() {
return [
{
name: 'plugin-a',
async transform(code, id) {
if (!id.endsWith('index.js')) return;

const chunkRefId = this.emitFile({
id: 'public/worker.js',
type: 'chunk'
});

return {
code: `const url = new URL(import.meta.ROLLUP_FILE_URL_${chunkRefId}, import.meta.url);
const worker = new Worker(url, { type: "module" });
worker.addEventListener("message", e => {
document.querySelector("h1").textContent = e.data;
});
worker.postMessage("hello");`,
map: null
};
}
}
];
}
10 changes: 10 additions & 0 deletions packages/wmr/test/plugins/plugins.test.js
Expand Up @@ -55,4 +55,14 @@ describe('config', () => {
await waitForMessage(instance.output, /oh no #3/);
});
});

it('should allow calls to emitFile without name', async () => {
await loadFixture('plugin-emit', env);
instance = await runWmrFast(env.tmp.path);
await getOutput(env, instance);
await withLog(instance.output, async () => {
await env.page.goto(await instance.address, { waitUntil: ['domcontentloaded', 'networkidle2'] });
expect(await env.page.content()).toMatch(/it works/);
});
});
});

0 comments on commit 0c62f25

Please sign in to comment.