Skip to content

Commit

Permalink
fix: fix hash generation for light-mode
Browse files Browse the repository at this point in the history
fix #159
  • Loading branch information
jantimon committed Aug 16, 2019
1 parent 3fe5163 commit 67a8850
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const child = require('./compiler');
const Oracle = require('./oracle');
const { tap, tapHtml, getAssetPath } = require('./compat');
const path = require('path');
const crypto = require('crypto');

const faviconCompilations = new WeakMap();

Expand Down Expand Up @@ -105,13 +106,20 @@ module.exports = class FaviconsWebpackPlugin {
return new Promise((resolve, reject) => {
const logoFileName = path.resolve(compilation.compiler.context, this.options.logo);
const webpackPublicPath = compilation.outputOptions.publicPath || '/';
const outputPath = webpackPublicPath + getAssetPath(compilation, this.options.prefix, {hash: compilation.hash, chunk: {}});
const faviconExt = path.extname(this.options.logo);
const logoOutputPath = outputPath + 'favicon' + faviconExt;
// Copy file to output directory
compiler.inputFileSystem.readFile(logoFileName, (err, content) => {
if (err) {
return reject(err);
}
const hasher = crypto.createHash("md5");
hasher.update(content.toString('utf8'));
const hash = hasher.digest('hex');
const outputPath = webpackPublicPath + getAssetPath(compilation, this.options.prefix, {hash, chunk: {
hash: hash,
contentHash: hash
}});
const logoOutputPath = outputPath + (outputPath.substr(-1) === '/' ? '' : '/') + 'favicon' + faviconExt;
compilation.assets[logoOutputPath] = {
source: () => content,
size: () => content.length
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/expected/prefixedlight/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html><head><link rel="icon" href="/custom/prefix/98487f08/favicon.png"></head><body></body></html>
1 change: 1 addition & 0 deletions test/fixtures/expected/prefixedlight/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/prefixed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@ test('should allow configuring the output prefix', async t => {
t.deepEqual(await compare(dist, path.resolve(expected, 'prefixed')), []);
});

test('should allow configuring the output prefix for light mode', async t => {
const dist = path.join(t.context.root, 'dist');
await generate({
context: t.context.root,
output: {
path: dist,
},
plugins: [
new HtmlWebpackPlugin(),
new FaviconsWebpackPlugin({ logo, mode:'light', prefix: 'custom/prefix/[hash:8]' }),
],
});

t.deepEqual(await compare(dist, path.resolve(expected, 'prefixedlight')), []);
});

test.afterEach(t => fs.remove(t.context.root));

0 comments on commit 67a8850

Please sign in to comment.