Skip to content

Commit

Permalink
Lift common code
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Jun 8, 2023
1 parent 1179c1b commit 6331e91
Showing 1 changed file with 33 additions and 44 deletions.
77 changes: 33 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,20 @@ class FaviconsWebpackPlugin {
logoFileSources,
logoMaskableFileSources,
manifestFileSource.content,
compilation,
outputPath
);
compilation
).then((favicons) => {
const RawSource = compilation.compiler.webpack.sources.RawSource;

return {
...favicons,
assets: favicons.assets.map((asset) => ({
name: outputPath
? path.join(outputPath, asset.name)
: asset.name,
contents: new RawSource(asset.contents, false),
})),
};
});
}
);

Expand Down Expand Up @@ -342,14 +353,12 @@ class FaviconsWebpackPlugin {
* @param {{content: Buffer | string, hash: string}[]} logoMaskableFileSources
* @param {Buffer | string} baseManifest - the content of the file from options.manifest
* @param {import('webpack').Compilation} compilation
* @param {string} outputPath
*/
generateFavicons(
logoFileSources,
logoMaskableFileSources,
baseManifest,
compilation,
outputPath
compilation
) {
const logoFileSourceContents = logoFileSources.map(
(source) => source.content
Expand Down Expand Up @@ -386,9 +395,7 @@ class FaviconsWebpackPlugin {
logoFileSourceContents,
logoMaskableFileSourceContents,
parsedBaseManifest,
compilation,
resolvedPublicPath,
outputPath
resolvedPublicPath
);
case 'webapp':
default:
Expand All @@ -398,9 +405,7 @@ class FaviconsWebpackPlugin {
logoFileSourceContents,
logoMaskableFileSourceContents,
parsedBaseManifest,
compilation,
resolvedPublicPath,
outputPath
resolvedPublicPath
);
}
}
Expand All @@ -413,48 +418,40 @@ class FaviconsWebpackPlugin {
* @param {(Buffer | string)[]} logoFileSources
* @param {(Buffer | string)[]} logoMaskableFileSources
* @param {{[key: string]: any}} baseManifest
* @param {import('webpack').Compilation} compilation
* @param {string} resolvedPublicPath
* @param {string} outputPath
*/
async generateFaviconsLight(
logoFileSources,
logoMaskableFileSources,
baseManifest,
compilation,
resolvedPublicPath,
outputPath
resolvedPublicPath
) {
const faviconExt = path.extname(this.options.logo[0]);
const faviconName = `favicon${faviconExt}`;
const RawSource = compilation.compiler.webpack.sources.RawSource;

const tags = [`<link rel="icon" href="${faviconName}">`];
const assets = [
{
name: path.join(outputPath, faviconName),
contents: new RawSource(logoFileSources[0], false),
name: faviconName,
contents: logoFileSources[0],
},
];

// If the manifest is not empty add it also to the light mode
if (Object.keys(baseManifest).length > 0) {
tags.push('<link rel="manifest" href="manifest.webmanifest">');
assets.push({
name: path.join(outputPath, 'manifest.webmanifest'),
contents: new RawSource(
JSON.stringify(
mergeManifests(baseManifest, {
icons: [
{
src: faviconName,
},
],
}),
null,
2
),
false
name: 'manifest.webmanifest',
contents: JSON.stringify(
mergeManifests(baseManifest, {
icons: [
{
src: faviconName,
},
],
}),
null,
2
),
});
}
Expand All @@ -474,19 +471,14 @@ class FaviconsWebpackPlugin {
* @param {(Buffer | string)[]} logoFileSources
* @param {(Buffer | string)[]} logoMaskableFileSources
* @param {{[key: string]: any}} baseManifest
* @param {import('webpack').Compilation} compilation
* @param {string} resolvedPublicPath
* @param {string} outputPath
*/
async generateFaviconsWebapp(
logoFileSources,
logoMaskableFileSources,
baseManifest,
compilation,
resolvedPublicPath,
outputPath
resolvedPublicPath
) {
const RawSource = compilation.compiler.webpack.sources.RawSource;
const { favicons } = loadFaviconsLibrary();

// Generate favicons using the npm favicons library
Expand Down Expand Up @@ -523,10 +515,7 @@ class FaviconsWebpackPlugin {
return file;
});

const assets = [...images, ...modifiedFiles].map(({ name, contents }) => ({
name: outputPath ? path.join(outputPath, name) : name,
contents: new RawSource(contents, false),
}));
const assets = [...images, ...modifiedFiles];

return { assets, tags, publicPath: resolvedPublicPath };
}
Expand Down

0 comments on commit 6331e91

Please sign in to comment.