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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ exports[`angular configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -748,6 +752,10 @@ exports[`angular configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ exports[`base configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -556,6 +560,10 @@ exports[`base configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ exports[`javascript configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -565,6 +569,10 @@ exports[`javascript configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -589,6 +593,10 @@ exports[`react configuration > android > base config 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -921,6 +929,10 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -1247,6 +1259,10 @@ exports[`react configuration > ios > base config 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ exports[`svelte configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -604,6 +608,10 @@ exports[`svelte configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ exports[`typescript configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -565,6 +569,10 @@ exports[`typescript configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ exports[`vue configuration for android 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(ios)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down Expand Up @@ -630,6 +634,10 @@ exports[`vue configuration for ios 1`] = `
new ContextExclusionPlugin(
/(.*)App_Resources(.*)/
),
/* config.plugin('ContextExclusionPlugin|Other_Platforms') */
new ContextExclusionPlugin(
/\\\\.(android)\\\\.(\\\\w+)$/
),
/* config.plugin('DefinePlugin') */
new DefinePlugin(
{
Expand Down
15 changes: 14 additions & 1 deletion packages/webpack5/src/configuration/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 8 additions & 1 deletion packages/webpack5/src/helpers/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -61,7 +68,7 @@ export function getPlatformName(): Platform {
throw error(`
Invalid platform: ${env.platform}

Valid platforms: ${Object.keys(platforms).join(', ')}
Valid platforms: ${getAvailablePlatforms().join(', ')}
`);
}

Expand Down
0