-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
What is the proper way to have an isBrowser
/isNode
check that causes the code to only bundle one of two paths depending on platform target?
#3992
Comments
You can add a try-catch around the requires so that esbuild won't check inside: const inBrowser = (typeof window !== "undefined");
let NodeWorker;
let NodeCrypto;
if (!inBrowser) {
try {
NodeWorker = require("worker_threads").Worker;
NodeCrypto = require("crypto");
} catch {}
} |
If in node, you would want an error if either of those fails. Adding a try with an empty catch would introduce a potential bug. |
Hmm, this seems to do the trick. Why is it that esbuild ignores the contents of try/catch blocks? const inBrowser = (typeof window !== "undefined");
let NodeWorker;
let NodeCrypto;
if (!inBrowser) {
try {
NodeWorker = require("worker_threads").Worker;
NodeCrypto = require("crypto");
} catch (error) {
// necessary so esbuild ignores this
throw error
}
} |
esbuild reports that error because it can not bundle something not exist if that thing isn't externalized. But there's a common code pattern which gracefully handles "optional dependencies" (i.e. inside a try-catch block) so esbuild also allows that to swallow the error. Lines 115 to 125 in 9eca464
Other choices are that you can tell esbuild those node modules are externalized, or replaced with |
Is it possible to achieve the second solution (just put undefined) via CLI options, or do I have to modify the package.json to achieve that? |
This code will fail to bundle when
--platform=browser
is set because neither of those libraries are available in the browser. How can I modify theisBrowser
check such that esbuild will correctly skip those lines?The text was updated successfully, but these errors were encountered: