|
1 | 1 | import HtmlWebpackPlugin from 'html-webpack-plugin'; |
2 | | -import addAllAssetsToCompilation from './addAllAssetsToCompilation'; |
| 2 | +import pEachSeries from 'p-each-series'; |
| 3 | +import micromatch from 'micromatch'; |
| 4 | +import crypto from 'crypto'; |
| 5 | +import globby from 'globby'; |
| 6 | +import { |
| 7 | + ensureTrailingSlash, |
| 8 | + handleUrl, |
| 9 | + resolveOutput, |
| 10 | + resolvePublicPath, |
| 11 | +} from './utils'; |
3 | 12 |
|
4 | 13 | export default class AddAssetHtmlPlugin { |
5 | 14 | constructor(assets = []) { |
6 | | - this.assets = Array.isArray(assets) ? assets.slice().reverse() : [assets]; |
| 15 | + this.assets = Array.isArray(assets) |
| 16 | + ? assets.slice().reverse() |
| 17 | + : [assets].filter(Boolean); |
7 | 18 | } |
8 | 19 |
|
9 | 20 | /* istanbul ignore next: this would be integration tests */ |
10 | 21 | apply(compiler) { |
11 | 22 | compiler.hooks.compilation.tap('AddAssetHtmlPlugin', compilation => { |
12 | 23 | let hook; |
| 24 | + |
13 | 25 | if (HtmlWebpackPlugin.version === 4) { |
14 | 26 | hook = HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration; |
15 | 27 | } else { |
16 | 28 | hook = compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration; |
17 | 29 | } |
18 | 30 |
|
19 | 31 | hook.tapPromise('AddAssetHtmlPlugin', htmlPluginData => |
20 | | - addAllAssetsToCompilation(this.assets, compilation, htmlPluginData), |
| 32 | + this.addAllAssetsToCompilation(compilation, htmlPluginData), |
21 | 33 | ); |
22 | 34 | }); |
23 | 35 | } |
| 36 | + |
| 37 | + async addAllAssetsToCompilation(compilation, htmlPluginData) { |
| 38 | + const handledAssets = await handleUrl(this.assets); |
| 39 | + await pEachSeries(handledAssets, asset => |
| 40 | + this.addFileToAssets(compilation, htmlPluginData, asset), |
| 41 | + ); |
| 42 | + return htmlPluginData; |
| 43 | + } |
| 44 | + |
| 45 | + // eslint-disable-next-line class-methods-use-this |
| 46 | + async addFileToAssets( |
| 47 | + compilation, |
| 48 | + htmlPluginData, |
| 49 | + { |
| 50 | + filepath, |
| 51 | + typeOfAsset = 'js', |
| 52 | + includeRelatedFiles = true, |
| 53 | + hash = false, |
| 54 | + publicPath, |
| 55 | + outputPath, |
| 56 | + files = [], |
| 57 | + }, |
| 58 | + ) { |
| 59 | + if (!filepath) { |
| 60 | + const error = new Error('No filepath defined'); |
| 61 | + compilation.errors.push(error); |
| 62 | + throw error; |
| 63 | + } |
| 64 | + |
| 65 | + const fileFilters = Array.isArray(files) ? files : [files]; |
| 66 | + |
| 67 | + if (fileFilters.length > 0) { |
| 68 | + const shouldSkip = !fileFilters.some(file => |
| 69 | + micromatch.isMatch(htmlPluginData.outputName, file), |
| 70 | + ); |
| 71 | + |
| 72 | + if (shouldSkip) { |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + const addedFilename = await htmlPluginData.plugin.addFileToAssets( |
| 78 | + filepath, |
| 79 | + compilation, |
| 80 | + ); |
| 81 | + |
| 82 | + let suffix = ''; |
| 83 | + if (hash) { |
| 84 | + const md5 = crypto.createHash('md5'); |
| 85 | + md5.update(compilation.assets[addedFilename].source()); |
| 86 | + suffix = `?${md5.digest('hex').substr(0, 20)}`; |
| 87 | + } |
| 88 | + |
| 89 | + const resolvedPublicPath = |
| 90 | + typeof publicPath === 'undefined' |
| 91 | + ? resolvePublicPath(compilation, addedFilename) |
| 92 | + : ensureTrailingSlash(publicPath); |
| 93 | + const resolvedPath = `${resolvedPublicPath}${addedFilename}${suffix}`; |
| 94 | + |
| 95 | + htmlPluginData.assets[typeOfAsset].unshift(resolvedPath); |
| 96 | + |
| 97 | + resolveOutput(compilation, addedFilename, outputPath); |
| 98 | + |
| 99 | + if (includeRelatedFiles) { |
| 100 | + const relatedFiles = await globby(`${filepath}.*`); |
| 101 | + await Promise.all( |
| 102 | + relatedFiles.sort().map(async relatedFile => { |
| 103 | + const addedMapFilename = await htmlPluginData.plugin.addFileToAssets( |
| 104 | + relatedFile, |
| 105 | + compilation, |
| 106 | + ); |
| 107 | + resolveOutput(compilation, addedMapFilename, outputPath); |
| 108 | + }), |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
24 | 112 | } |
0 commit comments