@rnx-kit/cli 0.9.54 → 0.9.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/CHANGELOG.md +5 -3
  2. package/coverage/clover.xml +110 -71
  3. package/coverage/coverage-final.json +3 -2
  4. package/coverage/lcov-report/index.html +43 -28
  5. package/coverage/lcov-report/src/bundle/index.html +15 -15
  6. package/coverage/lcov-report/src/bundle/kit-config.ts.html +1 -1
  7. package/coverage/lcov-report/src/bundle/metro.ts.html +15 -78
  8. package/coverage/lcov-report/src/bundle/overrides.ts.html +1 -1
  9. package/coverage/lcov-report/src/index.html +21 -21
  10. package/coverage/lcov-report/src/metro-config.ts.html +105 -129
  11. package/coverage/lcov-report/src/typescript/index.html +111 -0
  12. package/coverage/lcov-report/src/typescript/project-cache.ts.html +539 -0
  13. package/coverage/lcov.info +198 -126
  14. package/lib/bundle/metro.d.ts +1 -2
  15. package/lib/bundle/metro.d.ts.map +1 -1
  16. package/lib/bundle/metro.js +5 -17
  17. package/lib/bundle/metro.js.map +1 -1
  18. package/lib/bundle.d.ts.map +1 -1
  19. package/lib/bundle.js +1 -3
  20. package/lib/bundle.js.map +1 -1
  21. package/lib/metro-config.d.ts +3 -3
  22. package/lib/metro-config.d.ts.map +1 -1
  23. package/lib/metro-config.js +56 -67
  24. package/lib/metro-config.js.map +1 -1
  25. package/lib/start.d.ts.map +1 -1
  26. package/lib/start.js +6 -17
  27. package/lib/start.js.map +1 -1
  28. package/lib/types.d.ts +3 -4
  29. package/lib/types.d.ts.map +1 -1
  30. package/lib/typescript/project-cache.d.ts +38 -0
  31. package/lib/typescript/project-cache.d.ts.map +1 -0
  32. package/lib/typescript/project-cache.js +101 -0
  33. package/lib/typescript/project-cache.js.map +1 -0
  34. package/package.json +1 -1
  35. package/src/bundle/metro.ts +6 -27
  36. package/src/bundle.ts +1 -4
  37. package/src/metro-config.ts +73 -81
  38. package/src/start.ts +7 -26
  39. package/src/types.ts +3 -5
  40. package/src/typescript/project-cache.ts +153 -0
  41. package/CHANGELOG.json +0 -3289
