8000 fix(webpack): exclude other platforms from require.context (#9686) · NativeScript/NativeScript@cb7bd2a · GitHub
[go: up one dir, main page]

Skip to content

Commit cb7bd2a

Browse files
authored
fix(webpack): exclude other platforms from require.context (#9686)
fixes #9682
1 parent 1601caf commit cb7bd2a

File tree

9 files changed

+86
-2
lines changed

9 files changed

+86
-2
lines changed

packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ exports[`angular configuration for android 1`] = `
331331
new ContextExclusionPlugin(
332332
/(.*)App_Resources(.*)/
333333
),
334+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
335+
new ContextExclusionPlugin(
336+
/\\\\.(ios)\\\\.(\\\\w+)$/
337+
),
334338
/* config.plugin('DefinePlugin') */
335339
new DefinePlugin(
336340
{
@@ -748,6 +752,10 @@ exports[`angular configuration for ios 1`] = `
748752
new ContextExclusionPlugin(
749753
/(.*)App_Resources(.*)/
750754
),
755+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
756+
new ContextExclusionPlugin(
757+
/\\\\.(android)\\\\.(\\\\w+)$/
758+
),
751759
/* config.plugin('DefinePlugin') */
752760
new DefinePlugin(
753761
{

packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ exports[`base configuration for android 1`] = `
242242
new ContextExclusionPlugin(
243243
/(.*)App_Resources(.*)/
244244
),
245+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
246+
new ContextExclusionPlugin(
247+
/\\\\.(ios)\\\\.(\\\\w+)$/
248+
),
245249
/* config.plugin('DefinePlugin') */
246250
new DefinePlugin(
247251
{
@@ -556,6 +560,10 @@ exports[`base configuration for ios 1`] = `
556560
new ContextExclusionPlugin(
557561
/(.*)App_Resources(.*)/
558562
),
563+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
564+
new ContextExclusionPlugin(
565+
/\\\\.(android)\\\\.(\\\\w+)$/
566+
),
559567
/* config.plugin('DefinePlugin') */
560568
new DefinePlugin(
561569
{

packages/webpack5/__tests__/configuration/__snapshots__/javascript.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ exports[`javascript configuration for android 1`] = `
242242
new ContextExclusionPlugin(
243243
/(.*)App_Resources(.*)/
244244
),
245+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
246+
new ContextExclusionPlugin(
247+
/\\\\.(ios)\\\\.(\\\\w+)$/
248+
),
245249
/* config.plugin('DefinePlugin') */
246250
new DefinePlugin(
247251
{
@@ -565,6 +569,10 @@ exports[`javascript configuration for ios 1`] = `
565569
new ContextExclusionPlugin(
566570
/(.*)App_Resources(.*)/
567571
),
572+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
573+
new ContextExclusionPlugin(
574+
/\\\\.(android)\\\\.(\\\\w+)$/
575+
),
568576
/* config.plugin('DefinePlugin') */
569577
new DefinePlugin(
570578
{

packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR
264264
new ContextExclusionPlugin(
265265
/(.*)App_Resources(.*)/
266266
),
267+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
268+
new ContextExclusionPlugin(
269+
/\\\\.(ios)\\\\.(\\\\w+)$/
270+
),
267271
/* config.plugin('DefinePlugin') */
268272
new DefinePlugin(
269273
{
@@ -589,6 +593,10 @@ exports[`react configuration > android > base config 1`] = `
589593
new ContextExclusionPlugin(
590594
/(.*)App_Resources(.*)/
591595
),
596+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
597+
new ContextExclusionPlugin(
598+
/\\\\.(ios)\\\\.(\\\\w+)$/
599+
),
592600
/* config.plugin('DefinePlugin') */
593601
new DefinePlugin(
594602
{
@@ -921,6 +929,10 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena
921929
new ContextExclusionPlugin(
922930
/(.*)App_Resources(.*)/
923931
),
932+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
933+
new ContextExclusionPlugin(
934+
/\\\\.(android)\\\\.(\\\\w+)$/
935+
),
924936
/* config.plugin('DefinePlugin') */
925937
new DefinePlugin(
926938
{
@@ -1247,6 +1259,10 @@ exports[`react configuration > ios > base config 1`] = `
12471259
new ContextExclusionPlugin(
12481260
/(.*)App_Resources(.*)/
12491261
),
1262+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
1263+
new ContextExclusionPlugin(
1264+
/\\\\.(android)\\\\.(\\\\w+)$/
1265+
),
12501266
/* config.plugin('DefinePlugin') */
12511267
new DefinePlugin(
12521268
{

packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ exports[`svelte configuration for android 1`] = `
269269
new ContextExclusionPlugin(
270270
/(.*)App_Resources(.*)/
271271
),
272+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
273+
new ContextExclusionPlugin(
274+
/\\\\.(ios)\\\\.(\\\\w+)$/
275+
),
272276
/* config.plugin('DefinePlugin') */
273277
new DefinePlugin(
274278
{
@@ -604,6 +608,10 @@ exports[`svelte configuration for ios 1`] = `
604608
new ContextExclusionPlugin(
605609
/(.*)App_Resources(.*)/
606610
),
611+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
612+
new ContextExclusionPlugin(
613+
/\\\\.(android)\\\\.(\\\\w+)$/
614+
),
607615
/* config.plugin('DefinePlugin') */
608616
new DefinePlugin(
609617
{

packages/webpack5/__tests__/configuration/__snapshots__/typescript.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ exports[`typescript configuration for android 1`] = `
242242
new ContextExclusionPlugin(
243243
/(.*)App_Resources(.*)/
244244
),
245+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
246+
new ContextExclusionPlugin(
247+
/\\\\.(ios)\\\\.(\\\\w+)$/
248+
),
245249
/* config.plugin('DefinePlugin') */
246250
new DefinePlugin(
247251
{
@@ -565,6 +569,10 @@ exports[`typescript configuration for ios 1`] = `
565569
new ContextExclusionPlugin(
566570
/(.*)App_Resources(.*)/
567571
),
572+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
573+
new ContextExclusionPlugin(
574+
/\\\\.(android)\\\\.(\\\\w+)$/
575+
),
568576
/* config.plugin('DefinePlugin') */
569577
new DefinePlugin(
570578
{

packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ exports[`vue configuration for android 1`] = `
282282
new ContextExclusionPlugin(
283283
/(.*)App_Resources(.*)/
284284
),
285+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
286+
new ContextExclusionPlugin(
287+
/\\\\.(ios)\\\\.(\\\\w+)$/
288+
),
285289
/* config.plugin('DefinePlugin') */
286290
new DefinePlugin(
287291
{
@@ -630,6 +634,10 @@ exports[`vue configuration for ios 1`] = `
630634
new ContextExclusionPlugin(
631635
/(.*)App_Resources(.*)/
632636
),
637+
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
638+
new ContextExclusionPlugin(
639+
/\\\\.(android)\\\\.(\\\\w+)$/
640+
),
633641
/* config.plugin('DefinePlugin') */
634642
new DefinePlugin(
635643
{

packages/webpack5/src/configuration/base.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import { env as _env, IWebpackEnv } from '../index';
2222
import { getValue } from '../helpers/config';
2323
import { getIPS } from '../helpers/host';
2424
import {
25-
getPlatformName,
25+
getAvailablePlatforms,
2626
getAbsoluteDistPath,
27+
getPlatformName,
2728
getEntryDirPath,
2829
getEntryPath,
2930
} from '../helpers/platform';
@@ -365,6 +366,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
365366
.plugin('ContextExclusionPlugin|App_Resources')
366367
.use(ContextExclusionPlugin, [new RegExp(`(.*)App_Resources(.*)`)]);
367368

369+
// Makes sure that require.context will never include code from
370+
// another platform (ie .android.ts when building for ios)
371+
const otherPlatformsRE = getAvailablePlatforms()
372+
.filter((platform) => platform !== getPlatformName())
373+
.join('|');
374+
375+
config
376+
.plugin('ContextExclusionPlugin|Other_Platforms')
377+
.use(ContextExclusionPlugin, [
378+
new RegExp(`\\.(${otherPlatformsRE})\\.(\\w+)$`),
379+
]);
380+
368381
// Filter common undesirable warnings
369382
config.set(
370383
'ignoreWarnings',

packages/webpack5/src/helpers/platform.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export function getPlatform(): INativeScriptPlatform {
4040
return platforms[getPlatformName()];
4141
}
4242

43+
/**
44+
* Utility to get all registered/available platforms
45+
*/
46+
export function getAvailablePlatforms(): string[] {
47+
return Object.keys(platforms);
48+
}
49+
4350
/**
4451
* Utility to get the currently targeted platform name
4552
*/
@@ -61,7 +68,7 @@ export function getPlatformName(): Platform {
6168
throw error(`
6269
Invalid platform: ${env.platform}
6370
64-
Valid platforms: ${Object.keys(platforms).join(', ')}
71+
Valid platforms: ${getAvailablePlatforms().join(', ')}
6572
`);
6673
}
6774

0 commit comments

Comments
 (0)
0