@ms-cloudpack/package-utilities 5.1.13 → 5.1.14
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/lib/findFileInPackage.d.ts.map +1 -1
- package/lib/findFileInPackage.js +40 -43
- package/lib/findFileInPackage.js.map +1 -1
- package/lib/getCandidates.d.ts +12 -0
- package/lib/getCandidates.d.ts.map +1 -0
- package/lib/getCandidates.js +98 -0
- package/lib/getCandidates.js.map +1 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findFileInPackage.d.ts","sourceRoot":"","sources":["../src/findFileInPackage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"findFileInPackage.d.ts","sourceRoot":"","sources":["../src/findFileInPackage.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAO3E,MAAM,MAAM,wBAAwB,GAAG;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,EACjC,OAAO,EAAE;IAAE,QAAQ,EAAE,uBAAuB,CAAA;CAAE,GAC7C,OAAO,CAAC,uBAAuB,CAAC,CAoFlC"}
|
package/lib/findFileInPackage.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { intermediateToSourcePath, sourceToIntermediatePath,
|
|
3
|
-
import {
|
|
4
|
-
import { safeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import { intermediateToSourcePath, sourceToIntermediatePath, isFileSync } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import { safeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';
|
|
5
4
|
import { getExportsMap } from './getExportsMap.js';
|
|
6
5
|
import { flattenExportsMap } from './flattenExportsMap.js';
|
|
7
|
-
|
|
6
|
+
import { getCandidates } from './getCandidates.js';
|
|
7
|
+
import { isExternalPackage } from './isExternalPackage.js';
|
|
8
|
+
const sourceExtensions = ['.ts', '.tsx', '.cts', '.mts', '.jsx'];
|
|
9
|
+
const javascriptExtensions = ['.js', '.mjs', '.cjs'];
|
|
8
10
|
/**
|
|
9
11
|
* Given a requested partial filePath, resolves the relative intermediate path if the file exists, with special
|
|
10
12
|
* consideration for internal packages, which may not have intermediate files but only source files. Paths returned
|
|
@@ -16,48 +18,17 @@ const typescriptExtensions = ['.ts', '.tsx', '.cts', '.mts'];
|
|
|
16
18
|
* exist, then we should not include it in the exports map.
|
|
17
19
|
*/
|
|
18
20
|
export async function findFileInPackage(options, context) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
+
const originalPath = safeRelativePath(options.filePath);
|
|
22
|
+
const packagePath = slash(options.packagePath);
|
|
23
|
+
let filePath;
|
|
21
24
|
let sourcePath;
|
|
22
25
|
let typesPath;
|
|
23
26
|
const isInternal = !isExternalPackage(packagePath);
|
|
24
|
-
const candidates =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
safeRelativePath(path.join(filePath, 'index.js')),
|
|
30
|
-
safeRelativePath(path.join(filePath, 'package.json')),
|
|
31
|
-
];
|
|
32
|
-
filePath = undefined;
|
|
33
|
-
for (const candidate of candidates) {
|
|
34
|
-
if (await isFile(path.join(packagePath, candidate))) {
|
|
35
|
-
filePath = candidate;
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
// For internal packages, try and find source/typings and convert to intermediate (which may be missing.)
|
|
40
|
-
if (isInternal) {
|
|
41
|
-
for (const candidate of candidates) {
|
|
42
|
-
sourcePath = intermediateToSourcePath(candidate, packagePath);
|
|
43
|
-
if (sourcePath) {
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
// If the discovered source file is the same as the provided file, resolve the intermediate path.
|
|
48
|
-
if (sourcePath && (!filePath || sourcePath === filePath)) {
|
|
49
|
-
filePath = sourceToIntermediatePath(sourcePath);
|
|
50
|
-
}
|
|
51
|
-
// If the sourcePath is the same as the filePath, clear it. By this point we should have resolved
|
|
52
|
-
// the true intermediate path.
|
|
53
|
-
if (sourcePath === filePath) {
|
|
54
|
-
sourcePath = undefined;
|
|
55
|
-
}
|
|
56
|
-
if (sourcePath && typescriptExtensions.includes(path.extname(sourcePath))) {
|
|
57
|
-
typesPath = filePath && path.extname(filePath) === '.js' ? filePath.replace(/\.js$/, '.d.ts') : undefined;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const isNestedPackageDefinition = filePath && path.basename(filePath) === 'package.json' && path.basename(options.filePath) !== 'package.json';
|
|
27
|
+
const candidates = getCandidates({ filePath: originalPath, packagePath });
|
|
28
|
+
// Try and resolve a physical file given the candidates.
|
|
29
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
30
|
+
filePath = candidates.find((candidate) => isFileSync(path.join(packagePath, candidate)));
|
|
31
|
+
const isNestedPackageDefinition = filePath && path.basename(filePath) === 'package.json' && path.basename(originalPath) !== 'package.json';
|
|
61
32
|
const result = {};
|
|
62
33
|
if (isNestedPackageDefinition) {
|
|
63
34
|
const nestedDefinitionPath = path.dirname(path.join(packagePath, filePath));
|
|
@@ -74,6 +45,32 @@ export async function findFileInPackage(options, context) {
|
|
|
74
45
|
};
|
|
75
46
|
}
|
|
76
47
|
}
|
|
48
|
+
// If the resolved file is a typescript file in an internal package, try to resolve the intermediate.
|
|
49
|
+
if (filePath && sourceExtensions.includes(path.extname(filePath))) {
|
|
50
|
+
sourcePath = filePath;
|
|
51
|
+
filePath = isInternal ? sourceToIntermediatePath(filePath) : undefined;
|
|
52
|
+
}
|
|
53
|
+
// If we haven't resolved a sourcePath, try to resolve it from the intermediate.
|
|
54
|
+
if (isInternal && filePath && !sourcePath) {
|
|
55
|
+
const ext = path.extname(filePath);
|
|
56
|
+
const isJavaScriptExt = javascriptExtensions.includes(ext);
|
|
57
|
+
if (isJavaScriptExt) {
|
|
58
|
+
// Not a TS file, but a JS file. Try to find the source.
|
|
59
|
+
sourcePath = intermediateToSourcePath(filePath, packagePath);
|
|
60
|
+
// If we couldn't find a TS file, clear sourcePath.
|
|
61
|
+
if (sourcePath === filePath) {
|
|
62
|
+
sourcePath = undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Try to resolve the typesPath
|
|
67
|
+
if (sourcePath && filePath) {
|
|
68
|
+
typesPath = filePath.replace(/\.js$/, '.d.ts');
|
|
69
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
70
|
+
if (!isInternal && !isFileSync(path.join(packagePath, typesPath))) {
|
|
71
|
+
typesPath = undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
77
74
|
if (typesPath) {
|
|
78
75
|
result.typesPath = safeRelativePath(typesPath);
|
|
79
76
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findFileInPackage.js","sourceRoot":"","sources":["../src/findFileInPackage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,
|
|
1
|
+
{"version":3,"file":"findFileInPackage.js","sourceRoot":"","sources":["../src/findFileInPackage.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC9G,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;A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AG,oBAAoB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAE3D,IAAI,eAAe,EAAE;YACnB,wDAAwD;YACxD,UAAU,GAAG,wBAAwB,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;YAE7D,mDAAmD;YACnD,IAAI,UAAU,KAAK,QAAQ,EAAE;gBAC3B,UAAU,GAAG,SAAS,CAAC;aACxB;SACF;KACF;IAED,+BAA+B;IAC/B,IAAI,UAAU,IAAI,QAAQ,EAAE;QAC1B,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAE/C,qEAAqE;QACrE,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,EAAE;YACjE,SAAS,GAAG,SAAS,CAAC;SACvB;KACF;IAED,IAAI,SAAS,EAAE;QACb,MAAM,CAAC,SAAS,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;KAChD;IAED,IAAI,QAAQ,EAAE;QACZ,MAAM,CAAC,QAAQ,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC;KAC9C;IAED,IAAI,UAAU,EAAE;QACd,MAAM,CAAC,UAAU,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;KAClD;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import path from 'path';\nimport { intermediateToSourcePath, sourceToIntermediatePath, isFileSync } from '@ms-cloudpack/path-utilities';\nimport { safeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport { getExportsMap } from './getExportsMap.js';\nimport { flattenExportsMap } from './flattenExportsMap.js';\nimport type { PackageDefinitionsCache } from '@ms-cloudpack/bundler-types';\nimport { getCandidates } from './getCandidates.js';\nimport { isExternalPackage } from './isExternalPackage.js';\n\nconst sourceExtensions = ['.ts', '.tsx', '.cts', '.mts', '.jsx'];\nconst javascriptExtensions = ['.js', '.mjs', '.cjs'];\n\nexport type FindFileInPackageOptions = {\n packagePath: string;\n filePath: string;\n};\n\nexport type FindFileInPackageResult = {\n filePath?: string;\n typesPath?: string;\n sourcePath?: string;\n};\n\n/**\n * Given a requested partial filePath, resolves the relative intermediate path if the file exists, with special\n * consideration for internal packages, which may not have intermediate files but only source files. Paths returned\n * are always relative and start with `./`.\n *\n * This helper is used in deriving if the given package.json entries are actual validate candidates when constructing an\n * exports map from existing metadata. For example, a package.json may list `main` as `lib/index.js`. If this file\n * exists, or in internal packages, if `src/index.tsx` exists, then this is a valid candidate. If the file does not\n * exist, then we should not include it in the exports map.\n */\nexport async function findFileInPackage(\n options: FindFileInPackageOptions,\n context: { packages: PackageDefinitionsCache },\n): Promise<FindFileInPackageResult> {\n const originalPath = safeRelativePath(options.filePath);\n const packagePath = slash(options.packagePath);\n let filePath: string | undefined;\n let sourcePath: string | undefined;\n let typesPath: string | undefined;\n const isInternal = !isExternalPackage(packagePath);\n const candidates = getCandidates({ filePath: originalPath, packagePath });\n\n // Try and resolve a physical file given the candidates.\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n filePath = candidates.find((candidate) => isFileSync(path.join(packagePath, candidate)));\n\n const isNestedPackageDefinition =\n filePath && path.basename(filePath) === 'package.json' && path.basename(originalPath) !== 'package.json';\n const result: FindFileInPackageResult = {};\n\n if (isNestedPackageDefinition) {\n const nestedDefinitionPath = path.dirname(path.join(packagePath, filePath as string));\n const exportsMap = await getExportsMap({ packagePath: nestedDefinitionPath }, context);\n const entryPath = flattenExportsMap(exportsMap)['.'];\n\n if (entryPath) {\n typesPath = flattenExportsMap(exportsMap, { conditions: ['types'] })['.'];\n\n return {\n ...(await findFileInPackage(\n {\n packagePath,\n filePath: safeRelativePath(path.relative(packagePath, path.join(nestedDefinitionPath, entryPath))),\n },\n context,\n )),\n typesPath:\n typesPath && safeRelativePath(path.relative(packagePath, path.join(nestedDefinitionPath, typesPath))),\n };\n }\n }\n\n // If the resolved file is a typescript file in an internal package, try to resolve the intermediate.\n if (filePath && sourceExtensions.includes(path.extname(filePath))) {\n sourcePath = filePath;\n filePath = isInternal ? sourceToIntermediatePath(filePath) : undefined;\n }\n\n // If we haven't resolved a sourcePath, try to resolve it from the intermediate.\n if (isInternal && filePath && !sourcePath) {\n const ext = path.extname(filePath);\n const isJavaScriptExt = javascriptExtensions.includes(ext);\n\n if (isJavaScriptExt) {\n // Not a TS file, but a JS file. Try to find the source.\n sourcePath = intermediateToSourcePath(filePath, packagePath);\n\n // If we couldn't find a TS file, clear sourcePath.\n if (sourcePath === filePath) {\n sourcePath = undefined;\n }\n }\n }\n\n // Try to resolve the typesPath\n if (sourcePath && filePath) {\n typesPath = filePath.replace(/\\.js$/, '.d.ts');\n\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n if (!isInternal && !isFileSync(path.join(packagePath, typesPath))) {\n typesPath = undefined;\n }\n }\n\n if (typesPath) {\n result.typesPath = safeRelativePath(typesPath);\n }\n\n if (filePath) {\n result.filePath = safeRelativePath(filePath);\n }\n\n if (sourcePath) {\n result.sourcePath = safeRelativePath(sourcePath);\n }\n\n return result;\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a packagePath and a filePath from its exports map (or main/module), build candidate paths
|
|
3
|
+
* to look for the physical file in order of priority. For internal packages, we try to discover
|
|
4
|
+
* source files first, and then fallback to the original file name. We also search for index files
|
|
5
|
+
* and expand folder paths as well for ambiguous references. For external packages, we prioritize
|
|
6
|
+
* intermediate paths.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getCandidates(options: {
|
|
9
|
+
filePath: string;
|
|
10
|
+
packagePath: string;
|
|
11
|
+
}): string[];
|
|
12
|
+
//# sourceMappingURL=getCandidates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCandidates.d.ts","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AA0BA;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,YAoD/E"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { safeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { isExternalPackage } from './isExternalPackage.js';
|
|
4
|
+
// Mappings to possible candidate extensions for a given input extension.
|
|
5
|
+
const externalExtensions = ['.js', '.mjs', '.cjs'];
|
|
6
|
+
const internalExtensions = ['.ts', '.tsx', '.mts', '.cts', '.jsx', '.js', '.mjs', '.cjs'];
|
|
7
|
+
const internalExtensionMap = {
|
|
8
|
+
'.ts': ['.ts', '.js'],
|
|
9
|
+
'.tsx': ['.tsx', '.js'],
|
|
10
|
+
'.mts': ['.mts', '.mjs', '.js'],
|
|
11
|
+
'.cts': ['.cts', '.cjs', '.js'],
|
|
12
|
+
'.jsx': ['.jsx', '.js'],
|
|
13
|
+
'.js': ['.ts', '.tsx', '.mts', '.cts', '.jsx', '.js'],
|
|
14
|
+
'.mjs': ['.mjs', '.js'],
|
|
15
|
+
'.cjs': ['.cjs', '.js'],
|
|
16
|
+
};
|
|
17
|
+
const externalExtensionMap = {
|
|
18
|
+
'.ts': ['.ts', '.js'],
|
|
19
|
+
'.tsx': ['.js'],
|
|
20
|
+
'.mts': ['.mjs', '.js'],
|
|
21
|
+
'.cts': ['.mts', '.cts'],
|
|
22
|
+
'.jsx': ['.js'],
|
|
23
|
+
'.js': ['.js'],
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Given a packagePath and a filePath from its exports map (or main/module), build candidate paths
|
|
27
|
+
* to look for the physical file in order of priority. For internal packages, we try to discover
|
|
28
|
+
* source files first, and then fallback to the original file name. We also search for index files
|
|
29
|
+
* and expand folder paths as well for ambiguous references. For external packages, we prioritize
|
|
30
|
+
* intermediate paths.
|
|
31
|
+
*/
|
|
32
|
+
export function getCandidates(options) {
|
|
33
|
+
const { filePath, packagePath } = options;
|
|
34
|
+
const isBlankPath = !filePath || filePath === '.';
|
|
35
|
+
const originalPath = isBlankPath ? './index' : safeRelativePath(filePath);
|
|
36
|
+
const originalExt = path.extname(originalPath);
|
|
37
|
+
const isInternal = !isExternalPackage(packagePath);
|
|
38
|
+
const endsWithSlash = originalPath.endsWith('/');
|
|
39
|
+
const candidates = [];
|
|
40
|
+
const defaultExtensions = isInternal ? internalExtensions : externalExtensions;
|
|
41
|
+
const extensions = (isInternal ? internalExtensionMap : externalExtensionMap)[originalExt] || defaultExtensions;
|
|
42
|
+
const pathsToAdd = [originalPath];
|
|
43
|
+
if (isInternal && originalPath.startsWith('./lib')) {
|
|
44
|
+
pathsToAdd.unshift(originalPath.replace(/^\.\/lib/, './src'));
|
|
45
|
+
}
|
|
46
|
+
for (let currentPath of pathsToAdd) {
|
|
47
|
+
const unrecognizedExt = originalExt && !extensions.includes(path.extname(currentPath));
|
|
48
|
+
// For internal packages, we try and resolve the original file name first.
|
|
49
|
+
if (isInternal && unrecognizedExt) {
|
|
50
|
+
candidates.push(currentPath);
|
|
51
|
+
}
|
|
52
|
+
// If the path doesn't end with a slash, it could be a file. Add extensions.
|
|
53
|
+
if (!endsWithSlash) {
|
|
54
|
+
candidates.push(...expandExtensions(currentPath, extensions));
|
|
55
|
+
}
|
|
56
|
+
// For non-extension references, we might be referring to a folder, or a barrel file within a folder. Add the
|
|
57
|
+
// appropriate extensions for the barrel file, and fall back to a package.json in the case of nested definitions.
|
|
58
|
+
if (!originalExt) {
|
|
59
|
+
if (isBlankPath) {
|
|
60
|
+
candidates.push('./package.json');
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// Trim trailing slash
|
|
64
|
+
if (endsWithSlash) {
|
|
65
|
+
currentPath = currentPath.slice(0, -1);
|
|
66
|
+
}
|
|
67
|
+
candidates.push(...expandExtensions(currentPath + '/index', extensions));
|
|
68
|
+
candidates.push(currentPath + '/package.json');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// For external packages, we try and resolve the original file name last.
|
|
72
|
+
if (!isInternal && unrecognizedExt) {
|
|
73
|
+
candidates.push(currentPath);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return candidates;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Given a filepath and list of extensions, maps extensions to prepend the filepath. In the case that
|
|
80
|
+
* the filepath has an extension already in the list, avoids appending the extension twice.
|
|
81
|
+
*
|
|
82
|
+
* For example, filePath = './foo/bar.js' with extensions ['.ts', '.js'] would result in:
|
|
83
|
+
*
|
|
84
|
+
* ['./foo/bar.ts.js', './foo/bar.js'].
|
|
85
|
+
*
|
|
86
|
+
*/
|
|
87
|
+
function expandExtensions(filePath, extensions) {
|
|
88
|
+
const result = [];
|
|
89
|
+
const ext = path.extname(filePath);
|
|
90
|
+
if (extensions.includes(ext)) {
|
|
91
|
+
filePath = `${filePath.slice(0, -ext.length)}`;
|
|
92
|
+
}
|
|
93
|
+
for (const currentExt of extensions) {
|
|
94
|
+
result.push(filePath + currentExt);
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=getCandidates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCandidates.js","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,yEAAyE;AACzE,MAAM,kBAAkB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACnD,MAAM,kBAAkB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAC1F,MAAM,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C,WAAW,GAAG,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC;gBACzE,UAAU,CAAC,IAAI,CAAC,WAAW,GAAG,eAAe,CAAC,CAAC;aAChD;SACF;QAED,yEAAyE;QACzE,IAAI,CAAC,UAAU,IAAI,eAAe,EAAE;YAClC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;SAC9B;KACF;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,gBAAgB,CAAC,QAAgB,EAAE,UAAoB;IAC9D,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEnC,IAAI,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QAC5B,QAAQ,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;KAChD;IAED,KAAK,MAAM,UAAU,IAAI,UAAU,EAAE;QACnC,MAAM,CAAC,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC;KACpC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { safeRelativePath } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport { isExternalPackage } from './isExternalPackage.js';\n\n// Mappings to possible candidate extensions for a given input extension.\nconst externalExtensions = ['.js', '.mjs', '.cjs'];\nconst internalExtensions = ['.ts', '.tsx', '.mts', '.cts', '.jsx', '.js', '.mjs', '.cjs'];\nconst internalExtensionMap: Record<string, string[]> = {\n '.ts': ['.ts', '.js'],\n '.tsx': ['.tsx', '.js'],\n '.mts': ['.mts', '.mjs', '.js'],\n '.cts': ['.cts', '.cjs', '.js'],\n '.jsx': ['.jsx', '.js'],\n '.js': ['.ts', '.tsx', '.mts', '.cts', '.jsx', '.js'],\n '.mjs': ['.mjs', '.js'],\n '.cjs': ['.cjs', '.js'],\n};\nconst externalExtensionMap: Record<string, string[]> = {\n '.ts': ['.ts', '.js'],\n '.tsx': ['.js'],\n '.mts': ['.mjs', '.js'],\n '.cts': ['.mts', '.cts'],\n '.jsx': ['.js'],\n '.js': ['.js'],\n};\n\n/**\n * Given a packagePath and a filePath from its exports map (or main/module), build candidate paths\n * to look for the physical file in order of priority. For internal packages, we try to discover\n * source files first, and then fallback to the original file name. We also search for index files\n * and expand folder paths as well for ambiguous references. For external packages, we prioritize\n * intermediate paths.\n */\nexport function getCandidates(options: { filePath: string; packagePath: string }) {\n const { filePath, packagePath } = options;\n const isBlankPath = !filePath || filePath === '.';\n const originalPath = isBlankPath ? './index' : safeRelativePath(filePath);\n const originalExt = path.extname(originalPath);\n const isInternal = !isExternalPackage(packagePath);\n const endsWithSlash = originalPath.endsWith('/');\n const candidates = [];\n const defaultExtensions = isInternal ? internalExtensions : externalExtensions;\n const extensions = (isInternal ? internalExtensionMap : externalExtensionMap)[originalExt] || defaultExtensions;\n const pathsToAdd = [originalPath];\n\n if (isInternal && originalPath.startsWith('./lib')) {\n pathsToAdd.unshift(originalPath.replace(/^\\.\\/lib/, './src'));\n }\n\n for (let currentPath of pathsToAdd) {\n const unrecognizedExt = originalExt && !extensions.includes(path.extname(currentPath));\n\n // For internal packages, we try and resolve the original file name first.\n if (isInternal && unrecognizedExt) {\n candidates.push(currentPath);\n }\n\n // If the path doesn't end with a slash, it could be a file. Add extensions.\n if (!endsWithSlash) {\n candidates.push(...expandExtensions(currentPath, extensions));\n }\n\n // For non-extension references, we might be referring to a folder, or a barrel file within a folder. Add the\n // appropriate extensions for the barrel file, and fall back to a package.json in the case of nested definitions.\n if (!originalExt) {\n if (isBlankPath) {\n candidates.push('./package.json');\n } else {\n // Trim trailing slash\n if (endsWithSlash) {\n currentPath = currentPath.slice(0, -1);\n }\n\n candidates.push(...expandExtensions(currentPath + '/index', extensions));\n candidates.push(currentPath + '/package.json');\n }\n }\n\n // For external packages, we try and resolve the original file name last.\n if (!isInternal && unrecognizedExt) {\n candidates.push(currentPath);\n }\n }\n\n return candidates;\n}\n\n/**\n * Given a filepath and list of extensions, maps extensions to prepend the filepath. In the case that\n * the filepath has an extension already in the list, avoids appending the extension twice.\n *\n * For example, filePath = './foo/bar.js' with extensions ['.ts', '.js'] would result in:\n *\n * ['./foo/bar.ts.js', './foo/bar.js'].\n *\n */\nfunction expandExtensions(filePath: string, extensions: string[]) {\n const result: string[] = [];\n const ext = path.extname(filePath);\n\n if (extensions.includes(ext)) {\n filePath = `${filePath.slice(0, -ext.length)}`;\n }\n\n for (const currentExt of extensions) {\n result.push(filePath + currentExt);\n }\n\n return result;\n}\n"]}
|