|
| 1 | +import { resolve } from 'node:path'; |
| 2 | +import { ExpoModulesPlugin } from '@callstack/repack-plugin-expo-modules'; |
| 3 | +import type { Compiler as RspackCompiler } from '@rspack/core'; |
| 4 | +import type { Compiler as WebpackCompiler } from 'webpack'; |
| 5 | + |
| 6 | +interface ExpoRouterOptions { |
| 7 | + /** |
| 8 | + * Base URL for the Expo Router. |
| 9 | + * |
| 10 | + * By default, it's set to an empty string. |
| 11 | + */ |
| 12 | + baseUrl?: string; |
| 13 | + |
| 14 | + /** |
| 15 | + * Routes root directory. |
| 16 | + * |
| 17 | + * By default, it's set to <projectRoot>/app (which is the default for Expo Router). |
| 18 | + * It should match the `root` option in your `expo-router` `app.json` plugin configuration. |
| 19 | + * |
| 20 | + * @see https://docs.expo.dev/router/reference/src-directory/#custom-directory |
| 21 | + */ |
| 22 | + root?: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface ExpoPluginOptions { |
| 26 | + /** |
| 27 | + * Project root directory. |
| 28 | + * |
| 29 | + * By default, it's set to the current working directory. |
| 30 | + */ |
| 31 | + root?: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * Wheter to enable Expo Router support. |
| 35 | + * |
| 36 | + * By default, it's disabled. |
| 37 | + * |
| 38 | + * If set to `true`, the default options will be used. |
| 39 | + * If you want to customize the options, pass an object with the desired options. |
| 40 | + * |
| 41 | + * @see ExpoRouterOptions |
| 42 | + */ |
| 43 | + router?: boolean | ExpoRouterOptions; |
| 44 | + |
| 45 | + /** |
| 46 | + * Target application platform (e.g. `ios`, `android`). |
| 47 | + * |
| 48 | + * By default, the platform is inferred from `compiler.options.name` which is set by Re.Pack. |
| 49 | + */ |
| 50 | + platform?: string; |
| 51 | +} |
| 52 | + |
| 53 | +export class ExpoPlugin { |
| 54 | + constructor(private options: ExpoPluginOptions = {}) {} |
| 55 | + |
| 56 | + resolveRouterOptions(projectRoot: string) { |
| 57 | + if (!this.options.router) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return typeof this.options.router === 'object' |
| 62 | + ? this.options.router |
| 63 | + : { baseUrl: '', root: resolve(projectRoot, 'app') }; |
| 64 | + } |
| 65 | + |
| 66 | + apply(compiler: RspackCompiler): void; |
| 67 | + apply(compiler: WebpackCompiler): void; |
| 68 | + |
| 69 | + apply(__compiler: unknown) { |
| 70 | + const compiler = __compiler as RspackCompiler; |
| 71 | + |
| 72 | + const root = this.options.root ?? process.cwd(); |
| 73 | + const router = this.resolveRouterOptions(root); |
| 74 | + |
| 75 | + const platform = this.options.platform ?? (compiler.options.name as string); |
| 76 | + |
| 77 | + // Apply Expo Modules support |
| 78 | + new ExpoModulesPlugin({ platform }).apply(compiler); |
| 79 | + |
| 80 | + new compiler.webpack.DefinePlugin({ |
| 81 | + 'process.env.EXPO_PROJECT_ROOT': JSON.stringify(root), |
| 82 | + // If Expo Router is enabled, pass additional environment variables |
| 83 | + ...(router |
| 84 | + ? { |
| 85 | + 'process.env.EXPO_BASE_URL': JSON.stringify(router.baseUrl), |
| 86 | + 'process.env.EXPO_ROUTER_ABS_APP_ROOT': JSON.stringify(router.root), |
| 87 | + 'process.env.EXPO_ROUTER_APP_ROOT': JSON.stringify(router.root), |
| 88 | + 'process.env.EXPO_ROUTER_IMPORT_MODE': JSON.stringify('sync'), |
| 89 | + } |
| 90 | + : {}), |
| 91 | + }).apply(compiler); |
| 92 | + |
| 93 | + // Add proxy configuration in development |
| 94 | + if ( |
| 95 | + compiler.options.mode === 'development' && |
| 96 | + !!compiler.options.devServer |
| 97 | + ) { |
| 98 | + compiler.options.devServer.proxy ??= []; |
| 99 | + |
| 100 | + // Redirect `/.expo/.virtual-metro-entry` to `/index` to match metro behavior in Expo managed projects |
| 101 | + compiler.options.devServer.proxy.push({ |
| 102 | + context: ['/.expo/.virtual-metro-entry'], |
| 103 | + pathRewrite: { '^/.expo/.virtual-metro-entry': '/index' }, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments