|
| 1 | +import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '../../types/config.js'; |
| 2 | +import { toDeferResolve, toProductionEntry } from '../../util/input.js'; |
| 3 | +import { join } from '../../util/path.js'; |
| 4 | +import { hasDependency } from '../../util/plugin.js'; |
| 5 | +import type { PluginConfig } from './types.js'; |
| 6 | + |
| 7 | +// https://metrobundler.dev/docs/configuration |
| 8 | + |
| 9 | +const title = 'Metro'; |
| 10 | + |
| 11 | +const enablers = ['metro', 'react-native']; |
| 12 | + |
| 13 | +const isEnabled: IsPluginEnabled = options => hasDependency(options.dependencies, enablers); |
| 14 | + |
| 15 | +const packageJsonPath = 'metro'; |
| 16 | + |
| 17 | +const config: string[] = ['metro.config.{js,cjs,json}', 'package.json']; |
| 18 | + |
| 19 | +const resolveEntryPaths: ResolveEntryPaths<PluginConfig> = async config => { |
| 20 | + if (!config.projectRoot) return []; |
| 21 | + |
| 22 | + const entryFilePattern = 'index.{js,jsx,ts,tsx}'; |
| 23 | + const entryFilePath = join(config.projectRoot, entryFilePattern); |
| 24 | + return [toProductionEntry(entryFilePath)]; |
| 25 | +}; |
| 26 | + |
| 27 | +const resolveConfig: ResolveConfig<PluginConfig> = async config => { |
| 28 | + const { transformerPath, transformer } = config; |
| 29 | + const inputs: string[] = []; |
| 30 | + |
| 31 | + if (transformerPath) inputs.push(transformerPath); |
| 32 | + if (transformer?.assetPlugins) inputs.push(...transformer.assetPlugins); |
| 33 | + if (transformer?.minifierPath) inputs.push(transformer.minifierPath); |
| 34 | + if (transformer?.babelTransformerPath) inputs.push(transformer.babelTransformerPath); |
| 35 | + |
| 36 | + return [...inputs].map(toDeferResolve); |
| 37 | +}; |
| 38 | + |
| 39 | +export default { |
| 40 | + title, |
| 41 | + enablers, |
| 42 | + isEnabled, |
| 43 | + packageJsonPath, |
| 44 | + config, |
| 45 | + resolveEntryPaths, |
| 46 | + resolveConfig, |
| 47 | +} satisfies Plugin; |
0 commit comments