Skip to content

Commit

Permalink
Move conditional useInsertionEffect declarations into separate pack…
Browse files Browse the repository at this point in the history
…age (#2867)

* Move conditional `useInsertionEffect` declarations into separate package

* add missing dependecies

* regenerate lockfile

* Add missing peer dep

* regenerate lockfile

* add missing dev dep for a peer dep

* regenerate lockfile

* add changesets
  • Loading branch information
Andarist committed Aug 30, 2022
1 parent 28522cd commit 89b6dbb
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 44 deletions.
8 changes: 8 additions & 0 deletions .changeset/blue-pears-explode.md
@@ -0,0 +1,8 @@
---
'@emotion/react': patch
'@emotion/styled': patch
---

Externalized code referencing `React.useInsertionEffect` to a separate `@emotion/use-insertion-effect-with-fallbacks` package. This package should be used in your defined externals if you bundle Emotion for whatever reason. It references `useInsertionEffect` in a very specific way that allows us to use it conditionally. However, if the code consuming Emotion is bundled as a library with Emotion in it then some bundlers might change the way in which we reference `useInsertionEffect` and that might create problems for bundlers used to consume the said library code. By externalizing this new package you can still bundle Emotion if you want to without running into this problem as you won't "destroy" the carefully crafted reference to `useInsertionEffect` in the process.

Note that we don't recommend bundling Emotion. You should have very specific reasons to do so.
5 changes: 5 additions & 0 deletions .changeset/lemon-emus-applaud.md
@@ -0,0 +1,5 @@
---
'@emotion/use-insertion-effect-with-fallbacks': major
---

A wrapper package that uses `useInsertionEffect` or a specific fallback for it. It comes with two exports: `useInsertionEffectAlwaysWithSyncFallback` and `useInsertionEffectWithLayoutFallback`.
1 change: 1 addition & 0 deletions packages/react/package.json
Expand Up @@ -66,6 +66,7 @@
"@emotion/babel-plugin": "^11.10.0",
"@emotion/cache": "^11.10.0",
"@emotion/serialize": "^1.1.0",
"@emotion/use-insertion-effect-with-fallbacks": "^0.0.0",
"@emotion/utils": "^1.2.0",
"@emotion/weak-memoize": "^0.3.0",
"hoist-non-react-statics": "^3.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/class-names.js
Expand Up @@ -8,7 +8,7 @@ import {
import { serializeStyles } from '@emotion/serialize'
import { withEmotionCache } from './context'
import { ThemeContext } from './theming'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
import { isBrowser } from './utils'

type ClassNameArg =
Expand Down Expand Up @@ -94,7 +94,7 @@ type Props = {
}

const Insertion = ({ cache, serializedArr }) => {
let rules = useInsertionEffectMaybe(() => {
let rules = useInsertionEffectAlwaysWithSyncFallback(() => {
let rules = ''
for (let i = 0; i < serializedArr.length; i++) {
let res = insertStyles(cache, serializedArr[i], false)
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/emotion-element.js
Expand Up @@ -10,7 +10,7 @@ import {
import { hasOwnProperty, isBrowser } from './utils'
import { serializeStyles } from '@emotion/serialize'
import { getLabelFromStackTrace } from './get-label-from-stack-trace'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'

let typePropName = '__EMOTION_TYPE_PLEASE_DO_NOT_USE__'

Expand Down Expand Up @@ -57,7 +57,7 @@ export const createEmotionProps = (type: React.ElementType, props: Object) => {
const Insertion = ({ cache, serialized, isStringTag }) => {
registerStyles(cache, serialized, isStringTag)

const rules = useInsertionEffectMaybe(() =>
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
insertStyles(cache, serialized, isStringTag)
)

Expand Down
9 changes: 3 additions & 6 deletions packages/react/src/global.js
Expand Up @@ -4,6 +4,7 @@ import { withEmotionCache } from './context'
import { ThemeContext } from './theming'
import { insertStyles } from '@emotion/utils'
import { isBrowser } from './utils'
import { useInsertionEffectWithLayoutFallback } from '@emotion/use-insertion-effect-with-fallbacks'

import { serializeStyles } from '@emotion/serialize'

Expand All @@ -13,10 +14,6 @@ type GlobalProps = {
+styles: Styles | (Object => Styles)
}

const useInsertionEffect = React['useInsertion' + 'Effect']
? React['useInsertion' + 'Effect']
: React.useLayoutEffect

let warnedAboutCssPropForGlobal = false

// maintain place over rerenders.
Expand Down Expand Up @@ -87,7 +84,7 @@ export let Global: React.AbstractComponent<GlobalProps> =

let sheetRef = React.useRef()

useInsertionEffect(() => {
useInsertionEffectWithLayoutFallback(() => {
const key = `${cache.key}-global`

// use case of https://github.com/emotion-js/emotion/issues/2675
Expand Down Expand Up @@ -117,7 +114,7 @@ export let Global: React.AbstractComponent<GlobalProps> =
}
}, [cache])

useInsertionEffect(() => {
useInsertionEffectWithLayoutFallback(() => {
let sheetRefCurrent = (sheetRef.current: any)
let [sheet, rehydrating] = sheetRefCurrent
if (rehydrating) {
Expand Down
16 changes: 0 additions & 16 deletions packages/react/src/useInsertionEffectMaybe.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/styled/package.json
Expand Up @@ -15,6 +15,7 @@
"@emotion/babel-plugin": "^11.10.0",
"@emotion/is-prop-valid": "^1.2.0",
"@emotion/serialize": "^1.1.0",
"@emotion/use-insertion-effect-with-fallbacks": "^0.0.0",
"@emotion/utils": "^1.2.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/styled/src/base.js
Expand Up @@ -15,7 +15,7 @@ import {
registerStyles
} from '@emotion/utils'
import { serializeStyles } from '@emotion/serialize'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'

const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
Because you write your CSS inside a JavaScript string you actually have to do double escaping, so for example "content: '\\00d7';" should become "content: '\\\\00d7';".
Expand All @@ -27,7 +27,7 @@ let isBrowser = typeof document !== 'undefined'
const Insertion = ({ cache, serialized, isStringTag }) => {
registerStyles(cache, serialized, isStringTag)

const rules = useInsertionEffectMaybe(() =>
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
insertStyles(cache, serialized, isStringTag)
)

Expand Down
16 changes: 0 additions & 16 deletions packages/styled/src/useInsertionEffectMaybe.js

This file was deleted.

21 changes: 21 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Emotion team and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/use-insertion-effect-with-fallbacks/README.md
@@ -0,0 +1 @@
# @emotion/use-insertion-effect-with-fallbacks
44 changes: 44 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/package.json
@@ -0,0 +1,44 @@
{
"name": "@emotion/use-insertion-effect-with-fallbacks",
"version": "0.0.0",
"description": "A wrapper package that uses `useInsertionEffect` or a fallback for it",
"main": "dist/emotion-use-insertion-effect-with-fallbacks.cjs.js",
"module": "dist/emotion-use-insertion-effect-with-fallbacks.esm.js",
"browser": {
"./dist/emotion-use-insertion-effect-with-fallbacks.esm.js": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js"
},
"license": "MIT",
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/use-insertion-effect-with-fallbacks",
"publishConfig": {
"access": "public"
},
"files": [
"src",
"dist"
],
"peerDependencies": {
"react": ">=16.8.0"
},
"devDependencies": {
"react": "16.14.0"
},
"exports": {
".": {
"module": {
"worker": "./dist/emotion-use-insertion-effect-with-fallbacks.worker.esm.js",
"browser": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js",
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.esm.js"
},
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.cjs.js"
},
"./package.json": "./package.json"
},
"preconstruct": {
"exports": {
"envConditions": [
"browser",
"worker"
]
}
}
}
16 changes: 16 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/src/index.js
@@ -0,0 +1,16 @@
import * as React from 'react'

const isBrowser = typeof document !== 'undefined'

const syncFallback = create => create()

const useInsertionEffect = React['useInsertion' + 'Effect']
? React['useInsertion' + 'Effect']
: false

export const useInsertionEffectAlwaysWithSyncFallback = !isBrowser
? syncFallback
: useInsertionEffect || syncFallback

export const useInsertionEffectWithLayoutFallback =
useInsertionEffect || React.useLayoutEffect
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -2584,6 +2584,7 @@ __metadata:
"@emotion/serialize": ^1.1.0
"@emotion/server": 11.10.0
"@emotion/styled": 11.10.0
"@emotion/use-insertion-effect-with-fallbacks": ^0.0.0
"@emotion/utils": ^1.2.0
"@emotion/weak-memoize": ^0.3.0
hoist-non-react-statics: ^3.3.1
Expand Down Expand Up @@ -2658,6 +2659,7 @@ __metadata:
"@emotion/is-prop-valid": ^1.2.0
"@emotion/react": 11.10.0
"@emotion/serialize": ^1.1.0
"@emotion/use-insertion-effect-with-fallbacks": ^0.0.0
"@emotion/utils": ^1.2.0
react: 16.14.0
typescript: ^4.5.5
Expand All @@ -2679,6 +2681,16 @@ __metadata:
languageName: unknown
linkType: soft

"@emotion/use-insertion-effect-with-fallbacks@^0.0.0, @emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks":
version: 0.0.0-use.local
resolution: "@emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks"
dependencies:
react: 16.14.0
peerDependencies:
react: ">=16.8.0"
languageName: unknown
linkType: soft

"@emotion/utils@^1.2.0, @emotion/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@emotion/utils@workspace:packages/utils"
Expand Down

0 comments on commit 89b6dbb

Please sign in to comment.