From 86c969369325566e94de3659129d4db7a6ad27ca Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Fri, 30 Sep 2022 18:25:35 +0200 Subject: [PATCH] search: Reduce skins.vector.search module size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change I765d3bbf89 pushes the module over the configured maximum allowed size (3 KiB, see bundlesize.config.json); shave off some bytes elsewhere to bring it below the limit again. IMHO all of these changes should be acceptable: * arrow functions are already used elsewhere in this module; * using the mw.config.get() fallback argument is normal (it slightly changes behavior, but I don’t think explicitly setting the search client or URL generator to a falsy value and expecting to get the default behavior should be considered supported); * not quoting [name="search"] matches [name=title] immediately above; * using forEach() with a function reference is still readable (initApp() is now called with extra arguments, but that doesn’t matter). Change-Id: I45dda26cb59279d91804b0c2bbf12174fa78ee12 --- resources/skins.vector.search/instrumentation.js | 4 ++-- resources/skins.vector.search/restSearchClient.js | 5 ++--- resources/skins.vector.search/skins.vector.search.js | 9 +++------ resources/skins.vector.search/urlGenerator.js | 5 ++--- tests/jest/restSearchClient.test.js | 4 ++-- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/resources/skins.vector.search/instrumentation.js b/resources/skins.vector.search/instrumentation.js index 9e854c10..f3908326 100644 --- a/resources/skins.vector.search/instrumentation.js +++ b/resources/skins.vector.search/instrumentation.js @@ -64,8 +64,8 @@ function onFetchEnd( event ) { // execute before the rendering steps happen (e.g. layout and paint). A // nested rAF will execute after these rendering steps have completed // and ensure the search results are visible to the user. - requestAnimationFrame( function () { - requestAnimationFrame( function () { + requestAnimationFrame( () => { + requestAnimationFrame( () => { if ( !performance.getEntriesByName( queryMark ).length ) { return; } diff --git a/resources/skins.vector.search/restSearchClient.js b/resources/skins.vector.search/restSearchClient.js index f0c8bd83..ce5067b4 100644 --- a/resources/skins.vector.search/restSearchClient.js +++ b/resources/skins.vector.search/restSearchClient.js @@ -81,8 +81,7 @@ function adaptApiResponse( config, query, restResponse, showDescription ) { * @return {SearchClient} */ function restSearchClient( config ) { - const customClient = config.get( 'wgVectorSearchClient' ); - return customClient || { + return config.get( 'wgVectorSearchClient', { /** * @type {fetchByTitle} */ @@ -103,7 +102,7 @@ function restSearchClient( config ) { fetch: searchResponsePromise }; } - }; + } ); } module.exports = restSearchClient; diff --git a/resources/skins.vector.search/skins.vector.search.js b/resources/skins.vector.search/skins.vector.search.js index b740b3c7..39c5a86d 100644 --- a/resources/skins.vector.search/skins.vector.search.js +++ b/resources/skins.vector.search/skins.vector.search.js @@ -14,7 +14,7 @@ function initApp( searchBox ) { titleInput = /** @type {HTMLInputElement|null} */ ( searchBox.querySelector( 'input[name=title]' ) ), - search = /** @type {HTMLInputElement|null} */ ( searchBox.querySelector( 'input[name="search"]' ) ), + search = /** @type {HTMLInputElement|null} */ ( searchBox.querySelector( 'input[name=search]' ) ), searchPageTitle = titleInput && titleInput.value; if ( !searchForm || !search || !titleInput ) { @@ -43,10 +43,7 @@ function initApp( searchBox ) { * @return {void} */ function main( document ) { - const searchBoxes = document.querySelectorAll( '.vector-search-box' ); - - searchBoxes.forEach( ( searchBox ) => { - initApp( searchBox ); - } ); + document.querySelectorAll( '.vector-search-box' ) + .forEach( initApp ); } main( document ); diff --git a/resources/skins.vector.search/urlGenerator.js b/resources/skins.vector.search/urlGenerator.js index 8d1c5508..dbfa7da0 100644 --- a/resources/skins.vector.search/urlGenerator.js +++ b/resources/skins.vector.search/urlGenerator.js @@ -28,8 +28,7 @@ function urlGenerator( config ) { // TODO: This is a placeholder for enabling customization of the URL generator. // wgVectorSearchUrlGenerator has not been defined as a config variable yet. - const customGenerator = config.get( 'wgVectorSearchUrlGenerator' ); - return customGenerator || { + return config.get( 'wgVectorSearchUrlGenerator', { /** * @type {generateUrl} */ @@ -52,7 +51,7 @@ function urlGenerator( config ) { return articlePath + '?' + $.param( $.extend( {}, params, { search: suggestion } ) ); } - }; + } ); } /** @module urlGenerator */ diff --git a/tests/jest/restSearchClient.test.js b/tests/jest/restSearchClient.test.js index 2e740a6d..1e5dba19 100644 --- a/tests/jest/restSearchClient.test.js +++ b/tests/jest/restSearchClient.test.js @@ -4,14 +4,14 @@ const jestFetchMock = require( 'jest-fetch-mock' ); const mockedRequests = !process.env.TEST_LIVE_REQUESTS; const configMock = { - get: jest.fn().mockImplementation( key => { + get: jest.fn().mockImplementation( ( key, fallback = null ) => { if ( key === 'wgScriptPath' ) { return '/w'; } if ( key === 'wgScript' ) { return '/w/index.php'; } - return null; + return fallback; } ), set: jest.fn() };