Skip to content

Commit

Permalink
feat: make cache directory configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra authored and jantimon committed Aug 14, 2019
1 parent f2758ca commit 43a5aef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ plugins: [
new FaviconsWebpackPlugin({
// Your source logo (required)
logo: '/path/to/logo.png',
// The prefix for all image files (might be a folder or a name)
// Path to store cached data or false/null to disable caching altogether
// Note: disabling caching may increase build times considerably
cache: '.wwp-cache',
// Prefix for generated assets, might be a folder or just a name prefix
prefix: 'assets-[hash]/',
// Inject html links/metadata (requires html-webpack-plugin)
inject: true,
Expand Down
12 changes: 7 additions & 5 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const msgpack = require('msgpack-lite');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const {getAssetPath} = require('./compat');

module.exports.run = ({prefix, favicons: options, logo}, context, compilation) => {
module.exports.run = ({prefix, favicons: options, logo, cache: cacheDirectory}, context, compilation) => {
// The entry file is just an empty helper
const filename = '[hash]';
const publicPath = compilation.outputOptions.publicPath;
Expand All @@ -15,11 +15,13 @@ module.exports.run = ({prefix, favicons: options, logo}, context, compilation) =
const compiler = compilation.createChildCompiler('favicons-webpack-plugin', {filename, publicPath});
compiler.context = context;

const cacheDirectory = path.resolve(context, '.wwp-cache');
const cache = `${require.resolve('cache-loader')}?${JSON.stringify({cacheDirectory})}`;
const loader = `${require.resolve('./loader')}?${JSON.stringify({prefix, options})}`;
const loader = `!${require.resolve('./loader')}?${JSON.stringify({prefix, options})}`;
const cache = cacheDirectory
? `!${require.resolve('cache-loader')}?${JSON.stringify({cacheDirectory})}`
: ''
;

new SingleEntryPlugin(context, `!!${cache}!${loader}!${logo}`).apply(compiler);
new SingleEntryPlugin(context, `!${cache}${loader}!${logo}`).apply(compiler);

// Compile and return a promise
return new Promise((resolve, reject) => {
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module.exports = class FaviconsWebpackPlugin {
assert(typeof options === 'object' && typeof options.logo === 'string', 'An input file is required');

this.options = Object.assign({
cache: '.wwp-cache',
prefix: 'assets-[hash]/',
favicons: {},
inject: true,
}, options);
}

apply(compiler) {
const oracle = new Oracle(compiler.context)
const oracle = new Oracle(compiler.context);

if (this.options.cache) {
this.options.cache = path.resolve(compiler.context, this.options.cache);
}

{
const {
Expand Down

0 comments on commit 43a5aef

Please sign in to comment.