Skip to content

Commit

Permalink
Add warning if multiple versions are running (addresses #1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs139 committed Dec 6, 2019
1 parent 0b7ebdf commit dd191ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/jest-emotion/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import { registerEmotionModule } from '@emotion/utils'
import * as css from 'css'
import { replaceClassNames } from './replace-class-names'
import {
Expand All @@ -14,6 +15,12 @@ import {

export { matchers } from './matchers'

registerEmotionModule(require('../package.json').name, 'not it')
registerEmotionModule(
require('../package.json').name,
require('../package.json').version
)

function getNodes(node, nodes = []) {
if (Array.isArray(node)) {
for (let child of node) {
Expand Down
30 changes: 29 additions & 1 deletion packages/utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
// @flow
import type { RegisteredCache, EmotionCache, SerializedStyles } from './types'

let isBrowser = typeof document !== 'undefined'
const isBrowser = typeof document !== 'undefined'

const getGlobalVersions = (context: any) => {
const globalVersionsKey = '__EMOTION_VERSIONS__'
if (!context[globalVersionsKey]) {
context[globalVersionsKey] = {}
}
return context[globalVersionsKey]
}

type ModuleVersions = { [key: string]: string }

const versions: ModuleVersions = getGlobalVersions(isBrowser ? window : global)

export function registerEmotionModule(name: string, version: string) {
if (versions[name] && versions[name] !== version) {
console.warn(
`Package ${name}@${
versions[name]
} is already registered. Running ${version} may cause problems.`
)
}
versions[name] = version
}

registerEmotionModule(
require('../package.json').name,
require('../package.json').version
)

export function getRegisteredStyles(
registered: RegisteredCache,
Expand Down
3 changes: 2 additions & 1 deletion scripts/babel-preset-emotion-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-transform-flow-strip-types": "^7.6.3",
"babel-plugin-add-basic-constructor-for-react-components": "^0.1.0",
"babel-plugin-fix-dce-for-classes-with-statics": "^0.1.0"
"babel-plugin-fix-dce-for-classes-with-statics": "^0.1.0",
"babel-plugin-inline-package-json": "^2.0.0"
}
}
3 changes: 2 additions & 1 deletion scripts/babel-preset-emotion-dev/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = () => {
[
require.resolve('@babel/plugin-proposal-class-properties'),
{ loose: true }
]
],
require.resolve('babel-plugin-inline-package-json')
]
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4429,6 +4429,11 @@ babel-plugin-fix-dce-for-classes-with-statics@^0.1.0:
resolved "https://registry.npmjs.org/babel-plugin-fix-dce-for-classes-with-statics/-/babel-plugin-fix-dce-for-classes-with-statics-0.1.0.tgz#9cac930cfb50a879d66cd5dc3bd4af731c1ecb90"
integrity sha512-nya9Uw5te1kWNXGodrftTNQwx3oyPEQWOgg/k0tzViExKv6L1BoyNI0+rkin+FiF1Kp5ZjURjBnuJg/1n8erLA==

babel-plugin-inline-package-json@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/babel-plugin-inline-package-json/-/babel-plugin-inline-package-json-2.0.0.tgz#1f9558d96667f4b8787e807d764e6e15b4f0cd07"
integrity sha1-H5VY2WZn9Lh4foB9dk5uFbTwzQc=

babel-plugin-istanbul@^4.0.0, babel-plugin-istanbul@^4.1.6:
version "4.1.6"
resolved "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45"
Expand Down

0 comments on commit dd191ac

Please sign in to comment.