|
| 1 | +/* |
| 2 | + * This file is part of the Symfony Webpack Encore package. |
| 3 | + * |
| 4 | + * (c) Fabien Potencier <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | +const crypto = require('crypto'); |
| 15 | +const copyEntryTmpName = require('../utils/copyEntryTmpName'); |
| 16 | + |
| 17 | +/** |
| 18 | + * Return the file extension from a filename, without the leading dot and without the query string (if any). |
| 19 | + * |
| 20 | + * @param {string} filename |
| 21 | + * @returns {string} |
| 22 | + */ |
| 23 | +function getFileExtension(filename) { |
| 24 | + return path.extname(filename).slice(1).split('?')[0]; |
| 25 | +} |
| 26 | + |
| 27 | +class EntryPointsPlugin { |
| 28 | + /** |
| 29 | + * @param {object} options |
| 30 | + * @param {string} options.publicPath The public path of the assets, from where they are served |
| 31 | + * @param {string} options.outputPath The output path of the assets, from where they are saved |
| 32 | + * @param {Array<string>} options.integrityAlgorithms The algorithms to use for the integrity hash |
| 33 | + */ |
| 34 | + constructor({ |
| 35 | + publicPath, |
| 36 | + outputPath, |
| 37 | + integrityAlgorithms |
| 38 | + }) { |
| 39 | + this.publicPath = publicPath; |
| 40 | + this.outputPath = outputPath; |
| 41 | + this.integrityAlgorithms = integrityAlgorithms; |
| 42 | + } |
| 43 | + |
| 44 | + apply(compiler) { |
| 45 | + compiler.hooks.afterEmit.tapAsync({ name: 'EntryPointsPlugin' }, (compilation, callback) => { |
| 46 | + const manifest = { |
| 47 | + entrypoints: {}, |
| 48 | + }; |
| 49 | + |
| 50 | + const stats = compilation.getStats().toJson({ |
| 51 | + assets: true, |
| 52 | + moduleAssets: true, |
| 53 | + relatedAssets: false, |
| 54 | + chunkGroupAuxiliary: false, |
| 55 | + chunks: false, |
| 56 | + modules: false, |
| 57 | + timings: false, |
| 58 | + logging: false, |
| 59 | + errorDetails: false, |
| 60 | + }); |
| 61 | + |
| 62 | + for (const [entryName, entry] of Object.entries(stats.entrypoints)) { |
| 63 | + // We don't want to include the temporary entry in the manifest |
| 64 | + if (entryName === copyEntryTmpName) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + manifest.entrypoints[entryName] = {}; |
| 69 | + |
| 70 | + for (const asset of entry.assets) { |
| 71 | + // We don't want to include hot-update files in the manifest |
| 72 | + if (asset.name.includes('.hot-update.')) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + const fileExtension = getFileExtension(asset.name); |
| 77 | + const assetPath = this.publicPath.slice(-1) === '/' |
| 78 | + ? `${this.publicPath}${asset.name}` |
| 79 | + : `${this.publicPath}/${asset.name}`; |
| 80 | + |
| 81 | + if (!(fileExtension in manifest.entrypoints[entryName])) { |
| 82 | + manifest.entrypoints[entryName][fileExtension] = []; |
| 83 | + } |
| 84 | + |
| 85 | + manifest.entrypoints[entryName][fileExtension].push(assetPath); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (this.integrityAlgorithms.length > 0) { |
| 90 | + manifest.integrity = {}; |
| 91 | + |
| 92 | + for (const entryName in manifest.entrypoints) { |
| 93 | + for (const fileType in manifest.entrypoints[entryName]) { |
| 94 | + for (const asset of manifest.entrypoints[entryName][fileType]) { |
| 95 | + if (asset in manifest.integrity) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + const filePath = path.resolve( |
| 100 | + this.outputPath, |
| 101 | + asset.replace(this.publicPath, ''), |
| 102 | + ); |
| 103 | + |
| 104 | + if (fs.existsSync(filePath)) { |
| 105 | + const fileHashes = []; |
| 106 | + |
| 107 | + for (const algorithm of this.integrityAlgorithms) { |
| 108 | + const hash = crypto.createHash(algorithm); |
| 109 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 110 | + hash.update(fileContent, 'utf8'); |
| 111 | + |
| 112 | + fileHashes.push(`${algorithm}-${hash.digest('base64')}`); |
| 113 | + } |
| 114 | + |
| 115 | + manifest.integrity[asset] = fileHashes.join(' '); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + fs.writeFileSync( |
| 123 | + path.join(this.outputPath, 'entrypoints.json'), |
| 124 | + JSON.stringify(manifest, null, 2), |
| 125 | + { flag: 'w' }, |
| 126 | + ); |
| 127 | + |
| 128 | + callback(); |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = { EntryPointsPlugin }; |
0 commit comments