site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Enhance implementation of assets manifest

Open aaemnnosttv opened this issue 5 years ago β€’ 14 comments

Feature Description

In #1700 we updated our build to produce JS entry point files with content hashes in the file name. As part of this, we introduced a manifest for our built assets as a PHP class which was essentially just for accessing the map of handle -> dist file path. One quirk of the approach is that the Manifest class is currently git-ignored because it is written by the webpack build process. This introduces an odd order of operations related side-effect where assets may not load properly after doing an initial build depending on the order that dependencies were installed (npm install or composer install). If composer is run before npm, the Manifest class will not be present in the resulting autoloader.

While we no longer generate hashed filenames or a manifest for development builds, this is something we can improve to enhance the developer experience, and keep things more consistent across the codebase.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The \Google\Site_Kit\Core\Assets\Manifest class should no longer be generated during the build, but should be a persistent non-git-ignored class like all other core classes
  • The webpack manifest plugin should output the manifest as a PHP file into ./dist/manifest.php (see discussion below)
  • The manifest mapping should not be specific to scripts only
    • Accordingly, the [contenthash] should be added to filenames for stylesheets in production builds in the same way we construct filenames for scripts

Implementation Brief

  • Update the webpack config file:
    • Define an empty object constant in the global scope: const seed = {};
    • Define the shared arguments for the ManifestPlugin class: const manifestArgs = { ... }:
      • It should have the fileName property that points to the following location: dist/manifest.php
      • It should have the serialize property that does the same thing as the current serialize callback in the ManifestPlugin declaration for main entries with two changes:
        • Instead of removing just .js suffixes, it should remove .css and .js suffixes from entry names.
        • It should return a generated php script that returns an associated array:
          <?php
          
          return array(
              'scriptA' => 'filenameA.js',
              ...
          );
          
    • Update the filename option of the MiniCssExtractPlugin plugin to use the [contenthash] tag (/assets/css/[name].[contenthash].css) for production build. For non-production builds, this option value should remain as is right now.
    • Update ManifestPlugin options for the main entries config:
      • Remove the fileName option
      • Remove the serialize callback
      • Destruct the manifestArgs object to copy shared options.
    • Add a new ManifestPlugin plugin to the plugins list used for CSS entries:
      • It should use a custom callback for the filter option that allows files that end with .css extension only.
      • Destruct the same manifestArgs object to copy shared options.
  • Remove the /includes/Core/Assets/Manifest.php line from the .gitignore file.
  • Add a new /includes/Core/Assets/Manifest.php file with the Manifest class if it doesn't exist yet.
  • Add a new static method get_filename( $handler ) to the new Manifest class that includes the dist/manifest.php file on the first call and returns matching filename if it exists. If the provided $handler doesn't have the associated filename, it should return NULL.
  • Update the Script::register method to use the new Manifest::get_filename method instead of Manifest::$assets[...].
  • Update the Advanced_Tracking\Script_Injector::inject_event_script method to use the new Manifest::get_filename method instead of Manifest::$assets[...].
  • Update the Stylesheet::register method to use the same approach as we use in the Script::register method to determine the correct src for the stylesheet.
  • Update the bin/release script to copy the new dist/manifest.php file in the same way as we copy other files: https://github.com/google/site-kit-wp/blob/f54254f2bf2e71d6660c4f5f3510c89027a048bd/bin/release#L112-L120

Test Coverage

  • N/A

Visual Regression Changes

  • N/A

QA Brief

  • Build the dev version npm run build:dev and ensure css and js files load.
  • Build the prod version npm run build and ensure css and js files load.
  • Build the release zip npm run zip and ensure it contains the dist/manifest.php.

Changelog entry

  • Update CSS files to be built with unique file names for production builds.

aaemnnosttv avatar Feb 15 '21 09:02 aaemnnosttv

Technically speaking using and loading json files on every request is a bad practice because it is FS bound operation which is slower than including PHP files (because PHP files are cached with OPCache). The assets manifest was originally designed to be a PHP class intentionally.

I know that it is not a big deal, but at the same time, if we look at other parts of the plugin, we will see that we try to use micro-optimizations to make it slightly more performant. For example, we use the classmap approach for autoloading because it is slightly faster than the dynamic autoloader.

eugene-manuilov avatar Feb 15 '21 18:02 eugene-manuilov

@eugene-manuilov I'm not against implementing the manifest in PHP so much as I am generating a class as part of our assets build process since it causes problems with the autoloader but it's also weird to have generated code mixed with static source code.

With that in mind, I think the better approach would be to generate the manifest with PHP doing something like this:

<?php // dist/manifest.php
return array( /* ... * / );

Then in the Manifest class we could read this into a private static variable on demand if it wasn't set yet.

If that sounds good to you, I'd be happy to update the ACs to allow for it?

aaemnnosttv avatar Feb 17 '21 18:02 aaemnnosttv

Yep, it is better than the JSON approach even though it looks a bit cumbersome. Sounds good to me.

eugene-manuilov avatar Feb 18 '21 16:02 eugene-manuilov

The manifest mapping should not be specific to scripts only

@aaemnnosttv there is nothing else that we can use the Manifest plugin for. We need it only for scripts.

eugene-manuilov avatar Sep 07 '21 13:09 eugene-manuilov

@eugene-manuilov I'm not sure what you mean, surely we can use it for stylesheets as well?

Cached styles frequently come up as a problem in QA similar to scripts, they just don't break the app πŸ˜„

aaemnnosttv avatar Sep 07 '21 15:09 aaemnnosttv

@aaemnnosttv if we want to use it for styles as well, then we need to update AC to say that we need to add the [contenthash] tag to stylesheet files. Otherwise, it doesn't make sense to use the manifest file for styles. Do we want to change stylesheet filenames?

Also, it is worth noticing that if we enable hashes for stylesheets, we will have two manifest files because styles and scripts are two different jobs.

eugene-manuilov avatar Sep 07 '21 15:09 eugene-manuilov

if we want to use it for styles as well, then we need to update AC to say that we need to add the [contenthash] tag to stylesheet files. Otherwise, it doesn't make sense to use the manifest file for styles. Do we want to change stylesheet filenames?

Agreed, I'll update the ACs to update the filenames used for stylesheets in production builds.

Also, it is worth noticing that if we enable hashes for stylesheets, we will have two manifest files because styles and scripts are two different jobs.

The webpack manifest plugin has a seed argument which is for generating a single manifest across multiple compilations. I created a POC a while back which I just found. Here is the relevant part from the webpack config – the important detail here is that manifestSeed is defined outside of the args since its used as an accumulator.

const manifestSeed = {};
const manifestArgs = {
	basePath: '',
	seed: manifestSeed,
	filter: ( file ) => /\.map$/.test( file.path ) === false,
	generate: ( seed, files ) => {
		files.forEach( ( file ) => {
			if ( file.isChunk ) {
				// Map chunk id (entry key) -> path from dist root
				seed[ file.chunk.id ] = file.path;
			} else {
				seed[ file.name ] = file.path;
			}
		} );

		return seed;
	},
};

A really nice to have here (I think) would be to always generate the manifest – for development builds too but keep the non-hashed file names (due to the problems we had before). For dev builds, we could register the content hash as the asset's version instead which would be a really nice enhancement. This is maybe getting a bit out of scope though and we could handle it in a separate issue instead. What do you think?

aaemnnosttv avatar Sep 07 '21 16:09 aaemnnosttv

The webpack manifest plugin has a seed argument which is for generating a single manifest across multiple compilations. I created a POC a while back which I just found. Here is the relevant part from the webpack config – the important detail here is that manifestSeed is defined outside of the args since its used as an accumulator.

@aaemnnosttv did you try it for our css files? Unfortunately, it doesn't seem to work on my end. Probably because we use the MiniCssExtractPlugin plugin which emits files bypassing the manifest plugin. :thinking:

eugene-manuilov avatar Sep 07 '21 18:09 eugene-manuilov

@eugene-manuilov I see what you mean. I get CSS files with hashes in the name but the paths in the manifest don't have the hashes for the reason you mention.

I wonder if MiniCssExtractPlugin is the right thing to use here as it seems to be designed for loading CSS via JS rather than loading as a "traditional" asset the way we do πŸ€” What do you think?

We can always handle styles in a separate issue if it's overly complicated. The main thing here was that the manifest itself wasn't script-specific. If we were to fix the way stylesheets were generated, the manifest should just work.

aaemnnosttv avatar Sep 09 '21 08:09 aaemnnosttv

@aaemnnosttv, I figured out how to make it work with css files too. We just don't need to use the generate option. IB is updated.

eugene-manuilov avatar Sep 09 '21 12:09 eugene-manuilov

I figured out how to make it work with css files too

@eugene-manuilov nice job!

IB βœ…

aaemnnosttv avatar Sep 10 '21 06:09 aaemnnosttv

QA: βœ…

  • Built the dev version, CSS and JS files load fine.
  • Built the prod version, CSS and JS files load fine.
  • Built the release zip and it contains the dist/manifest.php.

asvinb avatar Sep 28 '21 10:09 asvinb

QA ❌

CSS files are now being generated in the wrong place. Will open a quick PR to fix.

aaemnnosttv avatar Sep 28 '21 15:09 aaemnnosttv

QA βœ… All of the below seemed present and correct.

  • Build the dev version npm run build:dev and ensure css and js files load. βœ…
  • Build the prod version npm run build and ensure css and js files load. βœ…
  • Build the release zip npm run zip and ensure it contains the dist/manifest.php. βœ…

johnPhillips avatar Sep 30 '21 13:09 johnPhillips