Enhance implementation of assets manifest
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\Manifestclass 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
- Accordingly, the
Implementation Brief
- Update the webpack config file:
- Define an empty object constant in the global scope:
const seed = {}; - Define the shared arguments for the
ManifestPluginclass:const manifestArgs = { ... }:- It should have the
fileNameproperty that points to the following location:dist/manifest.php - It should have the
serializeproperty that does the same thing as the current serialize callback in theManifestPlugindeclaration for main entries with two changes:- Instead of removing just
.jssuffixes, it should remove.cssand.jssuffixes from entry names. - It should return a generated php script that returns an associated array:
<?php return array( 'scriptA' => 'filenameA.js', ... );
- Instead of removing just
- It should have the
- Update the
filenameoption of theMiniCssExtractPluginplugin 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
ManifestPluginoptions for the main entries config:- Remove the
fileNameoption - Remove the
serializecallback - Destruct the
manifestArgsobject to copy shared options.
- Remove the
- Add a new
ManifestPluginplugin to the plugins list used for CSS entries:- It should use a custom callback for the
filteroption that allows files that end with.cssextension only. - Destruct the same
manifestArgsobject to copy shared options.
- It should use a custom callback for the
- Define an empty object constant in the global scope:
- Remove the
/includes/Core/Assets/Manifest.phpline from the.gitignorefile. - Add a new
/includes/Core/Assets/Manifest.phpfile with theManifestclass if it doesn't exist yet. - Add a new static method
get_filename( $handler )to the newManifestclass that includes thedist/manifest.phpfile on the first call and returns matching filename if it exists. If the provided$handlerdoesn't have the associated filename, it should returnNULL. - Update the
Script::registermethod to use the newManifest::get_filenamemethod instead ofManifest::$assets[...]. - Update the
Advanced_Tracking\Script_Injector::inject_event_scriptmethod to use the newManifest::get_filenamemethod instead ofManifest::$assets[...]. - Update the
Stylesheet::registermethod to use the same approach as we use in theScript::registermethod to determine the correctsrcfor the stylesheet. - Update the
bin/releasescript to copy the newdist/manifest.phpfile 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:devand ensure css and js files load. - Build the prod version
npm run buildand ensure css and js files load. - Build the release zip
npm run zipand ensure it contains the dist/manifest.php.
Changelog entry
- Update CSS files to be built with unique file names for production builds.
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 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?
Yep, it is better than the JSON approach even though it looks a bit cumbersome. Sounds good to me.
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 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 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.
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?
The webpack manifest plugin has a
seedargument 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 thatmanifestSeedis 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 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, 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.
I figured out how to make it work with css files too
@eugene-manuilov nice job!
IB β
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.
QA β
CSS files are now being generated in the wrong place. Will open a quick PR to fix.
QA β All of the below seemed present and correct.
- Build the dev version
npm run build:devand ensure css and js files load. β - Build the prod version
npm run buildand ensure css and js files load. β - Build the release zip
npm run zipand ensure it contains the dist/manifest.php. β