-
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
Packages: Stop using Node.js built-ins in browser-packaged code #13386
Comments
As discovered in the course of discussing Automattic/wp-calypso#37606 , since Node 10.0.0 there is a |
As discussed in Automattic/wp-calypso#37606, I will be attempting to rewrite |
@sgomes It's worth noting specifically for Given the following query string object: const paramObj = {
query: [ 'first', 'second' ],
} In order to read the array, PHP will expect it to be serialized as: const queryString = qs.stringify( paramObj )
console.log( queryString )
// query[0]=first&query[1]=second However, const queryString = new URLSearchParams( paramObj )
console.log( queryString.toString() )
// query=first%2Csecond That's not to say it can't be done, just that extra care will need to be taken. |
@chrisvanpatten Good to mention. Though at least based on the tests, I think the expectation is gutenberg/packages/url/src/test/index.test.js Lines 342 to 347 in 6fc0b70
That we have unit tests for this should at least help serve as a base against which an implementation can target. |
@aduth Technically PHP resolves it with or without the explicit key (it assumes a numerically indexed array if no keys are provided), although you are right EDIT: Looking again at the tests, one additional case that is supported, but we don't have a test for, is named keys, e.g.
URLSearchParams chokes a bit on this one, and serializes it as |
To this point, for an implementation, the distinction would likely come in using
Unfortunately it doesn't handle the addition of the |
Thanks, everyone! I'll take this into account. It seems like a PHP idiom, as I believe the more common way of doing things is simply repeating the param name, with no square brackets, e.g. Is there a good source where I can read up on the exact PHP syntax we want to enable? It's trivial to implement the empty square brackets, but since we're talking optional indices too, I want to make sure I'm covering the whole thing.
|
This is definitely supported by |
I found the manual page for Is the goal to ensure that |
I get that from a purely bundle size/complexity perspective supporting the entire API is not ideal, but addQueryArg is used in a bunch of places, and it’s especially important in things like the ServerSideRender component, where the block attributes are passed as args on a GET request. Given the various complex shapes that block attributes can take, I think any implementation needs to strive for as full parity as possible, or it risks breaking functionality for existing users. Personally, I feel like this is a case where qs, with its scope and extensive proven usage, provides enough advantages that outweigh the benefits of a custom implementation. But that’s just me :) |
Thanks, @chrisvanpatten! I suppose this is a case where it's hard to move away from Given the situation, I'll drop my effort in making |
When adopting URL, we should take care to provide a polyfill for environments where it is not supported (namely, IE11). There is prior art for how this has been done in core:
Until it can be patched in core, I expect we can Since we're already using https://github.com/Financial-Times/polyfill-library/blob/master/polyfills/URL/polyfill.js |
This is largely resolved by #19823. As of those changes, there are no longer any occurrences of the Node URL module. There is also #20693 awaiting review which would further consolidate logic by dropping the As a follow-up task, I've created #21647 as a minimal step to change Webpack configuration to forbid its reintroduction. As best I can tell, Webpack is following through on the promise to remove Node polyfills in the next major version. |
We should seek to move away from referencing Node.js built-ins from modules, e.g.
import { parse } from 'url';
, where'url'
is a reference to a browser-compatible variant of the Nodeurl
module.Reasons include:
qs
in the@wordpress/url
packageInstances:
gutenberg/packages/block-library/src/embed/embed-preview.js
Line 10 in 837fda7
gutenberg/packages/editor/src/editor-styles/transforms/url-rewrite.js
Line 4 in 837fda7
(Questionable: Instances within
@wordpress/e2e-tests
and@wordpress/e2e-test-utils
? These may be compiled with Webpack, despite not being intended for use in a browser)With this, it should be considered to disable automatic polyfills from Webpack (reference).
The text was updated successfully, but these errors were encountered: