-
Notifications
You must be signed in to change notification settings - Fork 6
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
faucet-pipeline-js(x) does not replace process.env.NODE_ENV when bundling an React app #144
Comments
I managed to get that under control by excluding all dependencies from transpilation ( So the remaining problem would be that the NODE_ENV guards are not replaced as I was expecting. |
This branch has a potential solution for the problem, but I guess you wouldn't want something like this to be enabled unconditionally? I also now see that #118 is kinda related. |
Hi Niels, this is a known problem with React. The For reference, I'm pasting an excerpt of a real-world config file for a React application below: js: [
{
source: "./src/index.tsx",
target: "./dist/bundle.js",
typescript: true,
jsx: true,
externals: {
react: "React",
"react-dom": "ReactDOM",
"react-router-dom": "ReactRouterDOM",
},
},
],
static: [
{
source: "./src/index.html",
target: "./dist/index.html",
},
{
source: "react/umd/react.development.js",
target: "./dist/react.js",
},
{
source: "react-dom/umd/react-dom.development.js",
target: "./dist/react-dom.js",
},
{
source: "react-router-dom/umd/react-router-dom.min.js",
target: "./dist/react-router-dom.js",
},
],
// ... Of course, this requires adding some extra From our PoV, this solution is even preferred over processing React itself through faucet. The reason is that your application code changes more frequently than your library code, so you can leverage HTTP caching better if your bundle does not contain huge dependencies like React. In particular, you could configure your HTTP server to serve React and others with a longer cache period, or use fingerprinting with an indefinite cache period. These measures will improve further page loads. Having said all that, there are React libraries (like React helmet) that do not provide prebuilt UMD files but use the |
Thanks Lars! I'm aware of the benefits of extracting library code from the bundle (I've usually been using Webpack's code splitting for this in the past), just had hoped to start quickly and add such optimisations later on. The snippet you included definitely helped getting my minimal example working, so thanks for that as well! If there is anything I can do to help with supporting the Thanks, Niels |
Hey Niels, thanks for your feedback and suggested patch. As pointed out by Lars, this issue has come up in the past, and while I can see why it'd be useful for faucet-js to support this convention, it needs to be an opt-in feature to account for faucet's philosophy:
This means that there needs to be a configuration option and dependencies (i.e. @rollup/plugin-replace) need to be moved into an optional meta-package (see However, it might might actually be easier (and more philosophy-compliant) to turn this into a separate plugin, post-processing bundles without having to worry about previous transformations. Unfortunately though, composing plugins like that is currently not easy; we're working to improve that. PS: Out of curiosity, your second comment suggests you're using React's already-optimized distribution with |
In larsrh/faucet-pipeline-essugar@9d1cc94, I've (unconditionally) enabled support for this idiom. faucet-pipeline-essugar was created as an experimental playground for better JSX and TypeScript support via sucrase. Consequently, its footprint is already a smaller than faucet-pipeline-js, so adding the replace plugin isn't at all problematic. It's designed to be a drop-in replacement for faucet-pipeline-js. @ngrewe, I've sent a PR to your repo (ngrewe/faucet-pipeline-react#1). Please let us know if that works for you. If so, we can consider adding this into a new major release of faucet-pipeline-js. (We're concerned about long-term stability of faucet, so additions like this need to be carefully considered.) |
Thanks for your feedback. The approach taken by faucet-pipeline-essugar works for me! What is the relation to faucet-pipeline-js? Do you plan to eventually have faucet-pipeline-essugar become the new faucet-pipeline-js?
I generally consider branching on
I agree. Webpack uses "define", which has a distinctly C preprocessor-y vibe that probably isn't very intelligible, and Rollup uses "replace", which is much too generic (yet adequate, since – as I discovered – it really just does string replacement without considering the source file's syntax at all). Maybe the naming problem can be alleviated by reducing the scope? If the goal is to only support |
I've updated my aforementioned branch to have this functionality pluggable. The demonstrator that works with this (after |
So far, faucet-pipeline-essugar has been a playground for a next-gen JS pipeline. Currently we don't have internal consensus yet about the future direction. |
Hey folks,
Having spent countless hours crafting webpack configurations, I find the concept of faucet quite appealing and am currently trying to bundle a simple React application. Unfortunately, I can't really wrap my head around how to do it. A reproducing repository of my problem can be found at ngrewe/faucet-pipeline-react.
If I run this through faucet with(I managed to solve this problem, see comment below)jsx
andesnext
set totrue
, I end up with a bundle that is unusable in the browser because it still contains commonJSrequire()
calls. These calls are from code imported from within my node_modules folder, so it seems that rollup is not making any effort to pull them into the bundle correctly. Interestingly, if I add arequire()
call to my own code, that will be bundled properly.I've also noticed that if I do not include
esnext: true
in the faucet configuration, allrequire()
d modules are correctly bundled up, but then things trip up on the various places where React usesprocess.env.NODE_ENV
, which remains present in the finished bundle. In the webpack world, that would automatically be defined away according to the--mode
switch – how is Rollup/Faucet supposed to handle this? Any pointers would be greatly appreciated.Thanks,
Niels
The text was updated successfully, but these errors were encountered: