question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use ESM build in modern browser (<script type="module" ...>)

See original GitHub issue

Current 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:

  1. Add an option to tsdx.config.js that allows users to opt out of babel-plugin-dev-expression, or replace with a different plugin.
  2. 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:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
mmuletcommented, Mar 23, 2020
  1. I agree, I think a separate production production, minified ESM build would be better than stubbing things in. I’ll close this issue.
  2. For completion’s sake:
//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 typeof process is the variable: var process. (This will actually throw an error if you use use let process or const process). So, it actually looks like this due to hoisting:

var process;
process = typeof process === "undefined" ? {env: {NODE_ENV: "production"}} : process;

As you can see, process will always be undefined. I mistakenly thought it worked in the browser because typeof process == undefined was the desired outcome. Finally: I did get a solution to work. In both node and browsers.

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"}};
1reaction
agilgur5commented, Mar 22, 2020

edit: Nevermind. Doesn’t work in nodejs

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 regard

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. 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 in package.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.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found