Skip to content

Commit 2e8c8ac

Browse files
committedJan 6, 2025
Add platform-specific entries to metro plugin
1 parent f5a1ede commit 2e8c8ac

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed
 

‎packages/knip/src/plugins/metro/index.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '../../types/config.js';
2+
import { compact } from '../../util/array.js';
23
import { toDeferResolve, toProductionEntry } from '../../util/input.js';
34
import { join } from '../../util/path.js';
45
import { hasDependency } from '../../util/plugin.js';
5-
import type { PluginConfig } from './types.js';
6+
import type { MetroConfig } from './types.js';
67

78
// https://metrobundler.dev/docs/configuration
89

910
const title = 'Metro';
1011

12+
const note = `False positives for platform-specific unused files?
13+
Override the entry patterns as shown below to match platforms and extensions.`;
14+
1115
const enablers = ['metro', 'react-native'];
1216

1317
const isEnabled: IsPluginEnabled = options => hasDependency(options.dependencies, enablers);
@@ -16,15 +20,27 @@ const packageJsonPath = 'metro';
1620

1721
const config: string[] = ['metro.config.{js,cjs,json}', 'package.json'];
1822

19-
const resolveEntryPaths: ResolveEntryPaths<PluginConfig> = async config => {
20-
if (!config.projectRoot) return [];
23+
const DEFAULT_PLATFORMS = ['ios', 'android', 'windows', 'web'];
24+
const PLATFORMS = [...DEFAULT_PLATFORMS, 'native', 'default'];
25+
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'json', 'ts', 'tsx'];
26+
27+
const production = [`src/**/*.{${PLATFORMS.join(',')}}.{${DEFAULT_EXTENSIONS.join(',')}}`];
28+
29+
const resolveEntryPaths: ResolveEntryPaths<MetroConfig> = async config => {
30+
const platformEntryPatterns = compact(PLATFORMS.concat(config.resolver?.platforms ?? []));
31+
const sourceExts = config.resolver?.sourceExts ?? DEFAULT_EXTENSIONS;
32+
const pattern = `src/**/*.{${platformEntryPatterns.join(',')}}.{${sourceExts.join(',')}}`;
33+
34+
if (!config.projectRoot) return [toProductionEntry(pattern)];
2135

2236
const entryFilePattern = 'index.{js,jsx,ts,tsx}';
2337
const entryFilePath = join(config.projectRoot, entryFilePattern);
24-
return [toProductionEntry(entryFilePath)];
38+
const entryFilePaths = join(config.projectRoot, pattern);
39+
40+
return [toProductionEntry(entryFilePath), toProductionEntry(entryFilePaths)];
2541
};
2642

27-
const resolveConfig: ResolveConfig<PluginConfig> = async config => {
43+
const resolveConfig: ResolveConfig<MetroConfig> = async config => {
2844
const { transformerPath, transformer } = config;
2945
const inputs: string[] = [];
3046

@@ -38,10 +54,12 @@ const resolveConfig: ResolveConfig<PluginConfig> = async config => {
3854

3955
export default {
4056
title,
57+
note,
4158
enablers,
4259
isEnabled,
4360
packageJsonPath,
4461
config,
62+
production,
4563
resolveEntryPaths,
4664
resolveConfig,
4765
} satisfies Plugin;
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// https://github.com/facebook/metro/blob/main/packages/metro-config/types/configTypes.d.ts
22

3-
export type PluginConfig = {
3+
export type MetroConfig = {
44
projectRoot?: string;
55
transformerPath?: string;
66
transformer?: {
77
minifierPath?: string;
88
assetPlugins?: string[];
99
babelTransformerPath?: string;
1010
};
11+
resolver?: {
12+
platforms?: string[];
13+
sourceExts?: string[];
14+
};
1115
};

0 commit comments

Comments
 (0)
Please sign in to comment.