|
1 | 1 | const fs = require('fs/promises'); |
2 | 2 | const path = require('path'); |
| 3 | +const { black, blueBright, yellow } = require('ansis'); |
3 | 4 | const HtmlBundlerPlugin = require('../../src/'); |
4 | 5 | const { favicons, config } = require('favicons'); |
| 6 | +const { outToConsole } = require('../../src/Common/Helpers'); |
5 | 7 |
|
6 | 8 | class FaviconsBundlerPlugin { |
7 | 9 | pluginName = 'favicons-bundler-plugin'; |
@@ -117,19 +119,40 @@ class FaviconsBundlerPlugin { |
117 | 119 | }); |
118 | 120 |
|
119 | 121 | // save favicon images on disk |
120 | | - compiler.hooks.afterEmit.tapPromise(pluginName, () => { |
121 | | - if (this.faviconResponse?.images.length > 0) { |
122 | | - const { images } = this.faviconResponse; |
123 | | - const outputPath = HtmlBundlerPlugin.option.getWebpackOutputPath(); |
124 | | - const saveDir = path.join(outputPath, this.options.faviconsConfig.path); |
125 | | - |
126 | | - return fs |
127 | | - .mkdir(saveDir, { recursive: true }) |
128 | | - .then(() => |
129 | | - Promise.all(images.map(async (image) => await fs.writeFile(path.join(saveDir, image.name), image.contents))) |
130 | | - ); |
131 | | - } |
132 | | - }); |
| 122 | + compiler.hooks.afterEmit.tapPromise( |
| 123 | + pluginName, |
| 124 | + () => |
| 125 | + new Promise((resolve, reject) => { |
| 126 | + if (this.faviconResponse?.images.length > 0) { |
| 127 | + const { images } = this.faviconResponse; |
| 128 | + const outputPath = HtmlBundlerPlugin.option.getWebpackOutputPath(); |
| 129 | + const saveDir = path.join(outputPath, this.options.faviconsConfig.path); |
| 130 | + |
| 131 | + return fs |
| 132 | + .mkdir(saveDir, { recursive: true }) |
| 133 | + .then(() => |
| 134 | + Promise.all( |
| 135 | + images.map(async (image) => await fs.writeFile(path.join(saveDir, image.name), image.contents)) |
| 136 | + ).then(resolve) |
| 137 | + ); |
| 138 | + } else { |
| 139 | + this.warningFaviconNotFound(); |
| 140 | + resolve(); |
| 141 | + } |
| 142 | + }) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + warningFaviconNotFound() { |
| 147 | + const header = `\n${black.bgYellow` ${this.pluginName} `}${black.bgAnsi(227)` WARNING `} `; |
| 148 | + |
| 149 | + let warning = `Favicon file is not found! |
| 150 | +If the ${blueBright( |
| 151 | + this.constructor.name |
| 152 | + )} is used, at last one favicon source file should be defined in the template, e.g.: |
| 153 | +${yellow`<link rel="icon" href="path/to/source/favicon.png">`}`; |
| 154 | + |
| 155 | + outToConsole(header + warning); |
133 | 156 | } |
134 | 157 | } |
135 | 158 |
|
|
0 commit comments