If a .TSX generates a reference to an object on compile (eg React.createElement) that is not otherwise referenced in source, it will not get included on production build #17147
Labels
needs: repro steps
We cannot reproduce the issue with the information given
🐞 bug report
Affected Package
Angular compilation process
Is this a regression?
Unknown
Description
I recently inherited a project which uses Angular with embedded React components.
import * as React from "react";
appears in a bunch of .TSX files included with the project, and embedded XML tags are used to indicate React elements, which I understand are compiled down to React.createElement calls.The React object itself is never referenced in any source code directly,
When I run the project in development mode it runs fine. When I build for production, the React.createElement calls remain as React.createElement instead of being compiled to the proper webpack import name.
If I add ANY explicit reference to React in any file, for example
console.log(React.version);
, the production build starts working again.I suspect what is happening is a pass to strip out unused code is happening before .tsx files are processed by TypeScript. And then after the code is compiled down to JS webpack then compiles everything together but React is already gone so the React references are left intact.
If so the best fix I can think of is to split up the TypeScript compilation process to first convert .TSX to temporary .TS files (if possible), run the stripping process on those and not the original .TSX, then complete the build process as normal with the .TS files.
Simpler solutions might include hard coding React to never be stripped out if there are .tsx files present in the project.
🔬 Minimal Reproduction
Unfortunately, I don't have the needed skill set currently to create a minimal test case from scratch (no experience in React) and I am not authorized to share code from my project publicly. Sorry.
🔥 Exception or Error
This occurs at the point in a .TSX where an inline XML element is located.
🌍 Your Environment
Angular Version:
Anything else relevant?
This may be a bug in webpack; I am unsure about which component this bug lies in.
I did not notice any change when messing with build options; only when adding an explicit reference to React to the code.
The text was updated successfully, but these errors were encountered: