fix(webpack): exclude other platforms from require.context (#9686)

fixes #9682
This commit is contained in:
Igor Randjelovic
2022-02-18 13:37:32 +01:00
committed by GitHub
parent 1601caff4e
commit cb7bd2a401
9 changed files with 86 additions and 2 deletions

View File

@ -22,8 +22,9 @@ import { env as _env, IWebpackEnv } from '../index';
import { getValue } from '../helpers/config';
import { getIPS } from '../helpers/host';
import {
getPlatformName,
getAvailablePlatforms,
getAbsoluteDistPath,
getPlatformName,
getEntryDirPath,
getEntryPath,
} from '../helpers/platform';
@ -365,6 +366,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.plugin('ContextExclusionPlugin|App_Resources')
.use(ContextExclusionPlugin, [new RegExp(`(.*)App_Resources(.*)`)]);
// Makes sure that require.context will never include code from
// another platform (ie .android.ts when building for ios)
const otherPlatformsRE = getAvailablePlatforms()
.filter((platform) => platform !== getPlatformName())
.join('|');
config
.plugin('ContextExclusionPlugin|Other_Platforms')
.use(ContextExclusionPlugin, [
new RegExp(`\\.(${otherPlatformsRE})\\.(\\w+)$`),
]);
// Filter common undesirable warnings
config.set(
'ignoreWarnings',

View File

@ -40,6 +40,13 @@ export function getPlatform(): INativeScriptPlatform {
return platforms[getPlatformName()];
}
/**
* Utility to get all registered/available platforms
*/
export function getAvailablePlatforms(): string[] {
return Object.keys(platforms);
}
/**
* Utility to get the currently targeted platform name
*/
@ -61,7 +68,7 @@ export function getPlatformName(): Platform {
throw error(`
Invalid platform: ${env.platform}
Valid platforms: ${Object.keys(platforms).join(', ')}
Valid platforms: ${getAvailablePlatforms().join(', ')}
`);
}