Use ESM build in modern browser (<script type="module" ...>)
See original GitHub issueCurrent Behavior
babel-plugin-dev-expression
replaces all instances of __DEV__
with process.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:
- Add an option to
tsdx.config.js
that allows users to opt out ofbabel-plugin-dev-expression
, or replace with a different plugin. - Replace
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
Issue Analytics
- State:
- Created 4 years ago
- Comments:9
Top Results From Across the Web
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >Using ES Modules in the Browser Today - SitePoint
This article will show you how you can use ES modules in the browser today. Until recently, JavaScript had no concept of modules....
Read more >How to use ESM on the web and in Node.js - ByteofDev
Using ESM on the Web To load a script with ESM, you need to add type="module" in the script tag. Then, all modules...
Read more >ECMAScript modules in browsers - JakeArchibald.com
All you need is type=module on the script element, and the browser will treat the inline or external script as an ECMAScript module....
Read more >JavaScript modules via script tag | Can I use... Support tables ...
Loading JavaScript module scripts (aka ES6 modules) using <script type="module"> Includes support for the nomodule attribute. Usage % of. all users, all tracked ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What’s happening, is that the process referred to in
typeof process
is the variable:var process
. (This will actually throw an error if you use uselet process
orconst process
). So, it actually looks like this due to hoisting:As you can see,
process
will always be undefined. I mistakenly thought it worked in the browser becausetypeof process == undefined
was the desired outcome. Finally: I did get a solution to work. In both node and browsers.Why doesn’t this work in Node? Seems to work ok on my local Node version (v10.16.0). Can also simplify to just
process || {env: {NODE_ENV: "production"}}
.Buuut stubbing
process
may cause unexpected errors further downstream if there were any checks for it etc.process?.env?.NODE_ENV
is much safer with that regardGiving 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. As a workaround, I think if you run
tsdx build --env production --minify --format esm
, that’s what you’d get. But there’s no way to include that inpackage.json
, afaik there is no field specific to browser ESM. See also #140 / #141 / #142 on why TSDX doesn’t ship dev/prod ESM bundles (tl;dr you can’t do conditional imports like you can with CJS) (Also--env
and--minify
aren’t officially supported (#508 , #310), I think they just happen to work because the options happen to get passed all the way through.)