Skip to content
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

fix: do not import non javascript chunks #16809

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/esm/ModuleChunkFormatPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const HotUpdateChunk = require("../HotUpdateChunk");
const Template = require("../Template");
const { getAllChunks } = require("../javascript/ChunkHelpers");
const {
chunkHasJs,
getCompilationHooks,
getChunkFilenameTemplate
} = require("../javascript/JavascriptModulesPlugin");
Expand Down Expand Up @@ -147,7 +148,11 @@ class ModuleChunkFormatPlugin {
undefined
);
for (const chunk of chunks) {
if (loadedChunks.has(chunk)) continue;
if (
loadedChunks.has(chunk) ||
!chunkHasJs(chunk, chunkGraph)
)
continue;
loadedChunks.add(chunk);
startupSource.add(
`import * as __webpack_chunk_${index}__ from ${JSON.stringify(
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/output-module/issue-16040/bar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bar {
color: #fff;
}
7 changes: 7 additions & 0 deletions test/configCases/output-module/issue-16040/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { countBy } from "lodash-es";

import "./bar.css";

const result = countBy([6.1, 4.2, 6.3], Math.floor);

export default result["6"];
3 changes: 3 additions & 0 deletions test/configCases/output-module/issue-16040/foo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: #fff;
}
7 changes: 7 additions & 0 deletions test/configCases/output-module/issue-16040/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { dropRight } from "lodash-es";

import "./foo.css";

const result = dropRight([10, 20, 30], 2);

export default result[0];
15 changes: 15 additions & 0 deletions test/configCases/output-module/issue-16040/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import foo from "./foo.js";
import bar from "./bar.js";

console.log(foo + bar);

it("should not contain non javascript chunk in the main bundle", () => {
const fs = require("fs");
const source = fs.readFileSync(__STATS__.outputPath + "/main.mjs", "utf-8");

expect(__STATS__.chunks.some(c => c.names.includes("style"))).toBe(true);
// Should not import "./style.mjs";`
expect(source).not.toMatch(
/import\s\*\sas+\s__webpack_chunk_[0-9]+__\sfrom\s"\.\/style\.mjs"/g
);
});
5 changes: 5 additions & 0 deletions test/configCases/output-module/issue-16040/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["main.mjs", "vendor.mjs", "runtime.mjs"];
}
};
5 changes: 5 additions & 0 deletions test/configCases/output-module/issue-16040/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const supportsRequireInModule = require("../../../helpers/supportsRequireInModule");

module.exports = () => {
return supportsRequireInModule();
};
77 changes: 77 additions & 0 deletions test/configCases/output-module/issue-16040/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
mode: "production",
devtool: false,
experiments: {
outputModule: true
},
output: {
publicPath: "/",
filename: "[name].mjs",
chunkFilename: "[name].chunk.js",
assetModuleFilename: "[hash][ext][query]",
module: true,
libraryTarget: "module",
chunkFormat: "module",
chunkLoading: "import",
environment: {
dynamicImport: true,
module: true
}
},

module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"]
}
]
},

plugins: [
new MiniCssExtractPlugin({
filename: "style.css",
chunkFilename: "[id].css"
})
],

optimization: {
splitChunks: {
chunks: "all",

cacheGroups: {
style: {
name: "style",
type: "css/mini-extract",
chunks: "all",
enforce: true
},

defaultVendors: {
name: "vendor",
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: "initial",
reuseExistingChunk: true
},

default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
},

runtimeChunk: {
name: "runtime"
},

// currently Webpack has bugs when setting concatenateModules to true while produce ES Module output.
// concatenateModules: false,

minimize: false
}
};