|
| 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 ExpoPluginOptions { |
| 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 | + routesRoot?: string; |
| 23 | + |
| 24 | + /** |
| 25 | + * Project root directory. |
| 26 | + * |
| 27 | + * By default, it's set to the current working directory. |
| 28 | + */ |
| 29 | + projectRoot?: string; |
| 30 | + |
| 31 | + /** |
| 32 | + * Target application platform (e.g. `ios`, `android`). |
| 33 | + * |
| 34 | + * By default, the platform is inferred from `compiler.options.name` which is set by Re.Pack. |
| 35 | + */ |
| 36 | + platform?: string; |
| 37 | +} |
| 38 | + |
| 39 | +export class ExpoPlugin { |
| 40 | + constructor(private options: ExpoPluginOptions = {}) {} |
| 41 | + |
| 42 | + apply(compiler: RspackCompiler): void; |
| 43 | + apply(compiler: WebpackCompiler): void; |
| 44 | + |
| 45 | + apply(__compiler: unknown) { |
| 46 | + const compiler = __compiler as RspackCompiler; |
| 47 | + |
| 48 | + const baseUrl = this.options.baseUrl ?? ''; |
| 49 | + const projectRoot = this.options.projectRoot ?? process.cwd(); |
| 50 | + const routesRoot = this.options.routesRoot ?? resolve(projectRoot, 'app'); |
| 51 | + |
| 52 | + const platform = this.options.platform ?? (compiler.options.name as string); |
| 53 | + |
| 54 | + // apply expo modules plugin |
| 55 | + new ExpoModulesPlugin({ platform }).apply(compiler); |
| 56 | + |
| 57 | + // expo router expect this to be defined in runtime |
| 58 | + new compiler.webpack.DefinePlugin({ |
| 59 | + 'process.env.EXPO_BASE_URL': JSON.stringify(baseUrl), |
| 60 | + 'process.env.EXPO_PROJECT_ROOT': JSON.stringify(projectRoot), |
| 61 | + 'process.env.EXPO_ROUTER_ABS_APP_ROOT': JSON.stringify(routesRoot), |
| 62 | + 'process.env.EXPO_ROUTER_APP_ROOT': JSON.stringify(routesRoot), |
| 63 | + 'process.env.EXPO_ROUTER_IMPORT_MODE': JSON.stringify('sync'), |
| 64 | + }).apply(compiler); |
| 65 | + |
| 66 | + // add proxy configuration in development |
| 67 | + if ( |
| 68 | + compiler.options.mode === 'development' && |
| 69 | + !!compiler.options.devServer |
| 70 | + ) { |
| 71 | + compiler.options.devServer.proxy ??= []; |
| 72 | + |
| 73 | + // redirect `/.expo/.virtual-metro-entry` to `/index` to match metro behavior in Expo managed projects |
| 74 | + compiler.options.devServer.proxy.push({ |
| 75 | + context: ['/.expo/.virtual-metro-entry'], |
| 76 | + pathRewrite: { '^/.expo/.virtual-metro-entry': '/index' }, |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments