-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add warning if multiple versions are running (addresses #1470) #1677
Conversation
🦋 Changeset is good to goLatest commit: b29b958 We got this. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
dd191ac
to
5c548f3
Compare
packages/utils/src/index.js
Outdated
registerEmotionModule( | ||
require('../package.json').name, | ||
require('../package.json').version | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more here for usage, it may be decided that it's OK for this module to have multiple concurrent versions.
packages/jest-emotion/src/index.js
Outdated
@@ -1,4 +1,5 @@ | |||
// @flow | |||
import { registerEmotionModule } from '@emotion/utils' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this only matters for @emotion/core
package - don't think there is a much need for this in other packages
packages/utils/src/index.js
Outdated
|
||
export function registerEmotionModule(name: string, version: string) { | ||
if (versions[name] && versions[name] !== version) { | ||
console.warn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not only about different versions (but that too) - the problem might also be caused by requiring the same version twice (from different locations at disk).
I think react-router prepared this pretty much in a way that I'd like to have this included here, take a look:
https://github.com/ReactTraining/react-router/blob/e81dfa2d01937969ee3f9b1f33c9ddd319f9e091/packages/react-router/modules/index.js#L1
For that to work exactly like in their case we'd have to have process.env.ROLLUP_MODULE_FORMAT
available - this could potentially be added in our build tool (preconstruct). I expect supporting this would only require changes around those lines:
https://github.com/preconstruct/preconstruct/blob/120f9b6d747e985ed917a898f13d7177fd74ef73/packages/cli/src/build/rollup.ts#L164-L168
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to warn based on module type as well as version, we would want to split this up:
https://github.com/preconstruct/preconstruct/blob/c1cfc0503f73a197487d6cba6ada607fe957f527/packages/cli/src/build/config.ts#L132-L153
If we allowed the messaging to be more vague, then we wouldn't need to do an explicit check of the module type. For example, we could change the code to this:
if (__DEV__) {
if (versions[name]) {
if (versions[name] === version) {
console.warn(
`Package ${name}@${versions[name]} is already registered,` +
`so things won't work right. This would only happen if you are `+
`loading two different builds of ${name}.`
);
} else {
console.warn(
`Package ${name}@${versions[name]} is already registered.` +
`Running ${version} may cause problems.`
);
}
}
versions[name] = version;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of this is to warn about second copy of @emotion/core
being loaded. It doesn't really matter what was the first version being loaded and it doesn't even matter what is the second one - we only need types of both to provide a helpful error message. I would limit this to a build type error - not sure if we need to complicate this with providing also an exact version.
5c548f3
to
2d82de4
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit b29b958:
|
2d82de4
to
0a2cdcc
Compare
packages/utils/src/index.js
Outdated
const instances: ModuleRegister = getGlobalInstances(globalContext) | ||
|
||
export function registerEmotionModule(name: string) { | ||
if (process.env.NODE_ENV !== 'production') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried using babel-plugin-dev-expression
, but it was causing failures when running the test suite.
Codecov Report
|
packages/utils/src/index.js
Outdated
|
||
const instances: ModuleRegister = getGlobalInstances(globalContext) | ||
|
||
export function registerEmotionModule(name: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this abstraction is much needed, we only need this for a single module (@emotion/core
), so I would prefer avoiding extra abstractions and just inlining this stuff to @emotion/core
's index
I also don't think we really need tests for this - it's rather hard to break it and writing a proper test (without testing implementation details) for this thing would be rather brittle and complicated
0a2cdcc
to
9955147
Compare
packages/core/src/utils.js
Outdated
const globalKey = '__EMOTION_CORE__' | ||
const globalContext = isBrowser ? window : global | ||
|
||
export function checkForMultipleVersions() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would have to be called to actually make it work :p however, calling this as a function could lead to this not being properly removed in production, so I would just move this function's content to core/src/index.js
and this should be good to go 👍
9955147
to
e275b57
Compare
e275b57
to
1410fcb
Compare
@ajs139 the only thing missing is an appropriate changeset, could you add it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than adding a changeset, LGTM
…) (emotion-js#1677) * Add warning if multiple versions are running (addresses emotion-js#1470) * Add changeset
What:
Add a register of all the
@emotion
packages, so that a warning can be displayed if the same package is registered with multiple versions.Why:
Running multiple versions of the packages can cause hard to trace issues, see #1470.
How:
Each module "registers" itself providing its name and version into a global object. If that module name is already registered for a different version, a warning is displayed.
This is just a draft, for discussion on the approach.
Checklist: