Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple assets to map to the same file on disk #73

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/WebpackAssetsManifest.js
Expand Up @@ -82,7 +82,7 @@ class WebpackAssetsManifest
// This is what gets JSON stringified
this.assets = this.options.assets;

// hashed filename : original filename
// original filename : hashed filename
this.assetNames = new Map();

// This is passed to the customize() hook
Expand Down Expand Up @@ -357,7 +357,7 @@ class WebpackAssetsManifest
maybeArrayWrap( assets[ chunkName ] )
.filter( f => ! this.isHMR(f) ) // Remove hot module replacement files
.forEach( filename => {
this.assetNames.set( filename, chunkName + this.getExtension( filename ) );
this.assetNames.set( chunkName + this.getExtension( filename ), filename );
});
});

Expand Down Expand Up @@ -449,7 +449,7 @@ class WebpackAssetsManifest

this.processAssetsByChunkName( this.stats.assetsByChunkName );

for ( const [ hashedFile, filename ] of this.assetNames ) {
for ( const [ filename, hashedFile ] of this.assetNames ) {
this.currentAsset = compilation.assets[ hashedFile ];

// `integrity` may have already been set by another plugin, like `webpack-subresource-integrity`.
Expand Down Expand Up @@ -511,7 +511,7 @@ class WebpackAssetsManifest
*/
handleAfterEmit(compilation)
{
// Reset the internal mapping of hashed name to original name after every compilation.
// Reset the internal mapping of original name to hashed name after every compilation.
this.assetNames.clear();

if ( ! this.options.writeToDisk ) {
Expand Down Expand Up @@ -542,13 +542,15 @@ class WebpackAssetsManifest
loaderContext.emitFile = (...args) => {
const [ name ] = args;

if ( ! this.assetNames.has( name ) ) {
const originalName = path.join(
path.dirname(name),
path.basename(module.userRequest)
);
const originalName = path.join(
path.dirname(name),
path.basename(module.userRequest)
);

this.assetNames.set(name, originalName);
// ignore emitFile calls from mini-css-extract-plugin
// https://github.com/webpack-contrib/mini-css-extract-plugin/pull/177
if (!module.identifier().includes('/mini-css-extract-plugin/')) {
this.assetNames.set(originalName, name);
}

return emitFile.apply(module, args);
Expand Down
12 changes: 9 additions & 3 deletions test/WebpackAssetsManifest-test.js
Expand Up @@ -1064,7 +1064,13 @@ describe('WebpackAssetsManifest', function() {
function(err, content) {
assert.isNull(err, 'Error found reading manifest.json');

assert.include(content.toString(), 'images/Ginger.jpg');
const manifest = JSON.parse(content.toString());
const manifestKeys = Object.keys(manifest);

// despite being emitted to the same file, both image names should appear in manifest
assert.equal(manifest['images/Ginger.jpg'], manifest['images/Ginger-duplicate.jpg']);
assert.include(manifestKeys, 'images/Ginger.jpg');
assert.include(manifestKeys, 'images/Ginger-duplicate.jpg');

done();
}
Expand Down Expand Up @@ -1277,9 +1283,9 @@ describe('WebpackAssetsManifest', function() {
main: [ 'main.123456.js', '0.123456.hot-update.js' ],
});

assert.equal( manifest.assetNames.get('main.123456.js'), 'main.js' );
assert.equal( manifest.assetNames.get('main.js'), 'main.123456.js' );

assert.isFalse( manifest.assetNames.has('0.123456.hot-update.js') );
assert.notInclude( Object.values(manifest.assetNames), '0.123456.hot-update.js' );
});

it('isHMR should return false when hotUpdateChunkFilename is ambiguous', function() {
Expand Down
Binary file added test/fixtures/Ginger-duplicate.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/fixtures/client.js
@@ -1,3 +1,5 @@
console.log('Client');

// despite having the same hash, both files should appear in manifest
require('./Ginger.jpg');
require('./Ginger-duplicate.jpg');
2 changes: 1 addition & 1 deletion test/fixtures/configs.js
Expand Up @@ -58,7 +58,7 @@ function client()
rules: [
{
test: /\.jpg$/i,
loader: 'file-loader?name=images/[name].[ext]',
loader: 'file-loader?name=images/[hash].[ext]',
},
],
},
Expand Down