1
1
import type { IsPluginEnabled , Plugin , ResolveConfig , ResolveEntryPaths } from '../../types/config.js' ;
2
+ import { compact } from '../../util/array.js' ;
2
3
import { toDeferResolve , toProductionEntry } from '../../util/input.js' ;
3
4
import { join } from '../../util/path.js' ;
4
5
import { hasDependency } from '../../util/plugin.js' ;
5
- import type { PluginConfig } from './types.js' ;
6
+ import type { MetroConfig } from './types.js' ;
6
7
7
8
// https://metrobundler.dev/docs/configuration
8
9
9
10
const title = 'Metro' ;
10
11
12
+ const note = `False positives for platform-specific unused files?
13
+ Override the entry patterns as shown below to match platforms and extensions.` ;
14
+
11
15
const enablers = [ 'metro' , 'react-native' ] ;
12
16
13
17
const isEnabled : IsPluginEnabled = options => hasDependency ( options . dependencies , enablers ) ;
@@ -16,15 +20,27 @@ const packageJsonPath = 'metro';
16
20
17
21
const config : string [ ] = [ 'metro.config.{js,cjs,json}' , 'package.json' ] ;
18
22
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 ) ] ;
21
35
22
36
const entryFilePattern = 'index.{js,jsx,ts,tsx}' ;
23
37
const entryFilePath = join ( config . projectRoot , entryFilePattern ) ;
24
- return [ toProductionEntry ( entryFilePath ) ] ;
38
+ const entryFilePaths = join ( config . projectRoot , pattern ) ;
39
+
40
+ return [ toProductionEntry ( entryFilePath ) , toProductionEntry ( entryFilePaths ) ] ;
25
41
} ;
26
42
27
- const resolveConfig : ResolveConfig < PluginConfig > = async config => {
43
+ const resolveConfig : ResolveConfig < MetroConfig > = async config => {
28
44
const { transformerPath, transformer } = config ;
29
45
const inputs : string [ ] = [ ] ;
30
46
@@ -38,10 +54,12 @@ const resolveConfig: ResolveConfig<PluginConfig> = async config => {
38
54
39
55
export default {
40
56
title,
57
+ note,
41
58
enablers,
42
59
isEnabled,
43
60
packageJsonPath,
44
61
config,
62
+ production,
45
63
resolveEntryPaths,
46
64
resolveConfig,
47
65
} satisfies Plugin ;
0 commit comments