Skip to content

Commit

Permalink
allow setAssetSource being called in generateBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored and lukastaegert committed Jun 11, 2018
1 parent 4704995 commit 3e3e260
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/assetHooks.ts
Expand Up @@ -60,7 +60,7 @@ export function createAssetPluginHooks(
});
const assetId = randomHexString(8);
const asset: Asset = { name, source, fileName: undefined };
if (outputBundle) finaliseAsset(asset, outputBundle, assetFileNames);
if (outputBundle && source !== undefined) finaliseAsset(asset, outputBundle, assetFileNames);
assetsById.set(assetId, asset);
return assetId;
},
Expand All @@ -71,7 +71,7 @@ export function createAssetPluginHooks(
code: 'ASSET_NOT_FOUND',
message: `Plugin error - Unable to set asset source for unknown asset ${assetId}.`
});
if (asset.source)
if (asset.source !== undefined)
error({
code: 'ASSET_SOURCE_ALREADY_SET',
message: `Plugin error - Unable to set asset source for ${
Expand Down
27 changes: 27 additions & 0 deletions test/hooks/index.js
Expand Up @@ -383,6 +383,33 @@ module.exports = input;
});
});

it('allows setting asset source at generateBundle', () => {
let assetId;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transform () {
return '';
},
generateBundle () {
assetId = this.emitAsset('test.ext');
this.setAssetSource(assetId, 'hello world');
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.then(({ output }) => {
assert.equal(output['assets/test-19916f7d.ext'], 'hello world');
});
});

it('throws when emitting assets too late', () => {
let calledHook = false;
return rollup
Expand Down

0 comments on commit 3e3e260

Please sign in to comment.