-
-
Notifications
You must be signed in to change notification settings - Fork 199
Replace assets-webpack-plugin
dependency by an internal plugin, to generate entrypoints.json
file
#1342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Replace assets-webpack-plugin
dependency by an internal plugin, to generate entrypoints.json
file
#1342
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,136 @@ | ||
/* | ||
* This file is part of the Symfony Webpack Encore package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 { | ||
/** | ||
* @param {object} options | ||
* @param {string} options.publicPath The public path of the assets, from where they are served | ||
* @param {string} options.outputPath The output path of the assets, from where they are saved | ||
* @param {Array<string>} options.integrityAlgorithms The algorithms to use for the integrity hash | ||
*/ | ||
constructor({ | ||
publicPath, | ||
outputPath, | ||
integrityAlgorithms | ||
}) { | ||
this.publicPath = publicPath; | ||
this.outputPath = outputPath; | ||
this.integrityAlgorithms = integrityAlgorithms; | ||
} | ||
|
||
/** | ||
* @param {import('webpack').Compiler} compiler | ||
*/ | ||
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; | ||
} | ||
Comment on lines
+74
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you update a file while the dev-server is running, an |
||
|
||
const fileExtension = getFileExtension(asset.name); | ||
const assetPath = this.publicPath.slice(-1) === '/' | ||
? `${this.publicPath}${asset.name}` | ||
: `${this.publicPath}/${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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.