-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 script and style REST APIs #21244
base: trunk
Are you sure you want to change the base?
Add script and style REST APIs #21244
Conversation
cc: @StevenDufresne as I think this ticket may be important for the block directory work. |
Noting: In order to make use of this endpoint in the Block Directory Package, we will need to update the code to Coincidentally, I have a PR open that does exactly that : #20001. |
Co-Authored-By: Steven Dufresne <[email protected]>
Worth noting this is WIP. It has no tests and only enough comments and formatting that travis passes. It really needs someone from GB team to look and maybe use before we continue with more PHP work. I don't want to spend time on a none starter. |
cc @ockham this relates to some stuff we've discussed related to more async loading of things in the editor |
As mentioned at #21684 (comment) I'm not sure how the REST API should be used together with core's Script Loader. Is the purpose to duplicate script loader in JS, or perhaps partially extend it, or..? If the current Script Loader must be used, it seems a lot simpler to just add a function to it that will take a script's handle as param, run WP, calculate the dependencies, and return either the script tags (including inline scripts) or the concatenated JS as blob, or... anything else that may be useful. Of course that will need a list (array, CSV, etc.) of already loaded script handles that will be excluded from the calculated dependencies. |
@azaozz This REST API allows you to pass a script and style handle and it return an array of javascript / css files. From that, a script / style tag could easily be generated in gutenberg. As for concatting javascript / css files. Little worried about this that jQuery are react might be reloaded unnecessarily. Feel like returning the data is more useful. |
Now that #21065 has been merge, I have updated the PR with embedded script and styles. Meaning that urls are for scripts can be requested as part of api request. What you do think of this PR @TimothyBJacobs ? |
Right, but an array of URIs to javascript files doesn't include all of the dependencies. There are also the "inline" dependencies (the js code added with For example, <script>
var mejsL10n = {"language":"en","strings":{"mejs.download-file":"Download File", ... }};
</script>
<script src='/wp-includes/js/mediaelement/mediaelement-and-player.js?ver=4.2.13-9993131'></script>
<script src='/wp-includes/js/mediaelement/mediaelement-migrate.js?ver=5.5-alpha-20200526.094621'></script>
<script>
var _wpmejsSettings = {"pluginPath":"\/wp-includes\/js\/mediaelement\/","classPrefix":"mejs-","stretching":"responsive"};
</script>
<script src='/wp-includes/js/mediaelement/wp-mediaelement.js?ver=5.5-alpha-20200526.094621'></script> As far as I see currently there is no support for the JS code that goes in the two Looking at how the inline scripts can be supported, there are generally two methods:
|
@azaozz Have you reviewed the output of the api? Take this example from - /wp/v2/scripts/wp-ajax-response
As you can see, the dependies are an listed as an array and all the extras are including, like translated strings. This is all the data is required to rebuild the enqueue system in javascript. |
Hmmmm, don't see this here? Will reset caches/npm and try again, sorry. |
Got it to work, was my error, sorry :) Couple of quick notes:
handle: "wp-mediaelement"
src: "/wp-includes/js/mediaelement/wp-mediaelement.js"
...
url: "https://site.tld/wp-includes/js/mediaelement/wp-mediaelement.js" However testing with handle: "mediaelement"
src: false
...
url: "/wp-json/wp/v2/scripts/mediaelement?ver=4.2.13-9993131" Wondering why the discrepancy for the
There is a discrepancy with the extra:
data: "var _wpmejsSettings = ..."
handle: "mediaelement" extra:
before: Array(2)
0: false
1: "var mejsL10n = ..."
handle: "mediaelement-core" Wondering if the API should be normalizing these in some way or at least removing the empty/invalid elements? On the server that data is not for "direct consumption", it's just a storage that shouldn't be accessed directly. Also note that plugins can add more data there, so an array is probably the proper format. |
Another thing that needs fixing is to perhaps run the |
Co-authored-by: Jonny Harris <[email protected]>
Co-authored-by: Jonny Harris <[email protected]>
…ey/gutenberg into feature/dependency-api-tweaks
Dependencies API tweaks
Thanks for your work here. I'm closing this PR now as we have import maps as an alternative now on Core. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @[email protected]. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Description
Create REST APIs for registered scripts and styles.
See original ticket at #48654
How has this been tested?
Endpoints can be found at
/__experimental/scripts
and/__experimental/styles
Checklist: