UNPKG

3.44 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: 0 /* ShouldBeInlined */, fileName: originalFileName, isExternal: false };
29 }
30 var typesLibraryName = node_modules_helpers_1.getTypesLibraryName(currentFilePath);
31 if (shouldLibraryBeInlined(npmLibraryName, typesLibraryName, criteria.inlinedLibraries)) {
32 return { type: 0 /* ShouldBeInlined */, fileName: originalFileName, isExternal: true };
33 }
34 if (shouldLibraryBeImported(npmLibraryName, typesLibraryName, criteria.importedLibraries)) {
35 return { type: 1 /* ShouldBeImported */, fileName: originalFileName, libraryName: typesLibraryName || npmLibraryName, isExternal: true };
36 }
37 if (typesLibraryName !== null && isLibraryAllowed(typesLibraryName, criteria.allowedTypesLibraries)) {
38 return { type: 2 /* ShouldBeReferencedAsTypes */, fileName: originalFileName, typesLibraryName: typesLibraryName, isExternal: true };
39 }
40 return { type: 3 /* ShouldBeUsedForModulesOnly */, fileName: originalFileName, isExternal: true };
41}
42function shouldLibraryBeInlined(npmLibraryName, typesLibraryName, inlinedLibraries) {
43 return isLibraryAllowed(npmLibraryName, inlinedLibraries) || typesLibraryName !== null && isLibraryAllowed(typesLibraryName, inlinedLibraries);
44}
45function shouldLibraryBeImported(npmLibraryName, typesLibraryName, importedLibraries) {
46 // npm library can be imported only when it is not from @types
47 var shouldNpmLibraryBeImported = typesLibraryName === null && isLibraryAllowed(npmLibraryName, importedLibraries);
48 // library from @types can be imported only when it is specified explicitly
49 var shouldTypesLibraryBeImported = importedLibraries !== undefined && typesLibraryName !== null && isLibraryAllowed(typesLibraryName, importedLibraries);
50 return shouldNpmLibraryBeImported || shouldTypesLibraryBeImported;
51}
52function isLibraryAllowed(libraryName, allowedArray) {
53 return allowedArray === undefined || allowedArray.indexOf(libraryName) !== -1;
54}
55function remapToTypesFromNodeModules(pathRelativeToTypesRoot) {
56 return "node_modules/@types/" + pathRelativeToTypesRoot;
57}