UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path = require("path");
4var node_modules_helpers_1 = require("./node-modules-helpers");
5function getModuleInfo(fileName, criteria) {
6 return getModuleInfoImpl(fileName, fileName, criteria);
7}
8exports.getModuleInfo = getModuleInfo;
9/**
10 * @param currentFilePath Current file path - can be used to override actual path of module (e.g. with `typeRoots`)
11 * @param originalFileName Original file name of the module
12 * @param criteria Criteria of module info
13 */
14function getModuleInfoImpl(currentFilePath, originalFileName, criteria) {
15 var npmLibraryName = node_modules_helpers_1.getLibraryName(currentFilePath);
16 if (npmLibraryName === null) {
17 if (criteria.typeRoots !== undefined) {
18 for (var _i = 0, _a = criteria.typeRoots; _i < _a.length; _i++) {
19 var root = _a[_i];
20 var relativePath = path.relative(root, originalFileName).replace(/\\/g, '/');
21 if (!relativePath.startsWith('../')) {
22 // relativePath is path relative to type root
23 // so we should treat it as "library from node_modules/@types/"
24 return getModuleInfoImpl(remapToTypesFromNodeModules(relativePath), originalFileName, criteria);
25 }
26 }
27 }
28 return { type: 1 /* ShouldBeInlined */, fileName: originalFileName, isExternal: false };
29 }
30 if (node_modules_helpers_1.isTypescriptLibFile(currentFilePath)) {
31 return { type: 0 /* ShouldNotBeUsed */ };
32 }
33 var typesLibraryName = node_modules_helpers_1.getTypesLibraryName(currentFilePath);
34 if (shouldLibraryBeInlined(npmLibraryName, typesLibraryName, criteria.inlinedLibraries)) {
35 return { type: 1 /* ShouldBeInlined */, fileName: originalFileName, isExternal: true };
36 }
37 if (shouldLibraryBeImported(npmLibraryName, typesLibraryName, criteria.importedLibraries)) {
38 return { type: 2 /* ShouldBeImported */, fileName: originalFileName, libraryName: typesLibraryName || npmLibraryName, isExternal: true };
39 }
40 if (typesLibraryName !== null && isLibraryAllowed(typesLibraryName, criteria.allowedTypesLibraries)) {
41 return { type: 3 /* ShouldBeReferencedAsTypes */, fileName: originalFileName, typesLibraryName: typesLibraryName, isExternal: true };
42 }
43 return { type: 4 /* ShouldBeUsedForModulesOnly */, fileName: originalFileName, isExternal: true };
44}
45function shouldLibraryBeInlined(npmLibraryName, typesLibraryName, inlinedLibraries) {
46 return isLibraryAllowed(npmLibraryName, inlinedLibraries) || typesLibraryName !== null && isLibraryAllowed(typesLibraryName, inlinedLibraries);
47}
48function shouldLibraryBeImported(npmLibraryName, typesLibraryName, importedLibraries) {
49 // npm library can be imported only when it is not from @types
50 var shouldNpmLibraryBeImported = typesLibraryName === null && isLibraryAllowed(npmLibraryName, importedLibraries);
51 // library from @types can be imported only when it is specified explicitly
52 var shouldTypesLibraryBeImported = importedLibraries !== undefined && typesLibraryName !== null && isLibraryAllowed(typesLibraryName, importedLibraries);
53 return shouldNpmLibraryBeImported || shouldTypesLibraryBeImported;
54}
55function isLibraryAllowed(libraryName, allowedArray) {
56 return allowedArray === undefined || allowedArray.indexOf(libraryName) !== -1;
57}
58function remapToTypesFromNodeModules(pathRelativeToTypesRoot) {
59 return "node_modules/@types/" + pathRelativeToTypesRoot;
60}