Skip to content

Commit

Permalink
fix: prevent recompilations
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jan 24, 2021
1 parent 04ca36a commit af5897a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const faviconCache = new WeakMap();
*
* @template TResult
*
* @param {string[]} files
* @param {string[]} absoluteFilePaths - file paths used used by the generator
* @param {any} pluginInstance - the plugin instance to use as cache key
* @param {boolean} useWebpackCache - Support webpack built in cache
* @param {WebpackCompilation} compilation - the current webpack compilation
Expand All @@ -31,7 +31,7 @@ const faviconCache = new WeakMap();
* @returns {Promise<TResult>}
*/
function runCached(
files,
absoluteFilePaths,
pluginInstance,
useWebpackCache,
compilation,
Expand All @@ -52,7 +52,7 @@ function runCached(
faviconCache.delete(latestSnapShot);

return runCached(
files,
absoluteFilePaths,
pluginInstance,
useWebpackCache,
compilation,
Expand All @@ -71,7 +71,7 @@ function runCached(
// to find out if the logo was changed
const newSnapShot = createSnapshot(
{
fileDependencies: files.filter(Boolean),
fileDependencies: absoluteFilePaths.filter(Boolean),
contextDependencies: [],
missingDependencies: []
},
Expand All @@ -81,8 +81,8 @@ function runCached(

// Start generating the favicons
const faviconsGenerationsPromise = useWebpackCache
? runWithFileCache(files, compilation, idGenerator, eTags, generator)
: readFiles(files, compilation).then(fileContents =>
? runWithFileCache(absoluteFilePaths, compilation, idGenerator, eTags, generator)
: readFiles(absoluteFilePaths, compilation).then(fileContents =>
generator(fileContents, idGenerator(fileContents))
);

Expand Down Expand Up @@ -153,24 +153,24 @@ async function runWithFileCache(
/**
* readFiles and get content hashes
*
* @param {string[]} files
* @param {string[]} absoluteFilePaths
* @param {WebpackCompilation} compilation
* @returns {Promise<{filePath: string, hash: string, content: Buffer}[]>}
*/
function readFiles(files, compilation) {
function readFiles(absoluteFilePaths, compilation) {
return Promise.all(
files.map(filePath =>
!filePath
? { filePath, hash: '', content: '' }
absoluteFilePaths.map(absoluteFilePath =>
!absoluteFilePath
? { filePath: absoluteFilePath, hash: '', content: '' }
: new Promise((resolve, reject) =>
compilation.inputFileSystem.readFile(
path.resolve(compilation.compiler.context, filePath),
path.resolve(compilation.compiler.context, absoluteFilePath),
(error, fileBuffer) => {
if (error) {
reject(error);
} else {
resolve({
filePath,
filePath: absoluteFilePath,
hash: getContentHash(fileBuffer),
content: fileBuffer
});
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class FaviconsWebpackPlugin {
typeof this.options.logo === 'string',
'Could not find `logo.png` for the current webpack context'
);
} else {
this.options.logo = path.resolve(compiler.context, this.options.logo);
}

if (typeof this.options.manifest === 'string') {
this.options.manifest = path.resolve(compiler.context, this.options.manifest);
}

// Hook into the webpack compilation
Expand Down
2 changes: 1 addition & 1 deletion src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* @param {WebpackCompilation} compilation
*/
const webpackLogger = compilation =>
compilation.compiler.getInfrastructureLogger('favicons-webpack-plugin');
compilation.getLogger('favicons-webpack-plugin');

module.exports = { webpackLogger };

0 comments on commit af5897a

Please sign in to comment.