fix: prevent asset conflicts between React and Grid.js versions

Add coexistence checks to all enqueue methods to prevent loading
both React and Grid.js assets simultaneously.

Changes:
- ReactAdmin.php: Only enqueue React assets when ?react=1
- Init.php: Skip Grid.js when React active on admin pages
- Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0
- Customer.php, Product.php, License.php: Add coexistence checks

Now the toggle between Classic and React versions works correctly.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-04-18 17:02:14 +07:00
parent bd9cdac02e
commit e8fbfb14c1
74973 changed files with 6658406 additions and 71 deletions

View File

@@ -0,0 +1,85 @@
/**
* Parts of this source were derived and modified from the package
* jest-environment-puppeteer, released under the MIT license.
*
* https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer
*
* Copyright 2018 Smooth Code
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const { promisify } = require( 'util' );
const cwd = require( 'cwd' );
const merge = require( 'merge-deep' );
const exists = promisify( fs.exists );
const DEFAULT_CONFIG = {
launch: {},
browser: 'chromium',
browserContext: 'default',
exitOnPageError: true,
};
const DEFAULT_CONFIG_CI = merge( DEFAULT_CONFIG, {
launch: {
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
],
},
} );
async function readConfig() {
const defaultConfig =
process.env.CI === 'true' ? DEFAULT_CONFIG_CI : DEFAULT_CONFIG;
const hasCustomConfigPath = !! process.env.JEST_PUPPETEER_CONFIG;
const configPath =
process.env.JEST_PUPPETEER_CONFIG || 'jest-puppeteer.config.js';
const absConfigPath = path.resolve( cwd(), configPath );
const configExists = await exists( absConfigPath );
if ( hasCustomConfigPath && ! configExists ) {
throw new Error(
`Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${ configPath }`
);
}
if ( ! hasCustomConfigPath && ! configExists ) {
return defaultConfig;
}
const localConfig = await require( absConfigPath );
return merge( {}, defaultConfig, localConfig );
}
function getPuppeteer( { browser } ) {
switch ( browser.toLowerCase() ) {
case 'chromium':
return require( 'puppeteer-core' );
case 'firefox':
return require( 'puppeteer-firefox' );
default:
throw new Error(
`Error: "browser" config option is given an unsupported value: ${ browser }`
);
}
}
module.exports = {
readConfig,
getPuppeteer,
};

View File

@@ -0,0 +1,101 @@
/**
* Parts of this source were derived and modified from the package
* jest-environment-puppeteer, released under the MIT license.
*
* https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer
*
* Copyright 2018 Smooth Code
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* External dependencies
*/
const {
setup: setupServer,
teardown: teardownServer,
ERROR_TIMEOUT,
ERROR_NO_COMMAND,
} = require( 'jest-dev-server' );
const chalk = require( 'chalk' );
/**
* Internal dependencies
*/
const { readConfig, getPuppeteer } = require( './config' );
let browser;
let didAlreadyRunInWatchMode = false;
let servers = [];
async function setup( jestConfig = {} ) {
const config = await readConfig();
const puppeteer = getPuppeteer( config );
if ( config.connect ) {
browser = await puppeteer.connect( config.connect );
} else {
browser = await puppeteer.launch( config.launch );
}
process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint();
// If we are in watch mode, - only setupServer() once.
if ( jestConfig.watch || jestConfig.watchAll ) {
if ( didAlreadyRunInWatchMode ) {
return;
}
didAlreadyRunInWatchMode = true;
}
if ( config.server ) {
try {
servers = await setupServer( config.server );
} catch ( error ) {
const { error: printError } = console;
if ( error.code === ERROR_TIMEOUT ) {
printError( '' );
printError( chalk.red( error.message ) );
printError(
chalk.blue(
`\n☝️ You can set "server.launchTimeout" in jest-puppeteer.config.js`
)
);
process.exit( 1 );
}
if ( error.code === ERROR_NO_COMMAND ) {
printError( '' );
printError( chalk.red( error.message ) );
printError(
chalk.blue(
`\n☝️ You must set "server.command" in jest-puppeteer.config.js`
)
);
process.exit( 1 );
}
throw error;
}
}
}
async function teardown( jestConfig = {} ) {
const config = await readConfig();
if ( config.connect ) {
await browser.disconnect();
} else {
await browser.close();
}
if ( ! jestConfig.watch && ! jestConfig.watchAll ) {
await teardownServer( servers );
}
}
module.exports = {
setup,
teardown,
};

View File

