-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
assets-webpack-plugin
dependency by an internal plugin, to …
…generate `entrypoints.json` file
- Loading branch information
Showing
6 changed files
with
134 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const copyEntryTmpName = require('../utils/copyEntryTmpName'); | ||
|
||
/** | ||
* Return the file extension from a filename, without the leading dot and without the query string (if any). | ||
* | ||
* @param {string} filename | ||
* @returns {string} | ||
*/ | ||
function getFileExtension(filename) { | ||
return path.extname(filename).slice(1).split('?')[0]; | ||
} | ||
|
||
class EntryPointsPlugin { | ||
constructor({ | ||
publicPath, | ||
outputPath, | ||
integrityAlgorithms | ||
}) { | ||
this.publicPath = publicPath; | ||
this.outputPath = outputPath; | ||
this.integrityAlgorithms = integrityAlgorithms; | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.afterEmit.tapAsync({ name: 'EntryPointsPlugin' }, (compilation, callback) => { | ||
const manifest = { | ||
entrypoints: {}, | ||
}; | ||
|
||
const stats = compilation.getStats().toJson({ | ||
assets: true, | ||
moduleAssets: true, | ||
relatedAssets: false, | ||
chunkGroupAuxiliary: false, | ||
chunks: false, | ||
modules: false, | ||
timings: false, | ||
logging: false, | ||
errorDetails: false, | ||
}); | ||
|
||
for (const [entryName, entry] of Object.entries(stats.entrypoints)) { | ||
// We don't want to include the temporary entry in the manifest | ||
if (entryName === copyEntryTmpName) { | ||
continue; | ||
} | ||
|
||
manifest.entrypoints[entryName] = {}; | ||
|
||
for (const asset of entry.assets) { | ||
// We don't want to include hot-update files in the manifest | ||
if (asset.name.includes('.hot-update.')) { | ||
continue; | ||
} | ||
|
||
const fileExtension = getFileExtension(asset.name); | ||
const assetPath = this.publicPath.trimRight('/') + asset.name; | ||
|
||
if (!(fileExtension in manifest.entrypoints[entryName])) { | ||
manifest.entrypoints[entryName][fileExtension] = []; | ||
} | ||
|
||
manifest.entrypoints[entryName][fileExtension].push(assetPath); | ||
} | ||
} | ||
|
||
if (this.integrityAlgorithms.length > 0) { | ||
manifest.integrity = {}; | ||
|
||
for (const entryName in manifest.entrypoints) { | ||
for (const fileType in manifest.entrypoints[entryName]) { | ||
for (const asset of manifest.entrypoints[entryName][fileType]) { | ||
if (asset in manifest.integrity) { | ||
continue; | ||
} | ||
|
||
const filePath = path.resolve( | ||
this.outputPath, | ||
asset.replace(this.publicPath, ''), | ||
); | ||
|
||
if (fs.existsSync(filePath)) { | ||
const fileHashes = []; | ||
|
||
for (const algorithm of this.integrityAlgorithms) { | ||
const hash = crypto.createHash(algorithm); | ||
const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
hash.update(fileContent, 'utf8'); | ||
|
||
fileHashes.push(`${algorithm}-${hash.digest('base64')}`); | ||
} | ||
|
||
manifest.integrity[asset] = fileHashes.join(' '); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fs.writeFileSync( | ||
path.join(this.outputPath, 'entrypoints.json'), | ||
JSON.stringify(manifest, null, 2), | ||
{ flag: 'w' }, | ||
); | ||
|
||
callback(); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { EntryPointsPlugin }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2091,15 +2091,6 @@ assertion-error@^1.1.0: | |
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b" | ||
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw== | ||
|
||
[email protected].*: | ||
version "7.0.0" | ||
resolved "https://registry.yarnpkg.com/assets-webpack-plugin/-/assets-webpack-plugin-7.0.0.tgz#c61ed7466f35ff7a4d90d7070948736f471b8804" | ||
integrity sha512-DMZ9r6HFxynWeONRMhSOFTvTrmit5dovdoUKdJgCG03M6CC7XiwNImPH+Ad1jaVrQ2n59e05lBhte52xPt4MSA== | ||
dependencies: | ||
camelcase "^6.0.0" | ||
escape-string-regexp "^4.0.0" | ||
lodash "^4.17.20" | ||
|
||
ast-types@^0.13.4: | ||
version "0.13.4" | ||
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.13.4.tgz#ee0d77b343263965ecc3fb62da16e7222b2b6782" | ||
|