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

[Bug]: The output code contains an incorrect moduleId when using a ternary inside a require call #7139

Closed
LinusCenterstrom opened this issue Jul 12, 2024 · 3 comments · Fixed by #7184
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@LinusCenterstrom
Copy link

LinusCenterstrom commented Jul 12, 2024

System Info

System:
OS: Windows 11 10.0.22631
CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K
Memory: 11.63 GB / 63.58 GB
Binaries:
Node: 18.18.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Chromium (126.0.2592.87)
Internet Explorer: 11.0.22621.3527

Details

Doing the following

const ReactDOMServer = require(process.env.NODE_ENV !== "production"
	? "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.development"
	: "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.production.min.js");

ReactDOMServer.renderToString(
	React.createElement("div", null, "Hello, world!")
);

causes the output to have an extra p in the moduleId in the webpack_require call
It looks like this

__webpack_require__(p774);

Reproduce link

https://github.com/LinusCenterstrom/rspack-repro-buildfail/tree/68572c6b9d758b41aac18ed3ceffc9a215724e03

Reproduce Steps

yarn install
yarn build
check dist/index.js

@LinusCenterstrom LinusCenterstrom added bug Something isn't working pending triage The issue/PR is currently untouched. labels Jul 12, 2024
@h-a-n-a
Copy link
Collaborator

h-a-n-a commented Jul 12, 2024

Duplicated.
Related PR: #6963.
Related issue: #6874
The walk around is to move the ternary out.

@LinusCenterstrom
Copy link
Author

Moving it outside results in a warning and the output code does not contain the code from the file I require.

 Module parse warning:
  ╰─▶   ⚠ Critical dependency: the request of a dependency is an expression
         ╭─[1:1]
       1 │ var importPath = process.env.NODE_ENV !== "production" ? "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.development" : "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.production.min.js"; 
       2 │ var ReactDOMServer = require(importPath);
         ·                              ───────────
       3 │ ReactDOMServer.renderToString(React.createElement("div", null, "Hello, world!"));
         ╰────

Perhaps that will be fixed by #6963?

@LinusCenterstrom
Copy link
Author

This does not work and produces the warning above

const importPath =
	process.env.NODE_ENV !== "production"
		? "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.development"
		: "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.production.min.js";
  
const ReactDOMServer = require(importPath);

This however does work.

let ReactDOMServer;
if (process.env.NODE_ENV !== "production") {
	ReactDOMServer = require("../node_modules/react-dom/cjs/react-dom-server-legacy.browser.development");
} else {
	ReactDOMServer = require("../node_modules/react-dom/cjs/react-dom-server-legacy.browser.production.min.js");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants