Skip to content

Commit

Permalink
chore: use typeof window checks for removing server code (#27514)
Browse files Browse the repository at this point in the history
# Why

- It's common in the web frameworks world to minify `typeof window`
checks. We'll add this to our babel config in a follow up PR.

# How

- This PR migrates `typeof document` checks to `typeof window` so we can
tree shake extra code.

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
  • Loading branch information
EvanBacon and expo-bot committed Mar 8, 2024
1 parent 4d9c14e commit 186bf0f
Show file tree
Hide file tree
Showing 23 changed files with 51 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/@expo/metro-config/CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@

### 💡 Others

- Use `typeof window` checks for removing server code. ([#27514](https://github.com/expo/expo/pull/27514) by [@EvanBacon](https://github.com/EvanBacon))
- Uncomment all Metro tests. ([#26610](https://github.com/expo/expo/pull/26610) by [@EvanBacon](https://github.com/EvanBacon))

## 0.17.5 - 2024-02-27
Expand Down
2 changes: 1 addition & 1 deletion packages/@expo/metro-config/build/transform-worker/css.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -10,7 +10,7 @@ exports[`getHotReplaceTemplate should generate the correct template 1`] = `

exports[`wrapDevelopmentCSS should transform css in dev mode 1`] = `
"(() => {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return
}
const head = document.head || document.getElementsByTagName('head')[0];
Expand Down
Expand Up @@ -4,7 +4,7 @@ exports[`CSS Modules web transforms for dev, minified 1`] = `
{
"input": "module.exports=Object.assign({"container":"_R_BGG_container"},{unstable_styles:{"container":{"$$css":true,"_":"_R_BGG_container"}}},{});
(() => {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return
}
const head = document.head || document.getElementsByTagName('head')[0];
Expand Down Expand Up @@ -46,7 +46,7 @@ exports[`CSS Modules web transforms for dev, not minified 1`] = `
{
"input": "module.exports=Object.assign({"container":"_R_BGG_container"},{unstable_styles:{"container":{"$$css":true,"_":"_R_BGG_container"}}},{});
(() => {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return
}
const head = document.head || document.getElementsByTagName('head')[0];
Expand Down Expand Up @@ -97,7 +97,7 @@ exports[`CSS Modules web transforms for prod, not minified 1`] = `"module.export
exports[`transforms a global CSS file in dev for web 1`] = `
{
"input": "(() => {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return
}
const head = document.head || document.getElementsByTagName('head')[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/@expo/metro-config/src/transform-worker/css.ts
Expand Up @@ -17,7 +17,7 @@ export function getHotReplaceTemplate(id: string) {
export function wrapDevelopmentCSS(props: { src: string; filename: string }) {
const withBackTicksEscaped = escapeBackticksAndOctals(props.src);
return `(() => {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return
}
const head = document.head || document.getElementsByTagName('head')[0];
Expand Down
2 changes: 2 additions & 0 deletions packages/@expo/metro-runtime/CHANGELOG.md
Expand Up @@ -12,6 +12,8 @@

### 💡 Others

- Use `typeof window` checks for removing server code. ([#27514](https://github.com/expo/expo/pull/27514) by [@EvanBacon](https://github.com/EvanBacon))

## 3.1.3 - 2024-02-06

### 🎉 New features
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -13,7 +13,7 @@ export function fetchThenEvalAsync(
crossOrigin,
}: { scriptType?: string; nonce?: string; crossOrigin?: string } = {}
): Promise<void> {
if (typeof document === 'undefined') {
if (typeof window === 'undefined') {
return require('./fetchThenEvalJs').fetchThenEvalAsync(url);
}
return new Promise<void>((resolve, reject) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/expo-image/CHANGELOG.md
Expand Up @@ -15,6 +15,8 @@

### 💡 Others

- Use `typeof window` checks for removing server code. ([#27514](https://github.com/expo/expo/pull/27514) by [@EvanBacon](https://github.com/EvanBacon))

## 1.11.0 — 2024-02-05

### 🎉 New features
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-image/build/web/imageStyles.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 186bf0f

Please sign in to comment.