27 lines
1.1 KiB
JavaScript
27 lines
1.1 KiB
JavaScript
/**
|
|
* Plugin for HtmlPlugin which inlines content for an extracted Webpack manifest
|
|
* into the HTML in a <script> tag before other emitted asssets are injected by
|
|
* HtmlPlugin itself.
|
|
*/
|
|
export default function injectManifestPlugin() {
|
|
this.plugin('compilation', (compilation) => {
|
|
compilation.plugin('html-webpack-plugin-before-html-processing', (data, cb) => {
|
|
Object.keys(compilation.assets).forEach((key) => {
|
|
if (!key.startsWith('manifest.')) { return; }
|
|
const { children } = compilation.assets[key];
|
|
if (children && children[0]) {
|
|
// eslint-disable-next-line no-param-reassign
|
|
data.html = data.html.replace(/^(\s*)<\/body>/m, `$1<script>${children[0]._value}</script>\n$1</body>`);
|
|
// Remove the manifest from HtmlPlugin's assets to prevent a <script>
|
|
// tag being created for it.
|
|
const manifestIndex = data.assets.js.indexOf(data.assets.publicPath + key);
|
|
data.assets.js.splice(manifestIndex, 1);
|
|
// eslint-disable-next-line no-param-reassign
|
|
delete data.assets.chunks.manifest;
|
|
}
|
|
});
|
|
cb(null, data);
|
|
});
|
|
});
|
|
}
|