Skip to content

Commit 1b59e09

Browse files
committed
fix: use new webpack 5 api to extract assets from child compilation
1 parent e6e34fd commit 1b59e09

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<head><script defer="defer" src="bundle.js"></script><link href="styles.css" rel="stylesheet"></head>Hello World from backend2020-10-14T14:25:45.361Z<h2>Partial</h2><img src="0714810ae3fb211173e2964249507195.png">
1+
<head><script defer="defer" src="bundle.js"></script><link href="styles.css" rel="stylesheet"></head>Hello World from backend2020-10-14T15:07:03.011Z<h2>Partial</h2><img src="0714810ae3fb211173e2964249507195.png">

lib/child-compiler.js

+24-32
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
1616
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
1717
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
1818
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
19+
const Compilation = require('webpack').Compilation;
1920

2021
/**
2122
* The HtmlWebpackChildCompiler is a helper to allow reusing one childCompiler
@@ -100,17 +101,38 @@ class HtmlWebpackChildCompiler {
100101
new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var').apply(childCompiler);
101102
new LoaderTargetPlugin('node').apply(childCompiler);
102103

104+
// Generate output file names
105+
const temporaryTemplateNames = this.templates.map((template, index) => `__child-HtmlWebpackPlugin_${index}-${template}`);
106+
103107
// Add all templates
104108
this.templates.forEach((template, index) => {
105-
new SingleEntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}`).apply(childCompiler);
109+
new SingleEntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}-${template}`).apply(childCompiler);
106110
});
107111

108112
this.compilationStartedTimestamp = new Date().getTime();
109113
this.compilationPromise = new Promise((resolve, reject) => {
114+
const extractedAssets = [];
115+
childCompiler.hooks.compilation.tap('HtmlWebpackPlugin', (compilation) => {
116+
compilation.hooks.processAssets.tap(
117+
{
118+
name: 'HtmlWebpackPlugin',
119+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
120+
},
121+
(assets) => {
122+
temporaryTemplateNames.forEach((temporaryTemplateName) => {
123+
if (assets[temporaryTemplateName]) {
124+
extractedAssets.push(assets[temporaryTemplateName]);
125+
compilation.deleteAsset(temporaryTemplateName);
126+
}
127+
});
128+
}
129+
);
130+
});
131+
110132
childCompiler.runAsChild((err, entries, childCompilation) => {
111133
// Extract templates
112134
const compiledTemplates = entries
113-
? extractHelperFilesFromCompilation(mainCompilation, childCompilation, outputOptions.filename, entries)
135+
? extractedAssets.map((asset) => asset.source())
114136
: [];
115137
// Extract file dependencies
116138
if (entries) {
@@ -159,36 +181,6 @@ class HtmlWebpackChildCompiler {
159181
}
160182
}
161183

162-
/**
163-
* The webpack child compilation will create files as a side effect.
164-
* This function will extract them and clean them up so they won't be written to disk.
165-
*
166-
* Returns the source code of the compiled templates as string
167-
*
168-
* @returns Array<string>
169-
*/
170-
function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) {
171-
const helperAssetNames = childEntryChunks.map((entryChunk, index) => {
172-
const entryConfig = {
173-
hash: childCompilation.hash,
174-
chunk: entryChunk,
175-
name: `HtmlWebpackPlugin_${index}`
176-
};
177-
178-
return mainCompilation.getAssetPath(filename, entryConfig);
179-
});
180-
181-
helperAssetNames.forEach((helperFileName) => {
182-
delete mainCompilation.assets[helperFileName];
183-
});
184-
185-
const helperContents = helperAssetNames.map((helperFileName) => {
186-
return childCompilation.assets[helperFileName].source();
187-
});
188-
189-
return helperContents;
190-
}
191-
192184
module.exports = {
193185
HtmlWebpackChildCompiler
194186
};

0 commit comments

Comments
 (0)