Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Bring back tree shaking #91

Merged
merged 105 commits into from
Mar 9, 2023
Merged

Bring back tree shaking #91

merged 105 commits into from
Mar 9, 2023

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Mar 6, 2023

Brings back Phil's image tree shaking. Phil did almost all of the work already.

Fixes part of #57 (Ensure assets save appropriately)

How to test

  1. Create a new pattern
  2. Add an Image block
  3. Click 'Insert from URL'
  4. Enter https://via.placeholder.com/640x350
  5. Click 'Create Pattern'
  6. Reload the browser
  7. Expected: The image still renders (though this worked before):

Screenshot 2023-03-07 at 3 25 07 PM

  1. In the Image block, click 'Replace'
  2. Scroll to the right, and click the pencil:

Screenshot 2023-03-07 at 3 27 05 PM

  1. Enter https://via.placeholder.com/640x351
  2. Click 'Update Pattern'
  3. Reload the browser
  4. Expected: The image still displays:

Screenshot 2023-03-07 at 3 28 44 PM

  1. Go to <active theme>/patterns/images/
  2. Expected: There's a 640x351 image, but not 640x350:

Screenshot 2023-03-07 at 3 30 11 PM

@kienstra kienstra mentioned this pull request Mar 6, 2023
11 tasks
// Spin up the filesystem api.
$wp_filesystem = \PatternManager\GetWpFilesystem\get_wp_filesystem_api();

function tree_shake_theme_images( $wp_filesystem, $copy_dir ) {
Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

Adding 2 parameters isn't good.

But it allows testing this function.

Mocking the $wp_filesystem global was really hard, I couldn't figure it out.

@@ -401,7 +402,7 @@ function tree_shake_theme_images() {
}

// Before we take any action, back up the current images directory.
copy_dir( $images_dir, $backedup_images_dir );
call_user_func( $copy_dir, $images_dir, $backedup_images_dir );
Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

In production, this line should work the same as before.

The copy_dir function is passed as $copy_dir.

Before, this line blocked testing tree_shake_theme_images().

I couldn't figure out a way around it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like a minor tradeoff to enable testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

[
$this->get_fixtures_directory() . '/patterns/images/used.jpg',
],
$wp_filesystem->get_copied()
Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

This tests that it tree shakes the unused image (not-used.jpg) out of fixtures/patterns/images/.

But it keeps the used image (used.jpg).

@kienstra kienstra force-pushed the add/tree-shaking-back-in branch from f93f30f to bf3bb5f Compare March 7, 2023 21:13
@@ -79,6 +81,8 @@ function save_pattern_to_file( WP_Post $post ) {
);

add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' );

tree_shake_theme_images( get_wp_filesystem_api(), 'copy_dir' );
Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

Before, tree_shake_theme_images() ran in update_pattern().

And that ran on post and meta updates.

Now, this only runs on post updates.

post_content should only change on post updates, and that's the only thing tree shaking applies to.

Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

Maybe tree shaking should also run on delete_pattern(), but I like the simplicity 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

It could be a nice touch to also run on delete_pattern imo — not a huge thing by any means, but I think it would make sense to destroy the asset along with the pattern file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied in 8534c8e

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, this is working well! Images are deleted along with the pattern file as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for testing this

@@ -116,6 +116,8 @@ function format_pattern_data( $pattern_data, $file ) {
$pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain );
}

opcache_invalidate( $file );
Copy link
Contributor Author

@kienstra kienstra Mar 7, 2023

Choose a reason for hiding this comment

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

Thanks to Phil's suggestion, this was getting an outdated cached version of this file.

This fixed it in my local, let me know if this fixes it for you.

@kienstra kienstra marked this pull request as ready for review March 7, 2023 21:32
@kienstra
Copy link
Contributor Author

kienstra commented Mar 8, 2023

Hi @johnstonphilip, does tree shaking work in your local with this?

Copy link
Contributor

@mike-day mike-day left a comment

Choose a reason for hiding this comment

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

This seems to be working well, @kienstra!

Upon removing an image from a given pattern and saving the post, the image is removed from the images subfolder.

I am also not seeing any caching issues as I have in the past, though they previously would appear sporadically.


Really nice work rounding this one out! You are @johnstonphilip are both monsters!

@kienstra
Copy link
Contributor Author

kienstra commented Mar 9, 2023

Hi @mike-day,
Thanks so much for testing this, and for you idea to run on delete_pattern(). Really appreciate your review!

@johnstonphilip
Copy link
Contributor

Amazing! This is actually working really well now. Excellent work @kienstra

@kienstra
Copy link
Contributor Author

kienstra commented Mar 9, 2023

Thanks so much, @johnstonphilip! You did the work, this just brings it back 😄

@kienstra kienstra merged commit 0a55171 into main Mar 9, 2023
@kienstra kienstra deleted the add/tree-shaking-back-in branch March 9, 2023 20:30
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