Skip to content

Commit

Permalink
Optimise/simplify getPlatformExtension
Browse files Browse the repository at this point in the history
Summary:
`getPlatformExtension` extracts the `"ios"` from `"path/MyComponent.ios.js"`, if `"ios"` is a known platform. In the context of this function, this is only relevant to Haste.

Currently, the effective set of platforms is the union of those passed from Metro (from `config.resolver.platforms`) and a hardcoded set.

Entries in the hardcoded set but not in `config.resolver.platforms` are mostly superfluous because Metro will never resolve to them (although they would *prevent* `MyComponent.extraplatform` from being added to the Haste map with a generic platform).

The notable exception is `native`, which in Metro is a special platform alias used in the absence of a specific platform match.

Here, we remove the hardcoded set, and pass `native` from Metro when creating MetroFileMap.

Changelog: Internal

Reviewed By: motiz88

Differential Revision: D47050529

fbshipit-source-id: e9b5495e87133033a1f5270e36809c99eb75bbf9
  • Loading branch information
robhogan authored and facebook-github-bot committed Jul 5, 2023
1 parent 668ee8d commit a6e3dd0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export default class FileMap extends EventEmitter {
this._startupPerfLogger?.point('constructHasteMap_start');
const hasteOptions = {
console: this._console,
platforms: this._options.platforms,
platforms: new Set(this._options.platforms),
rootDir,
throwOnModuleCollision: this._options.throwOnModuleCollision,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/metro-file-map/src/lib/MutableHasteMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const EMPTY_MAP: $ReadOnlyMap<string, DuplicatesSet> = new Map();

type HasteMapOptions = $ReadOnly<{
console?: ?Console,
platforms: $ReadOnlyArray<string>,
platforms: $ReadOnlySet<string>,
rootDir: Path,
throwOnModuleCollision: boolean,
}>;
Expand All @@ -46,7 +46,7 @@ export default class MutableHasteMap implements HasteMap {

+#console: ?Console;
#throwOnModuleCollision: boolean;
+#platforms: $ReadOnlyArray<string>;
+#platforms: $ReadOnlySet<string>;

constructor(options: HasteMapOptions) {
this.#console = options.console ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

import getPlatformExtension from '../getPlatformExtension';

const PLATFORMS = new Set(['ios', 'android']);

describe('getPlatformExtension', () => {
it('should get platform ext', () => {
expect(getPlatformExtension('a.ios.js')).toBe('ios');
expect(getPlatformExtension('a.android.js')).toBe('android');
expect(getPlatformExtension('/b/c/a.ios.js')).toBe('ios');
expect(getPlatformExtension('/b/c.android/a.ios.js')).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.ios.png')).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.lol.png')).toBe(null);
expect(getPlatformExtension('/b/c/a.lol.png')).toBe(null);
test('should get platform ext', () => {
expect(getPlatformExtension('a.ios.js', PLATFORMS)).toBe('ios');
expect(getPlatformExtension('a.android.js', PLATFORMS)).toBe('android');
expect(getPlatformExtension('c.android/a.ios.js', PLATFORMS)).toBe('ios');
expect(getPlatformExtension('/b/c/a.ios.js', PLATFORMS)).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.ios.png', PLATFORMS)).toBe('ios');
expect(getPlatformExtension('/b/c/a@1.5x.lol.png', PLATFORMS)).toBe(null);
expect(getPlatformExtension('/b/c/a.lol.png', PLATFORMS)).toBe(null);
});
});
11 changes: 2 additions & 9 deletions packages/metro-file-map/src/lib/getPlatformExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@
* @flow strict
*/

const SUPPORTED_PLATFORM_EXTS = new Set(['android', 'ios', 'native', 'web']);

// Extract platform extension: index.ios.js -> ios
export default function getPlatformExtension(
file: string,
platforms?: $ReadOnlyArray<string>,
platforms: $ReadOnlySet<string>,
): ?string {
const last = file.lastIndexOf('.');
const secondToLast = file.lastIndexOf('.', last - 1);
if (secondToLast === -1) {
return null;
}
const platform = file.substring(secondToLast + 1, last);
// If an overriding platform array is passed, check that first

if (platforms && platforms.indexOf(platform) !== -1) {
return platform;
}
return SUPPORTED_PLATFORM_EXTS.has(platform) ? platform : null;
return platforms.has(platform) ? platform : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function createFileMap(
ignorePattern: getIgnorePattern(config),
maxWorkers: config.maxWorkers,
mocksPattern: '',
platforms: config.resolver.platforms,
platforms: [...config.resolver.platforms, MetroFileMap.H.NATIVE_PLATFORM],
retainAllFiles: true,
resetCache: config.resetCache,
rootDir: config.projectRoot,
Expand Down

0 comments on commit a6e3dd0

Please sign in to comment.