-
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
Feature: Add webpack plugin to externalize and extract script dependencies #14869
Feature: Add webpack plugin to externalize and extract script dependencies #14869
Conversation
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.
Nice!
I wonder, instead of hardcoding the externals and the handle ref to use, could we make that configurable via an options array? This would allow WordPress plugins to use this for their own externals as well.
So the options config would be an array with two callbacks. The first callback would be used for mapping externals, the second callback would be for mapping to the json output. It could have the shape:
const options = [
( request ) => { /* eternalized dependencies callback for mapping external to */ },
( request ) => { /* dependencies map callback */ }
];
This way it's somewhat extensible by WP plugins that might want to use this package for non WP externals.
This is just an idea, the actual shape of the configuration option for callbacks could likely be iterated on a bit (eg - should callbacks always be provided and the application merged? Should the wp externals be hardcoded/permanent or can they be overridden?)
Extensibility and configuration is missing at this time. I had in mind a few options that might be helpful. Configuration for plugin extensibility allowing plugins provide script dependencies and build bundles that externalize them.
I can imagine cases where options like these might be helpful as well:
|
Related issue: #14837 Am I correct in thinking this should close the issue once merged? |
Yes, thanks. I've updated the description to reflect that. |
I don't see the following file removed in this PR: If that can be easily squeezed in then sure, we can consider it as closing the issue :) |
I had wondered that as well, whether this would allow for this file to be removed. |
9490fd5
to
eb53984
Compare
I've rebased, addressed some feedback, and implemented the configuration outlined in #14869 (comment) I haven't had an opportunity to test the new functionality yet. |
For those following along, this pull request was discussed during today's JavaScript Chat (link requires registration): https://wordpress.slack.com/archives/C5UNMSU4R/p1554818548169400 |
Co-Authored-By: sirreal <jon.surrell@automattic.com>
d7d964a
to
263e409
Compare
Rebased.
This review prompted me to reconsider the current approach and take some time to review webpack internals. I've pushed a small change to use
Notably, object paths may be provided as arrays of strings, for example With these finishing touches, I'm satisfied with this initial implementation 🙂 |
There is a failing e2e test:
The same test appears to be failing on most recent branches so I suspect the failure is unrelated. |
This is how it looks after changes applied: development /***/ "@wordpress/dom-ready":
/*!*******************************************!*\
!*** external {"this":["wp","domReady"]} ***!
\*******************************************/
/*! no static exports found */
/***/ (function(module, exports) {
(function() { module.exports = this["wp"]["domReady"]; }());
/***/ }) production 194:function(e,t){!function(){e.exports=this.wp.domReady}()}, It looks like it's identical to what we have in |
We made it happen, awesome work @sirreal. It will open new ways for optimizing how JS dependencies are managed for plugin authors and WordPress core as well. 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 |
@gziolo any idea on when this will be published? |
Whenever WordPress trunk is open for patches after WordPress 5.2 release process is close to ready? We might wait until plugin release. I hope this will happen at the end of the month the latest. |
Great work @sirreal 👍 @nerrad I expect this to be published once Core |
Description
Add a webpack plugin that externalizes WordPress script dependencies and adds a dependency manifest per entrypoint. The goal is to eliminate error-prone process of manually creating and maintaining script dependencies.
A JSON file is output alongside each entrypoint. That looks like (webpack output):
More detail in the README.
ClosesPart of #14837To do
scripts
How has this been tested?
The plugin is put to use in the Gutenberg webpack config. It can be tested using
npm run build
.Plugin output results
a11y
annotations
api-fetch
autop
blob
block-editor
block-library
block-serialization-default-parser
block-serialization-spec-parser
blocks
components
compose
core-data
data
date
deprecated
dom
dom-ready
edit-post
edit-widgets
editor
element
escape-html
format-library
hooks
html-entities
i18n
is-shallow-equal
keycodes
list-reusable-blocks
notices
nux
plugins
priority-queue
redux-routine
rich-text
shortcode
token-list
url
viewport
wordcount
Types of changes
New feature:
Adds a new package
@wordpress/dependency-extraction-webpack-plugin
.New feature:
Uses the plugin in
@wordpress/scripts
(will require version bump, not included initially). This can be part of another PR but is useful for testing.Checklist: