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

Create constants file for wp- prefixes #142

Merged
merged 1 commit into from
Feb 2, 2023

Conversation

DAreRodz
Copy link
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).

@@ -19,20 +24,20 @@ export function toVdom(node) {

for (let i = 0; i < attributes.length; i++) {
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
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
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
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
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