@@ -0,0 +1,153 @@
1
+ import { findPackageDir } from "@rnx-kit/tools-node";
2
+ import {
3
+ AllPlatforms,
4
+ platformExtensions,
5
+ } from "@rnx-kit/tools-react-native/platform";
6
+ import { changeHostToUseReactNativeResolver } from "@rnx-kit/typescript-react-native-resolver";
7
+ import {
8
+ createDiagnosticWriter,
9
+ Project,
10
+ readConfigFile,
11
+ } from "@rnx-kit/typescript-service";
12
+ import path from "path";
13
+ import ts from "typescript";
14
+
15
+ /**
16
+ * Collection of TypeScript projects, separated by their target platform.
17
+ *
18
+ * Target platform is a react-native concept, not a TypeScript concept.
19
+ * However, each project is configured with react-native module resolution,
20
+ * which means the module file graph could vary by platform. And that means
21
+ * each platform could yield different type errors.
22
+ *
23
+ * For example, `import { f } from "./utils"` could load `./utils.android.ts`
24
+ * for Android and `./utils.ios.ts` iOS.
25
+ */
26
+ export interface ProjectCache {
27
+ /**
28
+ * Discard all cached projects targeting a specific platform.
29
+ *
30
+ * @param platform Target platform
31
+ */
32
+ clearPlatform(platform: AllPlatforms): void;
33
+
34
+ /**
35
+ * Get the project which targets a specific platform and contains a specific
36
+ * source file. If the project is not cached, load it and add it to the cache.
37
+ *
38
+ * @param platform Target platform
39
+ * @param sourceFile Source file
40
+ * @returns Project targeting the given platform and containing the given source file
41
+ */
42
+ getProject(sourceFile: string, platform: AllPlatforms): Project;
43
+ }
44
+
45
+ /**
46
+ * Create an empty cache for holding TypeScript projects.
47
+ *
48
+ * @param print Optional method for printing messages. When not set, messages are printed to the console.
49
+ * @returns Empty project cache
50
+ */
51
+ export function createProjectCache(
52
+ print?: (message: string) => void
53
+ ): ProjectCache {
54
+ const documentRegistry = ts.createDocumentRegistry();
55
+ const diagnosticWriter = createDiagnosticWriter(print);
56
+
57
+ // Collection of projects organized by root directory, then by platform.
58
+ const projects: Record<string, Partial<Record<AllPlatforms, Project>>> = {};
59
+
60
+ function findProjectRoot(sourceFile: string): string {
61
+ // Search known root directories to see if the source file is in one of them.
62
+ for (const root of Object.keys(projects)) {
63
+ if (sourceFile.startsWith(root)) {
64
+ return root;
65
+ }
66
+ }
67
+
68
+ // Search the file system for the root of source file's package.
69
+ const root = findPackageDir(path.dirname(sourceFile));
70
+ if (!root) {
71
+ throw new Error(
72
+ `Cannot find project root for source file '${sourceFile}'`
73
+ );
74
+ }
75
+
76
+ return root;
77
+ }
78
+
79
+ function readTSConfig(root: string): ts.ParsedCommandLine {
80
+ const configFileName = path.join(root, "tsconfig.json");
81
+
82
+ const cmdLine = readConfigFile(configFileName);
83
+ if (!cmdLine) {
84
+ throw new Error(`Failed to load '${configFileName}'`);
85
+ } else if (cmdLine.errors.length > 0) {
86
+ const writer = createDiagnosticWriter();
87
+ cmdLine.errors.forEach((e) => writer.print(e));
88
+ throw new Error(`Failed to load '${configFileName}'`);
89
+ }
90
+
91
+ return cmdLine;
92
+ }
93
+
94
+ function createProject(root: string, platform: AllPlatforms): Project {
95
+ // Load the TypeScript configuration file for this project.
96
+ const cmdLine = readTSConfig(root);
97
+
98
+ // Create a TypeScript project using the configuration file. Enhance the
99
+ // underlying TS language service with our react-native module resolver.
100
+ const enhanceLanguageServiceHost = (host: ts.LanguageServiceHost): void => {
101
+ const platformExtensionNames = platformExtensions(platform);
102
+ const disableReactNativePackageSubstitution = true;
103
+ const traceReactNativeModuleResolutionErrors = false;
104
+ const traceResolutionLog = undefined;
105
+ changeHostToUseReactNativeResolver({
106
+ host,
107
+ options: cmdLine.options,
108
+ platform,
109
+ platformExtensionNames,
110
+ disableReactNativePackageSubstitution,
111
+ traceReactNativeModuleResolutionErrors,
112
+ traceResolutionLog,
113
+ });
114
+ };
115
+ const tsproject = new Project(
116
+ documentRegistry,
117
+ diagnosticWriter,
118
+ cmdLine,
119
+ enhanceLanguageServiceHost
120
+ );
121
+
122
+ // Start with an empty project, ignoring the file graph from tsconfig.json.
123
+ tsproject.removeAllFiles();
124
+
125
+ return tsproject;
126
+ }
127
+
128
+ function getProject(sourceFile: string, platform: AllPlatforms): Project {
129
+ const root = findProjectRoot(sourceFile);
130
+ if (!projects[root]) {
131
+ projects[root] = {};
132
+ }
133
+ if (!projects[root][platform]) {
134
+ projects[root][platform] = createProject(root, platform);
135
+ }
136
+ return projects[root][platform]!;
137
+ }
138
+
139
+ function clearPlatform(platform: AllPlatforms): void {
140
+ Object.values(projects).forEach((projectsByPlatform) => {
141
+ const project = projectsByPlatform[platform];
142
+ if (project) {
143
+ project.dispose();
144
+ delete projectsByPlatform[platform];
145
+ }
146
+ });
147
+ }
148
+
149
+ return {
150
+ clearPlatform,
151
+ getProject,
152
+ };
153
+ }