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(bundling): fix webpack publicPath: 'auto' behavior for esm builds #13186

Merged
merged 1 commit into from Nov 16, 2022

Conversation

karaggeorge
Copy link
Contributor

There's a lot more detail in the issue I opened, but I'll summarize the output changes here.

When building with webpack and:

  • output.publicPath is set to auto or omitted (it defaults to auto)
  • There's an async loaded chunk (for example using a dynamic import)
  • Scripts are of type module when injected in the html markup (i.e. esm: true)

The wrong snippet from the webpack AutoPublicPathRuntimeModule template is injected

Current Behavior

Current output uses document.currentScript which is not supported for type="module" scripts

Minified (note the currentScript usage):

(()=>{var e;n.g.importScripts&&(e=n.g.location+"");var r=n.g.document;if(!e&&r&&(r.currentScript&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName("script");t.length&&(e=t[t.length-1].src)}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),n.p=e})()

With minification manually disabled:

/******/ 	/* webpack/runtime/publicPath */
/******/ 	(() => {
/******/ 		var scriptUrl;
/******/ 		if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
/******/ 		var document = __webpack_require__.g.document;
/******/ 		if (!scriptUrl && document) {
/******/ 			if (document.currentScript)
/******/ 				scriptUrl = document.currentScript.src
/******/ 			if (!scriptUrl) {
/******/ 				var scripts = document.getElementsByTagName("script");
/******/ 				if(scripts.length) scriptUrl = scripts[scripts.length - 1].src
/******/ 			}
/******/ 		}
/******/ 		// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
/******/ 		// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
/******/ 		if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
/******/ 		scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
/******/ 		__webpack_require__.p = scriptUrl;
/******/ 	})();

Expected Behavior

After this change:

Minified (not import.meta.url usage):

(()=>{var e;if("string"==typeof import.meta.url&&(e=import.meta.url),!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),n.p=e})()

With minification manually disabled:

/******/ 	/* webpack/runtime/publicPath */
/******/ 	(() => {
/******/ 		var scriptUrl;
/******/ 		if (typeof import.meta.url === "string") scriptUrl = import.meta.url
/******/ 		// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
/******/ 		// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
/******/ 		if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
/******/ 		scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
/******/ 		__webpack_require__.p = scriptUrl;
/******/ 	})();

Related Issue(s)

Fixes #13185

@vercel
Copy link

vercel bot commented Nov 15, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
nx-dev ⬜️ Ignored (Inspect) Nov 15, 2022 at 7:46PM (UTC)

@karaggeorge
Copy link
Contributor Author

This issue was originally found while working with @antoniolofiego on their repo using nx@14.5.10

They are currently using a workaround overriding the webpack config. If desired, I can open a similar PR for the 14.x version, but since the issue was reproducible on the latest, I opened it targeting master

@AgentEnder AgentEnder changed the title Fix webpack publicPath: 'auto' behavior for esm builds fix(bundling): fix webpack publicPath: 'auto' behavior for esm builds Nov 16, 2022
@AgentEnder AgentEnder merged commit 60a7345 into nrwl:master Nov 16, 2022
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong webpack automatic public path snippet injected for esm builds
2 participants