@@ -0,0 +1,221 @@
/**
* Parts of this source were derived and modified from the package
* jest-environment-puppeteer, released under the MIT license.
*
* https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer
*
* Copyright 2018 Smooth Code
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* External dependencies
*/
const path = require( 'path' );
const { writeFile, mkdir } = require( 'fs' ).promises;
const filenamify = require( 'filenamify' );
const NodeEnvironment = require( 'jest-environment-node' ).default;
const chalk = require( 'chalk' );
/**
* Internal dependencies
*/
const { readConfig, getPuppeteer } = require( './config' );
const handleError = ( error ) => {
// To match the same behavior in jest-jasmine2:
// https://github.com/facebook/jest/blob/1be8d737abd0e2f30e3314184a0efc372ad6d88f/packages/jest-jasmine2/src/jasmine/Env.ts#L250-L251
// Emitting an uncaughtException event to the process will throw an
// empty error which is very hard to debug in puppeteer context.
// eslint-disable-next-line no-console
console.error( error );
};
const KEYS = {
CONTROL_C: '\u0003',
CONTROL_D: '\u0004',
ENTER: '\r',
};
const { WP_ARTIFACTS_PATH } = process.env;
class PuppeteerEnvironment extends NodeEnvironment {
// Jest is not available here, so we have to reverse engineer
// the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
setTimeout( timeout ) {
this.global[ Symbol.for( 'TEST_TIMEOUT_SYMBOL' ) ] = timeout;
}
async setup() {
const config = await readConfig();
const puppeteer = getPuppeteer( config );
this.global.puppeteerConfig = config;
const wsEndpoint = process.env.PUPPETEER_WS_ENDPOINT;
if ( ! wsEndpoint ) {
throw new Error( 'wsEndpoint not found' );
}
this.global.jestPuppeteer = {
debug: async () => {
// Set timeout to 4 days.
this.setTimeout( 345600000 );
// Run a debugger (in case Puppeteer has been launched with `{ devtools: true }`)
await this.global.page.evaluate( () => {
// eslint-disable-next-line no-debugger
debugger;
} );
// eslint-disable-next-line no-console
console.log(
chalk.blue(
'\n\n🕵 Code is paused, press enter to resume'
)
);
// Run an infinite promise.
return new Promise( ( resolve ) => {
const { stdin } = process;
const onKeyPress = ( key ) => {
if (
key === KEYS.CONTROL_C ||
key === KEYS.CONTROL_D ||
key === KEYS.ENTER
) {
stdin.removeListener( 'data', onKeyPress );
if ( ! listening ) {
if ( stdin.isTTY ) {
stdin.setRawMode( false );
}
stdin.pause();
}
resolve();
}
};
const listening = stdin.listenerCount( 'data' ) > 0;
if ( ! listening ) {
if ( stdin.isTTY ) {
stdin.setRawMode( true );
}
stdin.resume();
stdin.setEncoding( 'utf8' );
}
stdin.on( 'data', onKeyPress );
} );
},
resetPage: async () => {
if ( this.global.page ) {
this.global.page.removeListener( 'pageerror', handleError );
await this.global.page.close();
}
this.global.page = await this.global.context.newPage();
if ( config && config.exitOnPageError ) {
this.global.page.addListener( 'pageerror', handleError );
}
},
resetBrowser: async () => {
if ( this.global.page ) {
this.global.page.removeListener( 'pageerror', handleError );
}
if (
config.browserContext === 'incognito' &&
this.global.context
) {
await this.global.context.close();
} else if ( this.global.page ) {
await this.global.page.close();
}
this.global.page = null;
if ( this.global.browser ) {
await this.global.browser.disconnect();
}
this.global.browser = await puppeteer.connect( {
...config.connect,
...config.launch,
browserURL: undefined,
browserWSEndpoint: wsEndpoint,
} );
if ( config.browserContext === 'incognito' ) {
// Using this, pages will be created in a pristine context.
this.global.context =
await this.global.browser.createIncognitoBrowserContext();
} else if (
config.browserContext === 'default' ||
! config.browserContext
) {
/**
* Since this is a new browser, browserContexts() will return only one instance
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
*/
this.global.context =
await this.global.browser.browserContexts()[ 0 ];
} else {
throw new Error(
`browserContext should be either 'incognito' or 'default'. Received '${ config.browserContext }'`
);
}
await this.global.jestPuppeteer.resetPage();
},
};
await this.global.jestPuppeteer.resetBrowser();
try {
await mkdir( WP_ARTIFACTS_PATH, { recursive: true } );
} catch ( err ) {
if ( err.code !== 'EEXIST' ) {
throw err;
}
}
}
async teardown() {
const { page, context, browser, puppeteerConfig } = this.global;
if ( page ) {
page.removeListener( 'pageerror', handleError );
}
if ( puppeteerConfig.browserContext === 'incognito' ) {
if ( context ) {
await context.close();
}
} else if ( page ) {
await page.close();
}
if ( browser ) {
await browser.disconnect();
}
}
async storeArtifacts( testName ) {
const datetime = new Date().toISOString().split( '.' )[ 0 ];
const fileName = filenamify( `${ testName } ${ datetime }`, {
replacement: '-',
} );
await writeFile(
path.join( WP_ARTIFACTS_PATH, `${ fileName }-snapshot.html` ),
await this.global.page.content()
);
await this.global.page.screenshot( {
path: path.join( WP_ARTIFACTS_PATH, `${ fileName }.jpg` ),
} );
}
async handleTestEvent( event, state ) {
if ( event.name === 'test_fn_failure' ) {
const testName = state.currentlyRunningTest.name;
await this.storeArtifacts( testName );
}
}
}
module.exports = PuppeteerEnvironment;

View File

@@ -0,0 +1 @@
module.exports = require( './global' ).setup;

View File

@@ -0,0 +1 @@
module.exports = require( './global' ).teardown;