Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Create constants file for wp- prefixes#142

Merged
DAreRodz merged 1 commit intomain-wp-directives-pluginfrom
define-prefixes-as-constants
Feb 2, 2023
Merged

Create constants file for wp- prefixes#142
DAreRodz merged 1 commit intomain-wp-directives-pluginfrom
define-prefixes-as-constants

Conversation

@DAreRodz
Copy link
Copy Markdown
Collaborator

@DAreRodz DAreRodz commented Jan 27, 2023

What

Move all wp- prefixes (or strings containing any of them) to a constants file.

Why

To make it easier to change them if necessary (e.g., directivePrefix to data-wp-).

How

Creating a constants.js file.

https://github.com/WordPress/block-hydration-experiments/blob/4990097fef3f3c674eee195a3ea4bb2fd460c7cd/src/runtime/constants.js#L1-L3

I defined separated prefixes for components and directives, just in case we finally go for HTML-compliant directives (i.e., using custom data attributes).

const n = attributes[i].name;
if (n[0] === 'w' && n[1] === 'p' && n[2] === '-' && n[3]) {
if (n === 'wp-ignore') {
if (n[p.length] && n.slice(0, p.length) === p) {
Copy link
Copy Markdown
Collaborator Author

@DAreRodz DAreRodz Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed how we check the attribute prefix. It turns out that using String.prototype.slice is much faster than the other approaches we tested a while ago.

The previous method that checked fixed positions is ~72% slower than the current one using slice. 😄

https://jsbench.me/5lldeo3dr8/1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just blew my mind.

Copy link
Copy Markdown
Collaborator

@ockham ockham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM 👍 (I wasn't quite sure how to test though -- I take it we have some e2e coverage?)

@DAreRodz
Copy link
Copy Markdown
Collaborator Author

DAreRodz commented Feb 2, 2023

I wasn't quite sure how to test though -- I take it we have some e2e coverage?

We have e2e tests for full vDOM hydration, islands, and some directives. If none of them has broken, I guess these changes work. 🤞

@DAreRodz DAreRodz merged commit e640394 into main-wp-directives-plugin Feb 2, 2023
@DAreRodz DAreRodz deleted the define-prefixes-as-constants branch February 2, 2023 09:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants