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
Generated ES module trying to import already-removed empty JS chunk #15653
Comments
I've tried to add a quick&dirty check in the following loop, it works. With this change, generated webpack/lib/esm/ModuleChunkFormatPlugin.js Lines 149 to 161 in d3a0f8d
for (const chunk of chunks) {
if (loadedChunks.has(chunk)) continue;
+ // bypass if there is no javascript typed modules behind current chunk
+ if (Array.from(chunk.modulesIterable).every(m => !(m.type && m.type.startsWith('javascript')))) continue;
loadedChunks.add(chunk);
startupSource.add(
`import * as __webpack_chunk_${index}__ from ${JSON.stringify(
getRelativePath(chunk)
)};\n` Thanks. |
For anyone who runs into the same issue, before this is fixed in Webpack officially, you may consider my workaround below. const webpack = require('webpack');
const { JavascriptModulesPlugin } = webpack.javascript;
const PLUGIN_NAME = 'FixEsmImportPlugin';
class FixEsmImportPlugin {
/**
* Apply the plugin
* @param {import('webpack').Compiler} compiler
*/
apply(compiler) {
const removeEmptyChunks = compiler.options.optimization.removeEmptyChunks !== false;
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
hooks.renderChunk.tap(PLUGIN_NAME, (_modules, renderContext) => {
const { chunk, chunkGraph } = renderContext;
const moduleAndEntrypoints = chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk);
for (const [_module, entrypoint] of moduleAndEntrypoints) {
for (const chunk of entrypoint.chunks) {
if (
removeEmptyChunks &&
chunk !== entrypoint.getRuntimeChunk() &&
!chunkGraph.getChunkModulesIterableBySourceType(chunk, 'javascript')
) {
entrypoint.removeChunk(chunk);
console.debug(`[${PLUGIN_NAME}] Removed empty chunk with id "${chunk.id}" before rendering startupSource`);
}
}
}
});
});
}
} This plugin runs right before |
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
Hi, @tommie, I'm the submitter of guybedford/es-module-shims#295 you've recently resolved, great work indeed! Excuse me I'm at-ing you here, not really asking for help, but just FYI in case you might run into similar issues when using ESM with Webpack. |
Thank you, I will check it |
Fixed by #16809 |
Bug report
What is the current behavior?
I'm leveraging Webpack 5's ESM support to generate ESM libraries, in the meanwhile I use
mini-css-extract-plugin
to extract all CSS to a single file. With some typical config, I succeed in the code-splitting in a "multi JS + single CSS" style.However, although the
mini-css-extract-plugin
only generates a single CSS file, the generated entry ESM is still trying to import the JS file with the same chunk name. This will cause runtime error in browsers. The ESM will fail to initialize due to the following error:This is very similar to #11671, the key difference in this issue is
outputModule: true
I think. I've done some debugging on my own but no lock, any guidance will be appreciated. Thanks in advance.If the current behavior is a bug, please provide the steps to reproduce.
Please checkout https://github.com/evisong/testcase-webpack-esm-css-extract,
Visit http://localhost:8080/ in browser, the DevTools -> Console shows an error:
Double-check in DevTools -> Network, the resource is missing:
But its reference appears at the bottom the
http://localhost:8080/esm.js
:What is the expected behavior?
The entry ESM bundle
http://localhost:8080/esm.js
should not import anything from the nonexistingstyles.js
.According to a quick debug, in webpack/lib/esm/ModuleChunkFormatPlugin.js, the
import
code forstyles
chunk (containing bothjavascript
andcss/mini-extract
modules) is rendered just like the other chunks. However the emptystyles.js
file is somehow dropped (not sure by Webpack or MiniCssExtractPlugin), butModuleChunkFormatPlugin
is not aware of its nonexistence.Other relevant information:
webpack version: 5.72.0
Node.js version: 14.18.2
Operating System: macOS Monterey v12.0
Additional tools: yarn v1.22.18
The text was updated successfully, but these errors were encountered: