-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Rewrite sideEffects flags to use only positive patterns #26452
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,6 @@ | |
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"sideEffects": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module seems to export an |
||
"build-style/**", | ||
"!((src|build|build-module)/components/**)" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.11.2", | ||
"@wordpress/api-fetch": "file:../api-fetch", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,6 @@ | |
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"sideEffects": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package has a whole bunch of components, and this way none of them are modularised 😞 I think that it loads all of them anyway, so it should be fine, but it's hard to tell. And since there's an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean components like If you don't use these pseudo-components, it doesn't mean they can be removed from the bundle. The corresponding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right, I missed the re-exports at the end of the file, my bad 👍 |
||
"build-style/**", | ||
"!((src|build|build-module)/(components|utils)/**)" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.11.2", | ||
"@wordpress/a11y": "file:../a11y", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,6 @@ | |
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"sideEffects": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
"build-style/**", | ||
"!((src|build|build-module)/components/**)" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.11.2", | ||
"@wordpress/a11y": "file:../a11y", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,6 @@ | |
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"sideEffects": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
"build-style/**", | ||
"!((src|build|build-module)/components/**)" | ||
], | ||
"dependencies": { | ||
"@babel/runtime": "^7.11.2", | ||
"@wordpress/api-fetch": "file:../api-fetch", | ||
|
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 package confuses me. How is it supposed to be used?
Is it meant to be a single side effect, by doing just
import '@wordpress/block-directory';
? I'm seeing a bunch of components in there that don't seem to be loaded directly, but I guess they're all loaded transitively?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.
block-directory
is a JS plugin. You just load the script and it hooks into thewp.data
,wp.hooks
andwp.plugins
APIs, adding a new feature to the editor.If you were building your own app with the NPM packages, the package would be activated by a simple side-effectful import, as you wrote. The APIs it hooks into are (ideally) peer dependencies of the
block-editor
package.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.
These are probably all used to implement the plugin UI, rather than being exported for 3rd party consumers. Are there any specific components that look suspicious?
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.
None in particular. I expect them all to be used to implement the plugin UI, yes, unless one of them got dropped over time.
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.
We could make it side effects free. I didn't consider it when refactoring to behave like a plugin. Edit family of packages uses
initialize
methods and we could offer something similar. Definitely a follow up task if you feel like it's worth the hassle.Would it be useful if more functionality would be initialized this way?
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.
Great question, @gziolo!
In general, I feel that any package that could be classified as a library/plugin/utility (that is, something that doesn't invert control) should avoid side effects as much as possible.
Even setting aside the issues with bundlers for a second, initialisation functions generally provide more flexibility, because the consumer has more control over when they're invoked. Consumers can e.g. import something statically and call
initialize()
only when the editor is being loaded; without an initialisation method, they'd have to instead use a dynamic import, which by its async nature could be hard to fit into an existing codebase.In terms of difficulty, it's generally a trivial amount of extra work for the naive use-case anyway. It's really not too bad to go from:
to
This way, users who don't care still get their initialisation at the top level of their module, while users who do care can move it elsewhere.
And on the package side, it reduces guesswork and maintenance around the
sideEffects
property inpackage.json
, as this PR sadly shows 😕So while I don't think it's anything urgent that needs immediate attention, I'd definitely recommend switching to initialisation methods whenever the next code revamp opportunity comes along 🙂
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.
What @sgomes said 🙂 I'll only add that the inflexible initialization was sometimes causing problems in the first Gutenlypso integration 2 years ago. The problematic code usually looks like this:
The
rigid
package needs to be initialized only afterflexible
(because, for example, it doesselect( 'core/flexible' )
during initialization), but I can't do that.import
statements must be at the top level of the module, not inside a callback.Solutions are either using
require( '@wordpress/rigid' )
, or creating an extra async-loaded module that is loaded just-in-time.On the other hand, I think that declaring
sideEffects
on absolutely everything is not needed.sideEffects: false
is useful for libraries where you can use just a little part of the library and want to avoid bundling the rest.For plugins like
block-directory
or apps likeedit-post
we can keep the default where everything is assumed to have side effects, and I don't think it causes much deoptimization if any.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.
In WordPress core we would have to call
wp.blockLibrary.initialize()
but it wouldn't be that much work. It's definitely something to keep in mind when working on new packages.