mediawiki-skins-MinervaNeue/tests/resource-loader-bundlesize.js
jdlrobson 7408d87465 Run bundlesize tests on every commit
It was added in 7340485014
but for some reason never enabled.

This was supposed to be protecting us from performance regressions
by monitoring the size of modules in the repo. It appears to be
broken both locally and on Jenkins when run on the due to some changes
in Phabricator.

Since then we've added bundle size tests to Vector using a different
more foolproof method. I think that method is more preferable as it
also provides coverage for JS and icons so the
associated script is copied over and used in Minerva
and activated on all commits.

Bug: T259080
Change-Id: I9ba2dcc060ec09d91814c947d5be3f71b055a66e
2020-07-28 23:43:38 +00:00

67 lines
2.1 KiB
JavaScript

/**
* ResourceLoader module bandwidth test
*
* Fetches a series of ResourceLoader modules from a ResourceLoader URI and
* pipes their content into a bundlesize command which validates their bytesize
* against a configuration in in ../bundlesize.config.json.
*/
const
MW_SERVER = process.env.MW_SERVER || 'http://127.0.0.1:8080',
MW_SCRIPT_PATH = process.env.MW_SCRIPT_PATH || '/w',
childProcess = require( 'child_process' ),
fetch = require( 'node-fetch' ).default,
bundles = require( '../bundlesize.config.json' ),
// eslint-disable-next-line es/no-object-assign
bundlesizeEnv = Object.assign( {}, process.env );
/**
* Create a ResourceLoader URL based on $MW_SERVER and $MW_SCRIPT_PATH
*
* @param {string} rlModuleName
* @return {string}
*/
function createRLUrl( rlModuleName ) {
return `${MW_SERVER}${MW_SCRIPT_PATH}/load.php?lang=en&modules=${rlModuleName}`;
}
/**
* Remove the "CI" environment variable so that it is not passed to
* the bundlesize command, resulting in an unnecessary "Github token not found"
* warning. Note: assigning to null or undefined doesn't work because Node casts
* environment variable values to strings.
*/
delete bundlesizeEnv.CI;
/**
* Fetch each ResourceLoader module defined in the bundlesize config
* and pipe it's content into a bundlesize command.
* The bundlesize stdout and stderr are passed to the parent process,
* so any error in bundlesize will trigger a non-zero exit status for this script.
*/
bundles.forEach( async ( rlModule ) => {
const rlModuleResponse = await fetch( createRLUrl( rlModule.resourceModule ) ),
rlModuleContent = await rlModuleResponse.text();
// Execute the bundlesize command.
const cmd = childProcess.spawn(
'node_modules/.bin/bundlesize-pipe',
[ '--name', rlModule.resourceModule, '--max-size', rlModule.maxSize ],
{
// Stdin is writable stream, stdout and stderr are passed to the parent process.
stdio: [ 'pipe', 'inherit', 'inherit' ],
env: bundlesizeEnv
}
);
cmd.stdin.write( rlModuleContent );
cmd.stdin.end();
cmd.on( 'exit', ( code ) => {
if ( code !== 0 ) {
process.exitCode = code || 1; // prevent assigning falsy values.
}
} );
} );