内联模块引用

Expo 内联模块的参考。


For the complete documentation index, see llms.txt. Use this file to discover all available pages.

警告 内联模块是实验性的。API 可能会有破坏性更改。

内联模块允许你在 Expo 项目目录中直接编写原生模块代码(Kotlin 和 Swift),而无需创建单独的 Expo 模块包。Expo 会自动发现这些文件并将它们包含在构建中。

🌐 Inline modules let you write native module code (Kotlin and Swift) directly in your Expo project directory, without creating a separate Expo module package. Expo discovers these files automatically and includes them in the build.

配置

🌐 Configuration

expo.experiments.inlineModules

定义后,在 Expo CLI 和 Expo 模块自动链接中启用内联模块功能。

app.json
{ "expo": { "experiments": { "inlineModules": {} } } }

expo.experiments.inlineModules.watchedDirectories

配置内联模块可以创建在哪些目录中。

app.json
{ "expo": { "experiments": { "inlineModules": { "watchedDirectories": ["app", "src"] } } } }

嵌套目录中的文件也将被使用。例如,如果在应用配置中定义了 watchedDirectories = ["app"],并且在嵌套路径中存在一个模块文件,例如 app/nested/directory/SomeModule.kt,那么 SomeModule 可以在你的应用中使用。

🌐 Files inside nested directories will also be used. For example, if watchedDirectories = ["app"] is defined in the app config, and a module file such as app/nested/directory/SomeModule.kt exists in a nested path, then SomeModule can be used in your app.

watchedDirectories 中的一个目录:

🌐 A directory in watchedDirectories:

  • 需要位于 TypeScript/JavaScript 项目中。这意味着它在目录树中需要有一个包含 package.json 的上级目录。例如,"watchedDirectories": ["app", "src/some/directory", pathToOtherProject] 可以使用,而 "watchedDirectories": ["/", pathToFolderNotInNodeProject] 不行。
  • 不能是整个项目目录,例如 "./",也不能是它的上级目录(例如 ../)。
  • watchedDirectories 中不能是另一个目录的子目录。例如,watchedDirectories 不能是 ["app", "app/nested/directory"],你可以直接将 watchedDirectories 设置为 ["app"]
  • 不能包含像 " ", "(", ")", "$" 这样的特殊字符。这意味着你不能在 watchedDirectories 中有 "app/(tabs)",但你可以有 "app",并且它仍然应该使用来自 "app/(tabs)" 目录的本地文件。

警告 更改 应用配置 后,你需要运行 npx expo prebuild 才能生效。

命名规范

🌐 Naming convention

内联模块的文件名必须与本地模块名匹配(该名称在整个应用中必须唯一)。 如果你有一个 SimpleModule.kt,那么其中的内联模块的文件名就是该文件名。例如:

🌐 The inline module file name has to match the native module name (which needs to be unique in your whole app). If you have a SimpleModule.kt, then the inline module inside it has that file name. For example:

// SimpleModule.kt // ... class SimpleModule: Module() { // Note that the class name has to match the filename. public func definition() -> ModuleDefinition { // Name("SimpleModule") // Note that `Name` also has to match the filename. So you can just omit it. } }