1import { FileTransforms } from '../../../Transforms.types';
2
3const objcFilesPattern = '*.{h,m,mm}';
4const swiftFilesPattern = '*.swift';
5
6export function expoModulesTransforms(prefix: string): FileTransforms {
7  return {
8    path: [
9      // Here we prefix names of Objective-C/C++ files.
10      // There is no need to do the same for Swift files
11      // as we don't change the symbols inside. In Swift we don't use `EX` nor `UM`
12      // for symbols as the framework name takes part of the symbol signature,
13      // so there is no way we'll get duplicate symbols compilation errors.
14      // Files starting with `Expo` needs to be prefixed though,
15      // for umbrella headers (e.g. `ExpoModulesCore.h`).
16      {
17        find: /\b(Expo|EX|UM)([^/]*\.)(h|m|mm|cpp)\b/,
18        replaceWith: `${prefix}$1$2$3`,
19      },
20    ],
21    content: [
22      {
23        find: /\bReact(?!Common)/g,
24        replaceWith: `${prefix}React`,
25      },
26      {
27        // Prefix symbols and imports.
28        find: /\b(EX|UM|RCT)/g,
29        replaceWith: `${prefix}$1`,
30      },
31
32      // Only Swift
33
34      {
35        paths: swiftFilesPattern,
36        find: /\bimport (Expo|EX)(\w+)/g,
37        replaceWith: `import ${prefix}$1$2`,
38      },
39      {
40        paths: swiftFilesPattern,
41        find: /@objc\((Expo|EX)\)/g,
42        replaceWith: `@objc(${prefix}$1)`,
43      },
44
45      // Only Objective-C
46
47      {
48        // Prefix `Expo*` frameworks in imports.
49        paths: objcFilesPattern,
50        find: /#import <(Expo.*?)\//g,
51        replaceWith: `#import <${prefix}$1/`,
52      },
53      {
54        paths: objcFilesPattern,
55        find: /#import <(.*?)\/(Expo.*?)\.h>/g,
56        replaceWith: `#import <$1/${prefix}$2.h>`,
57      },
58      {
59        // Rename Swift compatibility headers from frameworks starting with `Expo`.
60        paths: objcFilesPattern,
61        find: /#import "(Expo.+?)-Swift\.h"/g,
62        replaceWith: `#import "${prefix}$1-Swift.h"`,
63      },
64      {
65        // Unprefix imports to unversionable (e.g. expo-gl-cpp) modules.
66        paths: [objcFilesPattern, 'EXGL'],
67        find: new RegExp(`#import <${prefix}(EXGL_CPP)\\b`),
68        replaceWith: '#import <$1',
69      },
70      {
71        // Prefixes Objective-C name of the Swift modules provider.
72        paths: ['EXNativeModulesProxy.m'],
73        find: 'NSClassFromString(@"ExpoModulesProvider")',
74        replaceWith: `NSClassFromString(@"${prefix}ExpoModulesProvider")`
75      },
76      {
77        // Prefixes Objective-C name of the Swift modules provider.
78        paths: ['EXNativeModulesProxy.m'],
79        find: '[NSString stringWithFormat:@"%@.ExpoModulesProvider"',
80        replaceWith: `[NSString stringWithFormat:@"%@.${prefix}ExpoModulesProvider"`
81      },
82      {
83        // Prefixes imports from other React Native libs
84        paths: objcFilesPattern,
85        find: new RegExp(`#import <(ReactCommon|jsi)/(${prefix})?`, 'g'),
86        replaceWith: `#import <${prefix}$1/${prefix}`,
87      },
88      {
89        // Prefixes versionable namespaces
90        paths: objcFilesPattern,
91        find: /\bnamespace (expo|facebook)\b/g,
92        replaceWith: `namespace ${prefix}$1`,
93      },
94      {
95        // Prefixes usages of versionable namespaces
96        paths: objcFilesPattern,
97        find: /\b(expo|facebook)::/g,
98        replaceWith: `${prefix}$1::`,
99      },
100
101      // Prefixes versionable namespaces (react namespace is already prefixed with uppercased "R")
102      {
103        paths: objcFilesPattern,
104        find: /::react::/g,
105        replaceWith: `::${prefix}React::`,
106      },
107      {
108        paths: objcFilesPattern,
109        find: /\bnamespace react\b/g,
110        replaceWith: `namespace ${prefix}React`,
111      },
112
113      // Prefix umbrella header imports
114      {
115        paths: '*.h',
116        find: /\b(\w+-umbrella\.h)\b/g,
117        replaceWith: `${prefix}$1`,
118      }
119    ],
120  };
121}
122