@rnx-kit/cli 0.15.1 → 0.15.2
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.
- package/CHANGELOG.md +8 -0
- package/coverage/clover.xml +23 -115
- package/coverage/coverage-final.json +1 -2
- package/coverage/lcov-report/index.html +20 -35
- package/coverage/lcov-report/src/bundle/defaultPlugins.ts.html +1 -1
- package/coverage/lcov-report/src/bundle/index.html +1 -1
- package/coverage/lcov-report/src/bundle/kit-config.ts.html +1 -1
- package/coverage/lcov-report/src/bundle/metro.ts.html +1 -1
- package/coverage/lcov-report/src/bundle/overrides.ts.html +1 -1
- package/coverage/lcov-report/src/copy-assets.ts.html +1 -1
- package/coverage/lcov-report/src/index.html +20 -20
- package/coverage/lcov-report/src/metro-config.ts.html +14 -323
- package/coverage/lcov.info +36 -207
- package/lib/metro-config.d.ts.map +1 -1
- package/lib/metro-config.js +2 -78
- package/lib/metro-config.js.map +1 -1
- package/package.json +5 -7
- package/src/metro-config.ts +3 -106
- package/coverage/lcov-report/src/typescript/index.html +0 -116
- package/coverage/lcov-report/src/typescript/project-cache.ts.html +0 -700
- package/lib/typescript/project-cache.d.ts +0 -42
- package/lib/typescript/project-cache.d.ts.map +0 -1
- package/lib/typescript/project-cache.js +0 -155
- package/lib/typescript/project-cache.js.map +0 -1
- package/src/typescript/project-cache.ts +0 -205
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { AllPlatforms } from "@rnx-kit/tools-react-native/platform";
|
|
2
|
-
import { Project } from "@rnx-kit/typescript-service";
|
|
3
|
-
export type ProjectInfo = {
|
|
4
|
-
tsproject: Project;
|
|
5
|
-
tssourceFiles: Set<string>;
|
|
6
|
-
};
|
|
7
|
-
/**
|
|
8
|
-
* Collection of TypeScript projects, separated by their target platform.
|
|
9
|
-
*
|
|
10
|
-
* Target platform is a react-native concept, not a TypeScript concept.
|
|
11
|
-
* However, each project is configured with react-native module resolution,
|
|
12
|
-
* which means the module file graph could vary by platform. And that means
|
|
13
|
-
* each platform could yield different type errors.
|
|
14
|
-
*
|
|
15
|
-
* For example, `import { f } from "./utils"` could load `./utils.android.ts`
|
|
16
|
-
* for Android and `./utils.ios.ts` iOS.
|
|
17
|
-
*/
|
|
18
|
-
export interface ProjectCache {
|
|
19
|
-
/**
|
|
20
|
-
* Discard all cached projects targeting a specific platform.
|
|
21
|
-
*
|
|
22
|
-
* @param platform Target platform
|
|
23
|
-
*/
|
|
24
|
-
clearPlatform(platform: AllPlatforms): void;
|
|
25
|
-
/**
|
|
26
|
-
* Get info on the project which targets a specific platform and contains a specific
|
|
27
|
-
* source file. If the project is not cached, load it and add it to the cache.
|
|
28
|
-
*
|
|
29
|
-
* @param platform Target platform
|
|
30
|
-
* @param sourceFile Source file
|
|
31
|
-
* @returns Project targeting the given platform and containing the given source file
|
|
32
|
-
*/
|
|
33
|
-
getProjectInfo(sourceFile: string, platform: AllPlatforms): ProjectInfo | undefined;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Create an empty cache for holding TypeScript projects.
|
|
37
|
-
*
|
|
38
|
-
* @param print Optional method for printing messages. When not set, messages are printed to the console.
|
|
39
|
-
* @returns Empty project cache
|
|
40
|
-
*/
|
|
41
|
-
export declare function createProjectCache(print?: (message: string) => void): ProjectCache;
|
|
42
|
-
//# sourceMappingURL=project-cache.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"project-cache.d.ts","sourceRoot":"","sources":["../../src/typescript/project-cache.ts"],"names":[],"mappings":"AACA,OAAO,EACL,YAAY,EAEb,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAEL,OAAO,EAER,MAAM,6BAA6B,CAAC;AAKrC,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,aAAa,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAC;IAE5C;;;;;;;OAOG;IACH,cAAc,CACZ,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,YAAY,GACrB,WAAW,GAAG,SAAS,CAAC;CAC5B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,GAChC,YAAY,CA+Id"}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.createProjectCache = void 0;
|
|
30
|
-
const tools_node_1 = require("@rnx-kit/tools-node");
|
|
31
|
-
const platform_1 = require("@rnx-kit/tools-react-native/platform");
|
|
32
|
-
const typescript_react_native_resolver_1 = require("@rnx-kit/typescript-react-native-resolver");
|
|
33
|
-
const typescript_service_1 = require("@rnx-kit/typescript-service");
|
|
34
|
-
const fs = __importStar(require("fs"));
|
|
35
|
-
const path = __importStar(require("path"));
|
|
36
|
-
const typescript_1 = __importDefault(require("typescript"));
|
|
37
|
-
/**
|
|
38
|
-
* Create an empty cache for holding TypeScript projects.
|
|
39
|
-
*
|
|
40
|
-
* @param print Optional method for printing messages. When not set, messages are printed to the console.
|
|
41
|
-
* @returns Empty project cache
|
|
42
|
-
*/
|
|
43
|
-
function createProjectCache(print) {
|
|
44
|
-
const documentRegistry = typescript_1.default.createDocumentRegistry();
|
|
45
|
-
const diagnosticWriter = (0, typescript_service_1.createDiagnosticWriter)(print);
|
|
46
|
-
// Collection of projects organized by root directory, then by platform.
|
|
47
|
-
const projects = {};
|
|
48
|
-
function findProjectRoot(sourceFile) {
|
|
49
|
-
// Search known root directories to see if the source file is in one of them.
|
|
50
|
-
for (const root of Object.keys(projects)) {
|
|
51
|
-
if (sourceFile.startsWith(root)) {
|
|
52
|
-
return root;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
// Search the file system for the root of source file's package.
|
|
56
|
-
const root = (0, tools_node_1.findPackageDir)(path.dirname(sourceFile));
|
|
57
|
-
if (!root) {
|
|
58
|
-
throw new Error(`Cannot find project root for source file '${sourceFile}'`);
|
|
59
|
-
}
|
|
60
|
-
return root;
|
|
61
|
-
}
|
|
62
|
-
function readTSConfig(root) {
|
|
63
|
-
const configFileName = path.join(root, "tsconfig.json");
|
|
64
|
-
if (!fs.existsSync(configFileName)) {
|
|
65
|
-
// Allow for packages that aren't TypeScript.
|
|
66
|
-
//
|
|
67
|
-
// Example: Users who enable bundling with all the config defaults will
|
|
68
|
-
// have type validation enabled automatically. They may not actually be
|
|
69
|
-
// using TypeScript.
|
|
70
|
-
//
|
|
71
|
-
// We shouldn't break them. We should use TS validation only for TS packages.
|
|
72
|
-
//
|
|
73
|
-
return undefined;
|
|
74
|
-
}
|
|
75
|
-
const cmdLine = (0, typescript_service_1.readConfigFile)(configFileName);
|
|
76
|
-
if (!cmdLine) {
|
|
77
|
-
throw new Error(`Failed to load '${configFileName}'`);
|
|
78
|
-
}
|
|
79
|
-
else if (cmdLine.errors.length > 0) {
|
|
80
|
-
const writer = (0, typescript_service_1.createDiagnosticWriter)();
|
|
81
|
-
cmdLine.errors.forEach((e) => writer.print(e));
|
|
82
|
-
throw new Error(`Failed to load '${configFileName}'`);
|
|
83
|
-
}
|
|
84
|
-
return cmdLine;
|
|
85
|
-
}
|
|
86
|
-
function createProjectInfo(root, platform) {
|
|
87
|
-
// Load the TypeScript configuration file for this project.
|
|
88
|
-
const cmdLine = readTSConfig(root);
|
|
89
|
-
if (!cmdLine) {
|
|
90
|
-
// Not a TypeScript project
|
|
91
|
-
return undefined;
|
|
92
|
-
}
|
|
93
|
-
// Trim down the list of source files found by TypeScript. This ensures
|
|
94
|
-
// that only explicitly added files are loaded and parsed by TypeScript.
|
|
95
|
-
// This is a perf optimization. We don't want to spend cycles on files
|
|
96
|
-
// that won't be used, such as *.android.ts when bundling for ios.
|
|
97
|
-
//
|
|
98
|
-
// The exception to this rule are .d.ts files. They hold global types,
|
|
99
|
-
// modules and namespaces, and having them in the project is the only
|
|
100
|
-
// way they can be loaded.
|
|
101
|
-
const tssourceFiles = new Set(cmdLine.fileNames);
|
|
102
|
-
cmdLine.fileNames = cmdLine.fileNames.filter((f) => f.endsWith(".d.ts"));
|
|
103
|
-
// Create a TypeScript project using the configuration file. Enhance the
|
|
104
|
-
// underlying TS language service with our react-native module resolver.
|
|
105
|
-
const enhanceLanguageServiceHost = (host) => {
|
|
106
|
-
const platformExtensionNames = (0, platform_1.platformExtensions)(platform);
|
|
107
|
-
const disableReactNativePackageSubstitution = true;
|
|
108
|
-
(0, typescript_react_native_resolver_1.changeHostToUseReactNativeResolver)({
|
|
109
|
-
host,
|
|
110
|
-
options: cmdLine.options,
|
|
111
|
-
platform,
|
|
112
|
-
platformExtensionNames,
|
|
113
|
-
disableReactNativePackageSubstitution,
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
const tsproject = new typescript_service_1.Project(documentRegistry, diagnosticWriter, cmdLine, enhanceLanguageServiceHost);
|
|
117
|
-
return {
|
|
118
|
-
tsproject,
|
|
119
|
-
tssourceFiles,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
function getProjectInfo(sourceFile, platform) {
|
|
123
|
-
const root = findProjectRoot(sourceFile);
|
|
124
|
-
projects[root] || (projects[root] = {});
|
|
125
|
-
const platforms = projects[root];
|
|
126
|
-
// Have we seen the project/platform for this source file before,
|
|
127
|
-
// even if what we saw is 'undefined' (e.g. not a TS project)?
|
|
128
|
-
if (Object.prototype.hasOwnProperty.call(platforms, platform)) {
|
|
129
|
-
return platforms[platform];
|
|
130
|
-
}
|
|
131
|
-
// We haven't seen this project/platform before. Try to load it,
|
|
132
|
-
// even if it isn't a TS project. Cache the result so we don't
|
|
133
|
-
// do this again.
|
|
134
|
-
const info = createProjectInfo(root, platform);
|
|
135
|
-
platforms[platform] = info;
|
|
136
|
-
return info;
|
|
137
|
-
}
|
|
138
|
-
function clearPlatform(platform) {
|
|
139
|
-
Object.values(projects).forEach((projectsByPlatform) => {
|
|
140
|
-
const info = projectsByPlatform[platform];
|
|
141
|
-
if (info) {
|
|
142
|
-
if (info.tsproject) {
|
|
143
|
-
info.tsproject.dispose();
|
|
144
|
-
}
|
|
145
|
-
delete projectsByPlatform[platform];
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
return {
|
|
150
|
-
clearPlatform,
|
|
151
|
-
getProjectInfo,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
exports.createProjectCache = createProjectCache;
|
|
155
|
-
//# sourceMappingURL=project-cache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"project-cache.js","sourceRoot":"","sources":["../../src/typescript/project-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAqD;AACrD,mEAG8C;AAC9C,gGAA+F;AAC/F,oEAIqC;AACrC,uCAAyB;AACzB,2CAA6B;AAC7B,4DAA4B;AAwC5B;;;;;GAKG;AACH,SAAgB,kBAAkB,CAChC,KAAiC;IAEjC,MAAM,gBAAgB,GAAG,oBAAE,CAAC,sBAAsB,EAAE,CAAC;IACrD,MAAM,gBAAgB,GAAG,IAAA,2CAAsB,EAAC,KAAK,CAAC,CAAC;IAEvD,wEAAwE;IACxE,MAAM,QAAQ,GAGV,EAAE,CAAC;IAEP,SAAS,eAAe,CAAC,UAAkB;QACzC,6EAA6E;QAC7E,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YACxC,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;gBAC/B,OAAO,IAAI,CAAC;aACb;SACF;QAED,gEAAgE;QAChE,MAAM,IAAI,GAAG,IAAA,2BAAc,EAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,EAAE;YACT,MAAM,IAAI,KAAK,CACb,6CAA6C,UAAU,GAAG,CAC3D,CAAC;SACH;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,SAAS,YAAY,CAAC,IAAY;QAChC,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;QACxD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE;YAClC,6CAA6C;YAC7C,EAAE;YACF,uEAAuE;YACvE,uEAA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SAAS,CAAC,OAAO,EAAE,CAAC;iBAC1B;gBACD,OAAO,kBAAkB,CAAC,QAAQ,CAAC,CAAC;aACrC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,aAAa;QACb,cAAc;KACf,CAAC;AACJ,CAAC;AAjJD,gDAiJC"}
|
|
@@ -1,205 +0,0 @@
|
|
|
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 * as fs from "fs";
|
|
13
|
-
import * as path from "path";
|
|
14
|
-
import ts from "typescript";
|
|
15
|
-
|
|
16
|
-
export type ProjectInfo = {
|
|
17
|
-
tsproject: Project;
|
|
18
|
-
tssourceFiles: Set<string>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Collection of TypeScript projects, separated by their target platform.
|
|
23
|
-
*
|
|
24
|
-
* Target platform is a react-native concept, not a TypeScript concept.
|
|
25
|
-
* However, each project is configured with react-native module resolution,
|
|
26
|
-
* which means the module file graph could vary by platform. And that means
|
|
27
|
-
* each platform could yield different type errors.
|
|
28
|
-
*
|
|
29
|
-
* For example, `import { f } from "./utils"` could load `./utils.android.ts`
|
|
30
|
-
* for Android and `./utils.ios.ts` iOS.
|
|
31
|
-
*/
|
|
32
|
-
export interface ProjectCache {
|
|
33
|
-
/**
|
|
34
|
-
* Discard all cached projects targeting a specific platform.
|
|
35
|
-
*
|
|
36
|
-
* @param platform Target platform
|
|
37
|
-
*/
|
|
38
|
-
clearPlatform(platform: AllPlatforms): void;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Get info on the project which targets a specific platform and contains a specific
|
|
42
|
-
* source file. If the project is not cached, load it and add it to the cache.
|
|
43
|
-
*
|
|
44
|
-
* @param platform Target platform
|
|
45
|
-
* @param sourceFile Source file
|
|
46
|
-
* @returns Project targeting the given platform and containing the given source file
|
|
47
|
-
*/
|
|
48
|
-
getProjectInfo(
|
|
49
|
-
sourceFile: string,
|
|
50
|
-
platform: AllPlatforms
|
|
51
|
-
): ProjectInfo | undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Create an empty cache for holding TypeScript projects.
|
|
56
|
-
*
|
|
57
|
-
* @param print Optional method for printing messages. When not set, messages are printed to the console.
|
|
58
|
-
* @returns Empty project cache
|
|
59
|
-
*/
|
|
60
|
-
export function createProjectCache(
|
|
61
|
-
print?: (message: string) => void
|
|
62
|
-
): ProjectCache {
|
|
63
|
-
const documentRegistry = ts.createDocumentRegistry();
|
|
64
|
-
const diagnosticWriter = createDiagnosticWriter(print);
|
|
65
|
-
|
|
66
|
-
// Collection of projects organized by root directory, then by platform.
|
|
67
|
-
const projects: Record<
|
|
68
|
-
string,
|
|
69
|
-
Partial<Record<AllPlatforms, ProjectInfo>>
|
|
70
|
-
> = {};
|
|
71
|
-
|
|
72
|
-
function findProjectRoot(sourceFile: string): string {
|
|
73
|
-
// Search known root directories to see if the source file is in one of them.
|
|
74
|
-
for (const root of Object.keys(projects)) {
|
|
75
|
-
if (sourceFile.startsWith(root)) {
|
|
76
|
-
return root;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Search the file system for the root of source file's package.
|
|
81
|
-
const root = findPackageDir(path.dirname(sourceFile));
|
|
82
|
-
if (!root) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
`Cannot find project root for source file '${sourceFile}'`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return root;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function readTSConfig(root: string): ts.ParsedCommandLine | undefined {
|
|
92
|
-
const configFileName = path.join(root, "tsconfig.json");
|
|
93
|
-
if (!fs.existsSync(configFileName)) {
|
|
94
|
-
// Allow for packages that aren't TypeScript.
|
|
95
|
-
//
|
|
96
|
-
// Example: Users who enable bundling with all the config defaults will
|
|
97
|
-
// have type validation enabled automatically. They may not actually be
|
|
98
|
-
// using TypeScript.
|
|
99
|
-
//
|
|
100
|
-
// We shouldn't break them. We should use TS validation only for TS packages.
|
|
101
|
-
//
|
|
102
|
-
return undefined;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const cmdLine = readConfigFile(configFileName);
|
|
106
|
-
if (!cmdLine) {
|
|
107
|
-
throw new Error(`Failed to load '${configFileName}'`);
|
|
108
|
-
} else if (cmdLine.errors.length > 0) {
|
|
109
|
-
const writer = createDiagnosticWriter();
|
|
110
|
-
cmdLine.errors.forEach((e) => writer.print(e));
|
|
111
|
-
throw new Error(`Failed to load '${configFileName}'`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return cmdLine;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function createProjectInfo(
|
|
118
|
-
root: string,
|
|
119
|
-
platform: AllPlatforms
|
|
120
|
-
): ProjectInfo | undefined {
|
|
121
|
-
// Load the TypeScript configuration file for this project.
|
|
122
|
-
const cmdLine = readTSConfig(root);
|
|
123
|
-
if (!cmdLine) {
|
|
124
|
-
// Not a TypeScript project
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Trim down the list of source files found by TypeScript. This ensures
|
|
129
|
-
// that only explicitly added files are loaded and parsed by TypeScript.
|
|
130
|
-
// This is a perf optimization. We don't want to spend cycles on files
|
|
131
|
-
// that won't be used, such as *.android.ts when bundling for ios.
|
|
132
|
-
//
|
|
133
|
-
// The exception to this rule are .d.ts files. They hold global types,
|
|
134
|
-
// modules and namespaces, and having them in the project is the only
|
|
135
|
-
// way they can be loaded.
|
|
136
|
-
|
|
137
|
-
const tssourceFiles = new Set(cmdLine.fileNames);
|
|
138
|
-
cmdLine.fileNames = cmdLine.fileNames.filter((f) => f.endsWith(".d.ts"));
|
|
139
|
-
|
|
140
|
-
// Create a TypeScript project using the configuration file. Enhance the
|
|
141
|
-
// underlying TS language service with our react-native module resolver.
|
|
142
|
-
const enhanceLanguageServiceHost = (host: ts.LanguageServiceHost): void => {
|
|
143
|
-
const platformExtensionNames = platformExtensions(platform);
|
|
144
|
-
const disableReactNativePackageSubstitution = true;
|
|
145
|
-
changeHostToUseReactNativeResolver({
|
|
146
|
-
host,
|
|
147
|
-
options: cmdLine.options,
|
|
148
|
-
platform,
|
|
149
|
-
platformExtensionNames,
|
|
150
|
-
disableReactNativePackageSubstitution,
|
|
151
|
-
});
|
|
152
|
-
};
|
|
153
|
-
const tsproject = new Project(
|
|
154
|
-
documentRegistry,
|
|
155
|
-
diagnosticWriter,
|
|
156
|
-
cmdLine,
|
|
157
|
-
enhanceLanguageServiceHost
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
tsproject,
|
|
162
|
-
tssourceFiles,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function getProjectInfo(
|
|
167
|
-
sourceFile: string,
|
|
168
|
-
platform: AllPlatforms
|
|
169
|
-
): ProjectInfo | undefined {
|
|
170
|
-
const root = findProjectRoot(sourceFile);
|
|
171
|
-
projects[root] ||= {};
|
|
172
|
-
|
|
173
|
-
const platforms = projects[root];
|
|
174
|
-
|
|
175
|
-
// Have we seen the project/platform for this source file before,
|
|
176
|
-
// even if what we saw is 'undefined' (e.g. not a TS project)?
|
|
177
|
-
if (Object.prototype.hasOwnProperty.call(platforms, platform)) {
|
|
178
|
-
return platforms[platform];
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// We haven't seen this project/platform before. Try to load it,
|
|
182
|
-
// even if it isn't a TS project. Cache the result so we don't
|
|
183
|
-
// do this again.
|
|
184
|
-
const info = createProjectInfo(root, platform);
|
|
185
|
-
platforms[platform] = info;
|
|
186
|
-
return info;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function clearPlatform(platform: AllPlatforms): void {
|
|
190
|
-
Object.values(projects).forEach((projectsByPlatform) => {
|
|
191
|
-
const info = projectsByPlatform[platform];
|
|
192
|
-
if (info) {
|
|
193
|
-
if (info.tsproject) {
|
|
194
|
-
info.tsproject.dispose();
|
|
195
|
-
}
|
|
196
|
-
delete projectsByPlatform[platform];
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
clearPlatform,
|
|
203
|
-
getProjectInfo,
|
|
204
|
-
};
|
|
205
|
-
}
|