From 89b6dbb3c13d49ef1fa3d88888672d810853f05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 30 Aug 2022 10:41:27 +0200 Subject: [PATCH] Move conditional `useInsertionEffect` declarations into separate package (#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 --- .changeset/blue-pears-explode.md | 8 ++++ .changeset/lemon-emus-applaud.md | 5 +++ packages/react/package.json | 1 + packages/react/src/class-names.js | 4 +- packages/react/src/emotion-element.js | 4 +- packages/react/src/global.js | 9 ++-- packages/react/src/useInsertionEffectMaybe.js | 16 ------- packages/styled/package.json | 1 + packages/styled/src/base.js | 4 +- .../styled/src/useInsertionEffectMaybe.js | 16 ------- .../LICENSE | 21 +++++++++ .../README.md | 1 + .../package.json | 44 +++++++++++++++++++ .../src/index.js | 16 +++++++ yarn.lock | 12 +++++ 15 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 .changeset/blue-pears-explode.md create mode 100644 .changeset/lemon-emus-applaud.md delete mode 100644 packages/react/src/useInsertionEffectMaybe.js delete mode 100644 packages/styled/src/useInsertionEffectMaybe.js create mode 100644 packages/use-insertion-effect-with-fallbacks/LICENSE create mode 100644 packages/use-insertion-effect-with-fallbacks/README.md create mode 100644 packages/use-insertion-effect-with-fallbacks/package.json create mode 100644 packages/use-insertion-effect-with-fallbacks/src/index.js diff --git a/.changeset/blue-pears-explode.md b/.changeset/blue-pears-explode.md new file mode 100644 index 000000000..cf1d682e0 --- /dev/null +++ b/.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. diff --git a/.changeset/lemon-emus-applaud.md b/.changeset/lemon-emus-applaud.md new file mode 100644 index 000000000..ed269d937 --- /dev/null +++ b/.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`. diff --git a/packages/react/package.json b/packages/react/package.json index 4bbd0c56b..9dad7231f 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -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" diff --git a/packages/react/src/class-names.js b/packages/react/src/class-names.js index c0ded58c0..6252324f2 100644 --- a/packages/react/src/class-names.js +++ b/packages/react/src/class-names.js @@ -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 = @@ -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) diff --git a/packages/react/src/emotion-element.js b/packages/react/src/emotion-element.js index 837027af6..d1f0deecb 100644 --- a/packages/react/src/emotion-element.js +++ b/packages/react/src/emotion-element.js @@ -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__' @@ -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) ) diff --git a/packages/react/src/global.js b/packages/react/src/global.js index d3d966e2b..bcbbbd2b0 100644 --- a/packages/react/src/global.js +++ b/packages/react/src/global.js @@ -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' @@ -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. @@ -87,7 +84,7 @@ export let Global: React.AbstractComponent = let sheetRef = React.useRef() - useInsertionEffect(() => { + useInsertionEffectWithLayoutFallback(() => { const key = `${cache.key}-global` // use case of https://github.com/emotion-js/emotion/issues/2675 @@ -117,7 +114,7 @@ export let Global: React.AbstractComponent = } }, [cache]) - useInsertionEffect(() => { + useInsertionEffectWithLayoutFallback(() => { let sheetRefCurrent = (sheetRef.current: any) let [sheet, rehydrating] = sheetRefCurrent if (rehydrating) { diff --git a/packages/react/src/useInsertionEffectMaybe.js b/packages/react/src/useInsertionEffectMaybe.js deleted file mode 100644 index 1438d1a7f..000000000 --- a/packages/react/src/useInsertionEffectMaybe.js +++ /dev/null @@ -1,16 +0,0 @@ -import * as React from 'react' - -const isBrowser = typeof document !== 'undefined' - -const useInsertionEffect = React['useInsertion' + 'Effect'] - ? React['useInsertion' + 'Effect'] - : function useInsertionEffect(create) { - create() - } - -export default function useInsertionEffectMaybe(create) { - if (!isBrowser) { - return create() - } - useInsertionEffect(create) -} diff --git a/packages/styled/package.json b/packages/styled/package.json index def6a034b..edf3b187c 100644 --- a/packages/styled/package.json +++ b/packages/styled/package.json @@ -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": { diff --git a/packages/styled/src/base.js b/packages/styled/src/base.js index 74ce54106..29f3a43a3 100644 --- a/packages/styled/src/base.js +++ b/packages/styled/src/base.js @@ -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';". @@ -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) ) diff --git a/packages/styled/src/useInsertionEffectMaybe.js b/packages/styled/src/useInsertionEffectMaybe.js deleted file mode 100644 index 1438d1a7f..000000000 --- a/packages/styled/src/useInsertionEffectMaybe.js +++ /dev/null @@ -1,16 +0,0 @@ -import * as React from 'react' - -const isBrowser = typeof document !== 'undefined' - -const useInsertionEffect = React['useInsertion' + 'Effect'] - ? React['useInsertion' + 'Effect'] - : function useInsertionEffect(create) { - create() - } - -export default function useInsertionEffectMaybe(create) { - if (!isBrowser) { - return create() - } - useInsertionEffect(create) -} diff --git a/packages/use-insertion-effect-with-fallbacks/LICENSE b/packages/use-insertion-effect-with-fallbacks/LICENSE new file mode 100644 index 000000000..56e808dea --- /dev/null +++ b/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. diff --git a/packages/use-insertion-effect-with-fallbacks/README.md b/packages/use-insertion-effect-with-fallbacks/README.md new file mode 100644 index 000000000..d81b8b951 --- /dev/null +++ b/packages/use-insertion-effect-with-fallbacks/README.md @@ -0,0 +1 @@ +# @emotion/use-insertion-effect-with-fallbacks diff --git a/packages/use-insertion-effect-with-fallbacks/package.json b/packages/use-insertion-effect-with-fallbacks/package.json new file mode 100644 index 000000000..f2b935930 --- /dev/null +++ b/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" + ] + } + } +} diff --git a/packages/use-insertion-effect-with-fallbacks/src/index.js b/packages/use-insertion-effect-with-fallbacks/src/index.js new file mode 100644 index 000000000..9109f993c --- /dev/null +++ b/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 diff --git a/yarn.lock b/yarn.lock index b9a1ef099..cf157d328 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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 @@ -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"