Skip to content
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

Preconstruct strips out browser env check for .browser.{cjs, esm}.js build files #494

Open
ajimae opened this issue Sep 19, 2022 · 1 comment

Comments

@ajimae
Copy link

ajimae commented Sep 19, 2022

Issue:

In snippet.ts I have the following snippet which checks for browser environment.

// snippet.ts
const isBrowser = () =>
  typeof window !== 'undefined' &&
  window.document &&
  window.document.nodeType === 9

In the build files snippet.browser.cjs.js and snippet.browser.esm.js the above snippet is transpiled into:

// snippet.browser.cjs.js or snippet.browser.esm.js
const isBrowser = () =>
  window.document &&
  window.document.nodeType === 9

stripping out the typeof window !== 'undefined' check.

However, in the build files snippet.cjs.js and snippet.esm.js the above snippet is transpiled into:

// snippet.cjs.js or snippet.esm.js
const isBrowser = () =>
  typeof window !== 'undefined' &&
  window.document &&
  window.document.nodeType === 9

as expected.

Expected behaviour:

This very check typeof window !== 'undefined' should not be stripped out of the *.broswer.cjs.js and *.browser.esm.js build files.

Question:

  • Why is this the behaviour?
  • Is there a way to prevent this?
@emmatown
Copy link
Member

Replacing typeof window with "object" and dead code elimination from that is the entire behavior of the browser builds, if you'd like to avoid it, you can avoid generating browser builds and use the general builds in browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants