|
| 1 | +const path = require('path') |
| 2 | +const { spawnSync } = require('child_process') |
| 3 | + |
| 4 | +interface MetroBundleOptions { |
| 5 | + platform: string |
| 6 | + entryFile: string |
| 7 | + outputDir?: string |
| 8 | + development?: boolean |
| 9 | + resetCache?: boolean |
| 10 | + bundleName?: string |
| 11 | + bundleOutput?: string |
| 12 | + assetsDest?: string |
| 13 | + sourcemapOutputDir?: string |
| 14 | + sourcemapOutput?: string |
| 15 | + bundleCommand?: string |
| 16 | + extraBundlerOptions?: string[] |
| 17 | +} |
| 18 | + |
| 19 | +export const metroBundle = ({ |
| 20 | + platform, |
| 21 | + entryFile, |
| 22 | + outputDir = '', |
| 23 | + development = false, |
| 24 | + resetCache = true, |
| 25 | + bundleCommand = 'bundle', |
| 26 | + bundleName, |
| 27 | + bundleOutput = path.join(outputDir, bundleName), |
| 28 | + assetsDest = outputDir, |
| 29 | + sourcemapOutputDir = '', |
| 30 | + sourcemapOutput = sourcemapOutputDir ? path.join(sourcemapOutputDir, `${bundleName}.map`) : null, |
| 31 | + extraBundlerOptions = [], |
| 32 | +}: MetroBundleOptions) => { |
| 33 | + const args = [ |
| 34 | + bundleCommand, |
| 35 | + `--platform=${platform}`, |
| 36 | + `--entry-file=${entryFile}`, |
| 37 | + `--dev=${development}`, |
| 38 | + `--bundle-output=${bundleOutput}`, |
| 39 | + sourcemapOutput && `--sourcemap-output=${sourcemapOutput}`, |
| 40 | + assetsDest && `--assets-dest=${assetsDest}`, |
| 41 | + resetCache && '--reset-cache', |
| 42 | + ...extraBundlerOptions, |
| 43 | + ].filter(Boolean) |
| 44 | + |
| 45 | + spawnSync(getCliPath(), args, { stdio: 'inherit' }) |
| 46 | +} |
| 47 | + |
| 48 | +function getCliPath() { |
| 49 | + return path.join('node_modules', '.bin', 'react-native') |
| 50 | +} |
0 commit comments