Skip to content

Commit 2e313ca

Browse files
authored
Merge pull request #14100 from Microsoft/BowerComponentsFix
Type acquisition support for bower_components directory
2 parents e298f75 + 8d1c9d5 commit 2e313ca

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/harness/unittests/typingsInstaller.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,54 @@ namespace ts.projectSystem {
692692
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
693693
});
694694

695-
it("configured projects discover from bower.josn", () => {
695+
it("configured projects discover from bower_components", () => {
696+
const app = {
697+
path: "/app.js",
698+
content: ""
699+
};
700+
const jsconfig = {
701+
path: "/jsconfig.json",
702+
content: JSON.stringify({})
703+
};
704+
const jquery = {
705+
path: "/bower_components/jquery/index.js",
706+
content: ""
707+
};
708+
const jqueryPackage = {
709+
path: "/bower_components/jquery/package.json",
710+
content: JSON.stringify({ name: "jquery" })
711+
};
712+
const jqueryDTS = {
713+
path: "/tmp/node_modules/@types/jquery/index.d.ts",
714+
content: ""
715+
};
716+
const host = createServerHost([app, jsconfig, jquery, jqueryPackage]);
717+
const installer = new (class extends Installer {
718+
constructor() {
719+
super(host, { globalTypingsCacheLocation: "/tmp", typesRegistry: createTypesRegistry("jquery") });
720+
}
721+
installWorker(_requestId: number, _args: string[], _cwd: string, cb: server.typingsInstaller.RequestCompletedAction) {
722+
const installedTypings = ["@types/jquery"];
723+
const typingFiles = [jqueryDTS];
724+
executeCommand(this, host, installedTypings, typingFiles, cb);
725+
}
726+
})();
727+
728+
const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer });
729+
projectService.openClientFile(app.path);
730+
731+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
732+
const p = projectService.configuredProjects[0];
733+
checkProjectActualFiles(p, [app.path]);
734+
checkWatchedFiles(host, [jsconfig.path, "/bower_components", "/node_modules"]);
735+
736+
installer.installAll(/*expectedCount*/ 1);
737+
738+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
739+
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
740+
});
741+
742+
it("configured projects discover from bower.json", () => {
696743
const app = {
697744
path: "/app.js",
698745
content: ""
@@ -975,7 +1022,7 @@ namespace ts.projectSystem {
9751022
}
9761023
});
9771024

978-
it("should use cached locaitons", () => {
1025+
it("should use cached locations", () => {
9791026
const f = {
9801027
path: "/a/b/app.js",
9811028
content: ""

src/services/jsTyping.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ namespace ts.JsTyping {
9999
const bowerJsonPath = combinePaths(searchDir, "bower.json");
100100
getTypingNamesFromJson(bowerJsonPath, filesToWatch);
101101

102+
const bowerComponentsPath = combinePaths(searchDir, "bower_components");
103+
getTypingNamesFromPackagesFolder(bowerComponentsPath);
104+
102105
const nodeModulesPath = combinePaths(searchDir, "node_modules");
103-
getTypingNamesFromNodeModuleFolder(nodeModulesPath);
106+
getTypingNamesFromPackagesFolder(nodeModulesPath);
104107
}
105108
getTypingNamesFromSourceFileNames(fileNames);
106109

@@ -199,20 +202,24 @@ namespace ts.JsTyping {
199202
}
200203

201204
/**
202-
* Infer typing names from node_module folder
203-
* @param nodeModulesPath is the path to the "node_modules" folder
205+
* Infer typing names from packages folder (ex: node_module, bower_components)
206+
* @param packagesFolderPath is the path to the packages folder
207+
204208
*/
205-
function getTypingNamesFromNodeModuleFolder(nodeModulesPath: string) {
209+
function getTypingNamesFromPackagesFolder(packagesFolderPath: string) {
210+
filesToWatch.push(packagesFolderPath);
211+
206212
// Todo: add support for ModuleResolutionHost too
207-
if (!host.directoryExists(nodeModulesPath)) {
213+
if (!host.directoryExists(packagesFolderPath)) {
208214
return;
209215
}
210216

211217
const typingNames: string[] = [];
212-
const fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
218+
const fileNames = host.readDirectory(packagesFolderPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
213219
for (const fileName of fileNames) {
214220
const normalizedFileName = normalizePath(fileName);
215-
if (getBaseFileName(normalizedFileName) !== "package.json") {
221+
const baseFileName = getBaseFileName(normalizedFileName);
222+
if (baseFileName !== "package.json" && baseFileName !== "bower.json") {
216223
continue;
217224
}
218225
const result = readConfigFile(normalizedFileName, (path: string) => host.readFile(path));
@@ -224,7 +231,7 @@ namespace ts.JsTyping {
224231
// npm 3's package.json contains a "_requiredBy" field
225232
// we should include all the top level module names for npm 2, and only module names whose
226233
// "_requiredBy" field starts with "#" or equals "/" for npm 3.
227-
if (packageJson._requiredBy &&
234+
if (baseFileName === "package.json" && packageJson._requiredBy &&
228235
filter(packageJson._requiredBy, (r: string) => r[0] === "#" || r === "/").length === 0) {
229236
continue;
230237
}

0 commit comments

Comments
 (0)