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
Child compiler target is still "browser" (should be "node") #1590
Comments
That's really amazing :) Would be interesting to if there is also a way which will be easier to maintain in future.. |
Thanks @jantimon for quick response! I am very interested in using JSX templates.
It feels like build-time server-side rendering (unlike traditional SSR that works in runtime). But unfortunately I don't have experience with Webpack plugin development. So I can't fix HtmlWebpackPlugin plugin to allow it properly build JSX. What I found out:
So suggested patch is not a solution. We need to find proper solution. I see that |
I posted this question to Webpack repo: webpack/webpack#12539 |
@braska I added the |
@jantimon Unfortunately it doesn't work :( You can use this CodeSandbox to see error: https://codesandbox.io/s/html-webpack-plugin-5x-alpha-forked-4g5yf?file=/src/template.jsx (I updated As a temp solution I am using this hack: https://gist.github.com/braska/ae753c26a31b01a61539f867a2cedfc7 I wrote this plugin and added it before |
When using above patch (https://gist.github.com/braska/ae753c26a31b01a61539f867a2cedfc7) with
. This error is caused in the line below. Line 142 in 0a6568d
As a workaround for this error, I remove Here is the diff from the patch @braska created. diff --git a/PatchHtmlWebpackPlugin.js b/PatchHtmlWebpackPlugin.js
index 8fec424..ed1ea74 100644
--- a/PatchHtmlWebpackPlugin.js
+++ b/PatchHtmlWebpackPlugin.js
@@ -4,6 +4,13 @@
const WebpackOptionsApply = require("webpack/lib/WebpackOptionsApply");
const ResolverFactory = require("webpack/lib/ResolverFactory");
+function removeTapFromHook(hooks, tapName) {
+ const index = hooks.taps.findIndex(tap => tap.name === tapName);
+ if (index > -1) {
+ hooks.taps.splice(index, 1);
+ }
+}
+
class PatchHtmlWebpackPluginPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(
@@ -111,6 +118,24 @@ class PatchHtmlWebpackPluginPlugin {
optionsCopy,
childCompiler
);
+
+ // workaround for
+ // Error: internal/modules/cjs/loader.js:883
+ // throw err;
+ // ^
+ // Error: Cannot find module './__child-189'
+ // happens in
+ // https://github.com/jantimon/html-webpack-plugin/blob/0a6568d587a82d88fd3a0617234ca98d26a1e0a6/index.js#L142
+ [
+ // "CommonJsChunkFormatPlugin",
+ // "ReadFileCompileWasmPlugin",
+ // "ReadFileCompileAsyncWasmPlugin",
+ // "WorkerPlugin",
+ "SplitChunksPlugin",
+ // "ResolverCachePlugin"
+ ].forEach((tapName) => {
+ removeTapFromHook(childCompiler.hooks.thisCompilation, tapName);
+ })
}
);
} updated This happens especially when using react component for the template. |
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Current behaviour 💣
I would like to use JSX as template. I created
myTemplate.jsx
file:And I pointed this file in plugin options in Webpack config:
But I see this error on compilation:
As I see
styled-components.browser.esm.js
was used. HtmlWebpackPlugin compiles template in child compailer and later runs compiled JS in Node (byvm
package). That means target for child compilation should be "node". But for some reason it still "browser". And bacause of thisstyled-components.browser.esm.js
was used.Expected behaviour ☀️
styled-components
has a lot of JS files declared inpackage.json
:I think
dist/styled-components.cjs.js
should be used. Proably, Webpack will automatically use it if target in child compiler set to "node".Found workaround
If you add
target: "node"
to webpack config, that child compiler inherits this target. It fixes template compilation, but breaks my entrypoint (it requirestarget: "browser"
).I temporary add this code to here
html-webpack-plugin/lib/child-compiler.js
Line 102 in a8ce9b0
This code patches child compiler configuration and everything works as expected. But it is dirty hack just to prove my idea that problem related to target in child compiler.
Reproduction Example 👾
https://codesandbox.io/s/html-webpack-plugin-5x-alpha-forked-4g5yf?file=/src/template.jsx
Environment 🖥
Node.js v15.7.0
webpack@5.18.0
html-webpack-plugin@5.0.0-beta.6
The text was updated successfully, but these errors were encountered: