UNPKG

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