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
Published NPM code references process.env.NODE_ENV #933
Comments
This is intentional – it's how we can distinguish between DEV and PROD for your application so we can add warning messages without bloating the production bundle size. Every other modern lib uses the same technique. If you're using Rollup, you need to use |
This is an interesting conundrum for the 'buildless' approach to using es modules in development. With the advent of web components and es modules, along with es-dev-server to serve them without a build process, it is quite easy for a developer to load a module which uses Do you think it might be wise to have these statements fail-safe to production mode if For those using For reference this issue also came up in the redux project reduxjs/redux#2907 where I think it has been addressed through removal in esm output, and more recently in graphql too graphql/graphql-js#1819 |
I think replacing the statements with |
Isn't the optional chaining operator is super new though? Redux still has |
Shipping an (the optional chaining operator would get transpiled in compatible code anyways) |
I think that an The browser compatible esm build should probably use standard |
It’s already reverted to .js on master. Out of curiosity, how do you plan to use TS and the browser-ready version of Popper together? I would expect a TS user to import the /lib version with process.env checks, since you need to compile the code to deploy it. |
Oh excellent thankyou! In spectrum-web-components we're developing with TS. We run a watch process which compiles our tsc code, and this is then served via storybook, via |
Hitting this issue as well, similar setup as benjamind. I also hit this in the past w/ redux before it was fixed. Before that PR was merged, a common workaround was monkey patching window.process.env.. eg pwastarter kit
I am also working on a custom element that uses the latest popper v2.. super excited about making that switch. I don't wish to have my esm class leave a side-effect on the window like that, but I think it's my best option at the moment |
@robrez you can use
|
We have found it hard to use the Rather than do the window modification, we have instead opted to use the babel configuration in our test and dev server environments. Unfortunately since we're building a library to be consumed by others, this does not actually solve the problem for our downstream consumers who will have to do similar babel / webpack fixes or set Honestly I'm not sure what the best approach is here generally speaking, I've searched around the community for guidance and from what I can tell use of |
Would adding a export * from '../../lib'; enough to add TS definitions to |
Doesn't appear to help, also tried other variations with module redeclaration, e.g.: declare module '@popperjs/core/dist/esm' {
export * from '@popperjs/core/lib';
} In all cases we get a type error here: import {
popperGenerator,
defaultModifiers,
} from '@popperjs/core/dist/esm/popper-lite'; The error is:
The only solution I have found is to mirror all the export * from '../../lib/popper-lite'; This appears to then make the types work properly. This way we have correct types for all the various module imports when doing the suggested treeshaking imports approach. |
Have you tried this?
|
That would only work if it was in a I believe the way that typescript resolves imported module types is by first resolving the package and then looking for a file with a So I think since you've got no types/typings definitions, and the main field resolves to a non-matching module name from the CJS output, we're a bit out of luck for any out of the box way of doing this without explicitly including the I think overall it might just be better to have the |
There are two problems with that:
|
Did a quick investigation into DCE with uglify-js (i'm assuming terser is somewhat similar)... With the following file: if (process && process.env && process.env.NODE_ENV === "debug") {
console.log("foo");
} Running process&&process.env,0; Which seems reasonable, you can further reduce this output with 0; Agreed though that the output from babel for optional chaining syntax does not work with DCE where this: if (process?.env?.NODE_ENV === "debug") {
console.log("bar");
} Transpiles to: var _process, _process$env;
if (((_process = process) === null || _process === void 0 ? void 0 : (_process$env = _process.env) === null || _process$env === void 0 ? void 0 : _process$env.NODE_ENV) === "debug") {
console.log("bar");
} And minifies to: var _process,_process$env;"debug"===(null===(_process=!1)||void 0===_process||null===(_process$env=_process.env)||void 0===_process$env?void 0:_process$env.NODE_ENV)&&console.log("bar"); Conclusion? ¯\(ツ)/¯ I honestly don't think there's a 'good' result here for 'buildless' consumers. In those cases you do really start to get into a situation where you can't have any nodejs global variables in your output source. But common practice for debug strings is clearly still falling into the nodejs pattern here. |
Hello friends! If anyone is running into this issue, a workaround solution is injecting Hope this helps! |
@gusnaughton you don't need to do that, popper exports a version without the |
Our use case requires support for IE11 which doesn't support ES Modules without a polyfill. We haven't yet made a decision on whether to include one for this purpose or not, but I agree, this feels unnecessary. |
If you need to support older browsers then you want to use the UMD build, which doesn't include any |
Thank you for this! I will revisit this soon. |
With vite builds, porcess.env is not available and the esm build of popperjs does not use it. See this issue at popper.js: floating-ui/floating-ui#933 UIEXT-561 (Update knime-core-ui to vue 3 and vite)
With vite builds, porcess.env is not available and the esm build of popperjs does not use it. See this issue at popper.js: floating-ui/floating-ui#933 UIEXT-561 (Update knime-core-ui to vue 3 and vite)
With vite builds, porcess.env is not available and the esm build of popperjs does not use it. See this issue at popper.js: floating-ui/floating-ui#933 UIEXT-561 (Update knime-core-ui to vue 3 and vite)
I am trying out rc1 and I hit an issue where browser code is trying to access
process.env.NODE_ENV
.This code gets built into:
I hit this when importing
@popperjs/core/lib/popper-lite
as described in the documentation here. Since this is intended as a client-side UI library, it would be great to not rely on Node.js variables.The text was updated successfully, but these errors were encountered: