-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom webpack plugin for chunk file name extraction
- Loading branch information
1 parent
06a6ffa
commit e61ce27
Showing
4 changed files
with
102 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const path = require('path'); | ||
|
||
/** | ||
* This plugin appends the non-entrypoint JS chunk file names to the asset files generated by the DependencyExtractionWebpackPlugin. | ||
* | ||
* This mostly used to load translation files for the non-entrypoint JS chunks. | ||
*/ | ||
class ChunksNameAssetAppendPlugin { | ||
apply(compiler) { | ||
compiler.hooks.compilation.tap( | ||
'EntrypointChunksPlugin', | ||
(compilation) => { | ||
compilation.hooks.processAssets.tapAsync( | ||
{ | ||
name: 'EntrypointChunksPlugin', | ||
stage: compiler.webpack.Compilation | ||
.PROCESS_ASSETS_STAGE_ANALYSE, | ||
}, | ||
(assets, callback) => { | ||
// Get entrypoint names | ||
const entrypoints = Array.from( | ||
compilation.entrypoints.keys() | ||
); | ||
|
||
// Collecting non-entrypoint JS chunk file names | ||
const jsChunkFiles = Object.keys(assets).filter( | ||
(file) => | ||
file.endsWith('.js') && | ||
!entrypoints.includes( | ||
path.basename(file, '.js') | ||
) | ||
); | ||
|
||
if (jsChunkFiles.length === 0) { | ||
callback(); | ||
return; | ||
} | ||
|
||
// Manually constructing the array string for PHP | ||
const jsChunkFilesArrayString = | ||
'array(' + | ||
jsChunkFiles.map((file) => `'${file}'`).join(', ') + | ||
')'; | ||
|
||
// Process each asset | ||
for (const assetName in assets) { | ||
if (assetName.endsWith('.asset.php')) { | ||
// Modify asset content | ||
const assetContent = assets[assetName].source(); | ||
const modifiedContent = assetContent.replace( | ||
'<?php return array(', | ||
`<?php return array('chunks' => ${jsChunkFilesArrayString},` | ||
); | ||
|
||
/* eslint-disable no-console */ | ||
console.log( | ||
'\n\x1b[35m%s\x1b[0m \x1b[0m%s\x1b[0m \x1b[36m%s\x1b[0m \x1b[33m%s\x1b[0m\n', | ||
'[Neve Webpack Plugin][ChunksNameAssetAppendPlugin]', | ||
assetName, | ||
'has been changed to include the following file chunks name as array:', | ||
jsChunkFilesArrayString | ||
); | ||
|
||
// Updating the asset in the compilation | ||
compilation.updateAsset( | ||
assetName, | ||
new compiler.webpack.sources.RawSource( | ||
modifiedContent | ||
) | ||
); | ||
} | ||
} | ||
|
||
callback(); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
} | ||
|
||
module.exports = { | ||
ChunksNameAssetAppendPlugin, | ||
}; |
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