-
Notifications
You must be signed in to change notification settings - Fork 508
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
Use ESM build in modern browser (<script type="module" ...>) #631
Comments
Ok, without the context you added in the Unfortunately it's currently non-trivial to swap out Babel plugins, even if we move to a partial preset, you can't remove plugins in a preset either 😕 Per the issue in |
Hmmmm. I guess the simplest way would be to add a polyfill at the start of the esm out. if(typeof window === "object" && typeof process === "undefined" ){
window.process = {env: {NODE_ENV: "production"}};
} |
Of course, we probably don't want to mess with the global variables in window. |
Here, we go. Now we define a module scoped variable process, that will mirror node's process in node, but be a polyfill variable in an esm module var process2 = process
var process = typeof process === "undefined" ? {env: {NODE_ENV: "production"}} : process; edit: Nevermind. Doesn't work in nodejs |
Now it does work with we shadow the process variable in function scope //Works in node and in esm modules
(function(process) {
console.log(process.env.NODE_ENV)
})(
typeof process === "undefined" ? { env: { NODE_ENV: "production" } } : process
); But esm modules don't support exporting from inside a function, so you would either have to wrap function definitions or play around with the function prototype. Either way, it's not longer a "simple" solution, so probably isn't worth it. |
Why doesn't this work in Node? Seems to work ok on my local Node version (v10.16.0). Can also simplify to just Buuut stubbing Giving it some thought, I also don't think this solution is optimal for browser ESM support, because the files aren't minified. If we make a production, minified ESM build, that would support this feature. |
//test.js
var process = typeof process === "undefined" ? {env: {NODE_ENV: "production"}} : process;
console.log(process ); node test.js
#Outputs {env: {NODE_ENV: "production"}} What's happening, is that the process referred to in var process;
process = typeof process === "undefined" ? {env: {NODE_ENV: "production"}} : process; As you can see, const globalScope = typeof global === "undefined" ? window : global;
const process = globalScope.process || {env: {NODE_ENV: "production"}};
//or in the future:
const process = globalScope.process ?? {env: {NODE_ENV: "production"}}; |
React is currently having the same issue with conditionally importing dev/prod ESM builds: https://twitter.com/sebmarkbage/status/1250444550197264387 And: https://twitter.com/sebmarkbage/status/1250453658904326144?s=19 And: nodejs/node#32869
|
The Twitter thread didn't seem to get any new updates but followed the GitHub threads upstream a few times and found: nodejs/node#32869 was superseded by nodejs/node#33171 was superseded by nodejs/node#34637 and that was merged and backported to Node 12 as well. The React issue which has has a key comment from Webpack's author/lead maintainer: facebook/react#11503 (comment) . That was written before the above was merged, so I'm not sure what the current state of this is. If a clear standard emerges and becomes well-tested, I'd like to support that, but it seems like it's still too early to tell. And once it emerges, it'll go through a feedback phase that I think is important before TSDX pushes it to wider adoption, as too early could spur too much breakage or poor feedback. In the meantime, reduxjs/redux-toolkit#658 created a workaround for this too (but requires manually importing a |
Current Behavior
babel-plugin-dev-expression
replaces all instances of__DEV__
withprocess.env.NODE_ENV !== 'production'
. As the process variable may not be available outside of nodejs, it introduces an incompatibility with modern browsers.Suggested solution(s)
There are several different ways we could approach fixing this:
tsdx.config.js
that allows users to opt out ofbabel-plugin-dev-expression
, or replace with a different plugin.babel-plugin-dev-expression
with another plugin that works in both browser and nodejs environments. I made a quick fork of babel-plugin-dev-expression that does just that.Additional context
Affects these issues:
immer
an open issue in
babel-plugin-dev-expression
The text was updated successfully, but these errors were encountered: