Skip to content

Fixed publicPath issue#19546

Merged
alexander-akait merged 3 commits intowebpack:mainfrom
mariusGundersen:esm-publicPath-issue
May 19, 2025
Merged

Fixed publicPath issue#19546
alexander-akait merged 3 commits intowebpack:mainfrom
mariusGundersen:esm-publicPath-issue

Conversation

@mariusGundersen
Copy link
Contributor

@mariusGundersen mariusGundersen commented May 19, 2025

This fixes an issue for esm modules where they incorrectly used publicPath to locate the module. See #19495 (comment) and #19432 (comment). It was introduced in #19434

fixes #19547

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 19, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: alexander-akait / name: Alexander Akait (0a482d3, fd5729c)
  • ✅ login: mariusGundersen / name: Marius Gundersen (d4ec7a9)

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case

@mariusGundersen
Copy link
Contributor Author

mariusGundersen commented May 19, 2025

I've added a failing test case. I tried to fix this, but that causes the other tests to fail. I'm not certain, but I think that is because the existing tests are not correct.

This new test fails with the error Cannot find module '/bundle14/../async/936.bundle14.mjs' from 'test/ConfigTestCases.template.js' given this config:

output: {
	path: path.resolve(testPath, "./bundle14"),
	module: true,
	publicPath: "/bundle14/",
	filename: "initial/bundle14.mjs",
	chunkFilename: "async/[id].bundle14.mjs"
}

It should look for bundle14/async/936.bundle14.mjs but it looks for /bundle14/../async/936.bundle14.mjs. The /../ comes from the rootOutputPath that shouldn't be there.

@mariusGundersen
Copy link
Contributor Author

I'm not able to understand how these tests verify the paths, but looking at the generated files it looks incorrect. For example for bundle11, which has this config:

output: {
	path: path.resolve(testPath, "./bundle11"),
	module: true,
	publicPath: "https://example.com/public/path/",
	filename: "initial/bundle11.mjs",
	chunkFilename: "async/[id].bundle11.mjs"
},

The generated code contains:

__webpack_require__.u = (chunkId) => {
	// return url for filenames based on template
	return "async/" + chunkId + ".bundle11.mjs";
};

__webpack_require__.p = "https://example.com/public/path/";

var promise = import(__webpack_require__.p + "../" + __webpack_require__.u(chunkId)).then(installChunk, (e) => {
	if(installedChunks[chunkId] !== 0) installedChunks[chunkId] = undefined;
	throw e;
});

The result is import("https://example.com/public/path/../async/[chunkId].bundle11.mjs"). That is not what I expect.

The same is true for other tests, for example for bundle6, which has this config:

{
	devtool: false,
	target: "web",
	output: {
		module: true,
		publicPath: "",
		filename: "initial/bundle6.mjs",
		chunkFilename: "async/[id].bundle6.mjs"
	},
	experiments: {
		outputModule: true
	}
}

It produces the following (summarized)

__webpack_require__.u = (chunkId) => {
	// return url for filenames based on template
	return "async/" + chunkId + ".bundle6.mjs";
};

__webpack_require__.p = "";

var promise = import(__webpack_require__.p + "../" + __webpack_require__.u(chunkId)).then(installChunk, (e) => {
	if(installedChunks[chunkId] !== 0) installedChunks[chunkId] = undefined;
	throw e;
});

The result is import("../async/[chunkId].bundle6.mjs"). I'm not sure if this is correct, according to the documentation a publicPath: '' should result in relative to HTML directory, but it becomes relative to entry file instead.

@alexander-akait
Copy link
Member

Yes, tests are not good, I am WIP on this

@alexander-akait
Copy link
Member

Ignore failed tests, they will fixed in other PR, because it is not related to this job

@codspeed-hq
Copy link

codspeed-hq bot commented May 19, 2025

CodSpeed Performance Report

Merging #19546 will degrade performances by 80.74%

Comparing mariusGundersen:esm-publicPath-issue (fd5729c) with main (c1e7ba2)

Summary

⚡ 2 improvements
❌ 4 regressions
✅ 127 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
benchmark "devtool-eval-source-map", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 39.7 ms 45.6 ms -12.83%
benchmark "devtool-source-map", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 11.5 ms 59.6 ms -80.74%
benchmark "future-defaults", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 11.6 ms 9.9 ms +17.14%
benchmark "many-chunks-commonjs", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 11.5 ms 51 ms -77.42%
benchmark "many-chunks-esm", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 50.5 ms 62.1 ms -18.62%
benchmark "many-modules-esm", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 50 ms 11.2 ms ×4.5

@alexander-akait alexander-akait merged commit 12b8458 into webpack:main May 19, 2025
40 of 44 checks passed
3ru pushed a commit to 3ru/webpack that referenced this pull request Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken import of esm modules

2 participants