@ms-cloudpack/package-utilities 7.1.7 → 7.2.1
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/createExportsMap.d.ts.map +1 -1
- package/lib/createExportsMap.js +23 -13
- package/lib/createExportsMap.js.map +1 -1
- package/lib/createResolveMap/findPackagesFromPath.d.ts.map +1 -1
- package/lib/createResolveMap/findPackagesFromPath.js +6 -1
- package/lib/createResolveMap/findPackagesFromPath.js.map +1 -1
- package/lib/findFileInPackage.js +1 -1
- package/lib/findFileInPackage.js.map +1 -1
- package/lib/findPackageOutputPaths.d.ts +21 -0
- package/lib/findPackageOutputPaths.d.ts.map +1 -0
- package/lib/findPackageOutputPaths.js +84 -0
- package/lib/findPackageOutputPaths.js.map +1 -0
- package/lib/getCandidates.d.ts +3 -2
- package/lib/getCandidates.d.ts.map +1 -1
- package/lib/getCandidates.js +32 -15
- package/lib/getCandidates.js.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createExportsMap.d.ts","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAI/G;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE;IACP,WAAW,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,EACD,OAAO,EAAE;IAAE,QAAQ,EAAE,uBAAuB,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,GACtE,OAAO,CAAC,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"createExportsMap.d.ts","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAI/G;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE;IACP,WAAW,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,EACD,OAAO,EAAE;IAAE,QAAQ,EAAE,uBAAuB,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,GACtE,OAAO,CAAC,kBAAkB,CAAC,CAqG7B"}
|
package/lib/createExportsMap.js
CHANGED
|
@@ -17,24 +17,34 @@ export async function createExportsMap(options, context) {
|
|
|
17
17
|
const { browser, main, module, types, typings, type } = definition;
|
|
18
18
|
const exports = {};
|
|
19
19
|
const typesPath = types || typings;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
// Only try to add `index.js` as an entry if main/module/browser isn't available.
|
|
21
|
+
if (!(main || module || browser)) {
|
|
22
|
+
await addExportsMapEntry({
|
|
23
|
+
exports,
|
|
24
|
+
packagePath,
|
|
25
|
+
filePath: 'index.js',
|
|
26
|
+
typesPath,
|
|
27
|
+
condition: type === 'module' ? 'import' : 'default',
|
|
28
|
+
}, context);
|
|
29
|
+
}
|
|
30
|
+
if (main) {
|
|
31
|
+
await addExportsMapEntry({
|
|
29
32
|
exports,
|
|
30
33
|
packagePath,
|
|
31
34
|
filePath: main,
|
|
32
35
|
typesPath,
|
|
33
36
|
condition: type === 'module' ? 'import' : 'default',
|
|
34
|
-
}, context)
|
|
35
|
-
|
|
36
|
-
module
|
|
37
|
-
|
|
37
|
+
}, context);
|
|
38
|
+
}
|
|
39
|
+
if (module) {
|
|
40
|
+
await addExportsMapEntry({
|
|
41
|
+
exports,
|
|
42
|
+
packagePath,
|
|
43
|
+
filePath: module,
|
|
44
|
+
typesPath,
|
|
45
|
+
condition: 'import',
|
|
46
|
+
}, context);
|
|
47
|
+
}
|
|
38
48
|
if (browser) {
|
|
39
49
|
if (typeof definition.browser === 'string') {
|
|
40
50
|
await addExportsMapEntry({ exports, packagePath, filePath: definition.browser, condition: 'browser' }, context);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createExportsMap.js","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,OAGC,EACD,OAAuE;IAEvE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAChC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,UAAU,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAErG,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,oCAAoC,WAAW,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,qEAAqE;IACrE,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;QACvB,OAAO,UAAU,CAAC,OAAO,CAAC;IAC5B,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC;IACnE,MAAM,OAAO,GAAG,EAAE,CAAC;IACnB,MAAM,SAAS,GAAG,KAAK,IAAI,OAAO,CAAC;IAEnC,MAAM,kBAAkB,CACtB;
|
|
1
|
+
{"version":3,"file":"createExportsMap.js","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;GAEG;AACH,MAAM,CAA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S;qBACrB,EACD,OAAO,CACR,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,qEAAqE;IACrE,+CAA+C;IAC/C,mEAAmE;IACnE,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtC,CAAC,MAAM,kBAAkB,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAE,EAAE,OAAO,CAAC,CAAC;YACvF,CAAC,MAAM,kBAAkB,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,EAAE,EAAE,OAAO,CAAC,CAAC;YACxF,CAAC,MAAM,kBAAkB,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;IAC1F,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { CloudpackConfig, PackageDefinitionsCache, PackageJsonExports } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing';\nimport { addExportsMapEntry } from './addExportsMapEntry.js';\n\n/**\n * Given a package path, generates an export map for the package.\n */\nexport async function createExportsMap(\n options: {\n packagePath: string;\n disableTransforms?: boolean;\n },\n context: { packages: PackageDefinitionsCache; config: CloudpackConfig },\n): Promise<PackageJsonExports> {\n const { packagePath } = options;\n const { packages } = context;\n const definition = await packages.get(packagePath, { disableTransforms: options.disableTransforms });\n\n if (!definition) {\n throw new Error(`Package definition not found for ${packagePath}`);\n }\n\n // Don't create an exports map for a definition that already has one.\n if (definition.exports) {\n return definition.exports;\n }\n\n const { browser, main, module, types, typings, type } = definition;\n const exports = {};\n const typesPath = types || typings;\n\n // Only try to add `index.js` as an entry if main/module/browser isn't available.\n if (!(main || module || browser)) {\n await addExportsMapEntry(\n {\n exports,\n packagePath,\n filePath: 'index.js',\n typesPath,\n condition: type === 'module' ? 'import' : 'default',\n },\n context,\n );\n }\n\n if (main) {\n await addExportsMapEntry(\n {\n exports,\n packagePath,\n filePath: main,\n typesPath,\n condition: type === 'module' ? 'import' : 'default',\n },\n context,\n );\n }\n\n if (module) {\n await addExportsMapEntry(\n {\n exports,\n packagePath,\n filePath: module,\n typesPath,\n condition: 'import',\n },\n context,\n );\n }\n\n if (browser) {\n if (typeof definition.browser === 'string') {\n await addExportsMapEntry({ exports, packagePath, filePath: definition.browser, condition: 'browser' }, context);\n } else if (typeof definition.browser === 'object') {\n for (const [key, value] of Object.entries(definition.browser)) {\n if (!value || typeof value !== 'string') {\n continue;\n }\n\n const isDefaultImport = normalizeRelativePath(module || main) === normalizeRelativePath(key);\n\n const importPaths = isDefaultImport ? ['.'] : [key];\n\n if (!isDefaultImport && key.endsWith('.js')) {\n importPaths.push(key.replace(/\\.js$/, ''));\n }\n\n for (const importPath of importPaths) {\n await addExportsMapEntry(\n {\n exports,\n packagePath,\n importPath,\n filePath: value,\n condition: 'browser',\n },\n context,\n );\n }\n }\n }\n }\n\n // If we couldn't infer the exports map shape from existing metadata,\n // formulate based off inferred file structure.\n // Note: CRA defaults to src/index.js, Vite defaults to src/main.js\n if (Object.keys(exports).length === 0) {\n (await addExportsMapEntry({ exports, packagePath, filePath: './lib/index.js' }, context)) ||\n (await addExportsMapEntry({ exports, packagePath, filePath: './lib/main.js' }, context)) ||\n (await addExportsMapEntry({ exports, packagePath, filePath: './index.js' }, context));\n }\n\n return exports;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findPackagesFromPath.d.ts","sourceRoot":"","sources":["../../src/createResolveMap/findPackagesFromPath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAuC,MAAM,4BAA4B,CAAC;AAM/G,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAOzD;;;GAGG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE;IACP;;OAEG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B,EACD,OAAO,EAAE;IACP;;OAEG;IACH,QAAQ,EAAE,uBAAuB,CAAC;CACnC,GACA,OAAO,CAAC,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"findPackagesFromPath.d.ts","sourceRoot":"","sources":["../../src/createResolveMap/findPackagesFromPath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAuC,MAAM,4BAA4B,CAAC;AAM/G,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAOzD;;;GAGG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE;IACP;;OAEG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B,EACD,OAAO,EAAE;IACP;;OAEG;IACH,QAAQ,EAAE,uBAAuB,CAAC;CACnC,GACA,OAAO,CAAC,UAAU,CAAC,CAiDrB"}
|
|
@@ -39,7 +39,12 @@ export async function findPackagesFromPath(options, context) {
|
|
|
39
39
|
if (!visitedPaths.has(packagePath)) {
|
|
40
40
|
visitedPaths.add(packagePath);
|
|
41
41
|
const dependencyPaths = await visitPackage({ packagePath, packageMap, rootPath, importerContext }, context);
|
|
42
|
-
|
|
42
|
+
for (const dependencyPath of dependencyPaths) {
|
|
43
|
+
// Let's avoid pushing tons of things into the work queue that we'll be skipping later.
|
|
44
|
+
if (!visitedPaths.has(dependencyPath.packagePath)) {
|
|
45
|
+
pathsToVisit.push(dependencyPath);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
}
|
|
@@ -1 +1 @@
|
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AAE,CAAC;YACnC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { PackageDefinitionsCache, PackageImporterContext, PackageJson } from '@ms-cloudpack/common-types';\nimport { isFolder } from '@ms-cloudpack/path-utilities';\nimport glob from 'fast-glob';\nimport path from 'path';\nimport { findPackage } from '../findPackage.js';\nimport { isExternalPackage } from '../isExternalPackage.js';\nimport type { PackageMap } from '../types/PackageMap.js';\nimport type { ResolveMapEntry } from '../types/ResolveMapEntry.js';\nimport { getDependencies } from '../getDependencies.js';\nimport { findProjectRoot } from '../findProjectRoot.js';\n\ntype PackagePathInfo = { packagePath: string; importerContext?: PackageImporterContext };\n\n/**\n * Find all packages from a given path. We do this by walking the dependency tree\n * of the app to build a map of all packages and their dependencies.\n */\nexport async function findPackagesFromPath(\n options: {\n /**\n * Paths to search for package.json files and traverse their dependencies to build a PackageMap.\n */\n searchPaths: string[];\n\n /**\n * Where to stop looking for packages (mainly used for testing).\n * If not specified, will look for the project/git root of the first path in the paths array.\n */\n rootPath?: string;\n\n /**\n * If true will glob for all package.json files in the path and add them to the map.\n */\n discoverPackages?: boolean;\n },\n context: {\n /**\n * Package cache.\n */\n packages: PackageDefinitionsCache;\n },\n): Promise<PackageMap> {\n const { searchPaths, discoverPackages } = options;\n const packageMap: PackageMap = {};\n const visitedPaths = new Set<string>();\n\n for (const searchPath of searchPaths) {\n if (!(await isFolder(searchPath))) {\n throw new Error(`Path ${searchPath} is not a folder.\\n\\nMake sure the path exists and try again.`);\n }\n\n const rootPath = options.rootPath || findProjectRoot(searchPath, { noPackageRoot: true });\n\n let pathsToVisit: PackagePathInfo[];\n\n if (discoverPackages) {\n const packagePaths = await glob('**/package.json', { cwd: searchPath, ignore: ['**/node_modules/**'] });\n\n pathsToVisit = packagePaths.map((p) => ({ packagePath: path.dirname(path.join(searchPath, p)) }));\n } else {\n pathsToVisit = [{ packagePath: searchPath }];\n }\n\n while (pathsToVisit.length) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- already checked length\n const { packagePath, importerContext } = pathsToVisit.shift()!;\n\n // There's a very slight possibility (not yet observed even in large repos) that both semver\n // and non-semver dependencies could resolve to the same package name and version and therefore\n // be installed at the same path. (If the non-semver dependency was encountered first, the\n // version and possibly name in the package definition would have been transformed to match by\n // getNonSemverTransform.) It would be possible to detect this case if needed, but for now we're\n // not going to bother because it's so rare/theoretical and the effect in practice will probably\n // be the same as a dependency that's forced with `resolutions` to a version violating semver.\n if (!visitedPaths.has(packagePath)) {\n visitedPaths.add(packagePath);\n\n const dependencyPaths = await visitPackage({ packagePath, packageMap, rootPath, importerContext }, context);\n pathsToVisit.push(...dependencyPaths);\n }\n }\n }\n\n return packageMap;\n}\n\n/**\n * Visit a package path, and add entries for the package and its dependencies to `packageMap`.\n * Returns a list of additional dependency paths to visit.\n */\nasync function visitPackage(\n options: PackagePathInfo & {\n packageMap: PackageMap;\n rootPath: string | undefined;\n },\n context: Parameters<typeof findPackagesFromPath>[1],\n) {\n const { packagePath, packageMap, rootPath, importerContext } = options;\n const { packages } = context;\n\n const packageDefinition = await packages.get(packagePath, { importerContext });\n if (!packageDefinition) {\n throw new Error(\n `Could not find package definition at \"${packagePath}\".\\n\\n` +\n `Make sure the package.json file exists and try again.\\n`,\n );\n }\n if (!(packageDefinition.name && packageDefinition.version)) {\n return [];\n }\n\n const { name, version } = packageDefinition;\n const entry = ensurePackageEntry({\n packageName: importerContext?.importedName || name,\n packagePath: packagePath,\n packageMap,\n definition: packageDefinition,\n });\n\n // Add all dependencies to the list of paths to visit.\n const dependencies = getDependencies(packageDefinition) as Record<string, string>;\n\n const dependencyPaths: PackagePathInfo[] = [];\n\n for (const [dependencyName, versionRequirement] of Object.entries(dependencies)) {\n const depImporterContext: PackageImporterContext = {\n parentName: name,\n parentVersion: version,\n importedName: dependencyName,\n versionRequirement,\n };\n\n const dependencyPackage = await findPackage(\n { dependencyName, startPath: packagePath, rootPath, importerContext: depImporterContext },\n context,\n );\n if (!dependencyPackage) {\n if (packageDefinition.peerDependencies?.[dependencyName]) {\n // Ignore missing peer dependencies\n continue;\n }\n\n throw new Error(\n `A dependency could not be found:\\n` +\n ` * Dependency: ${dependencyName} (Required: ${versionRequirement})\\n` +\n ` * Required by: ${packagePath}\\n\\n` +\n `Make sure the dependency is installed and try again.`,\n );\n }\n\n const { packagePath: dependencyPath, definition: dependencyDefinition } = dependencyPackage;\n\n if (!(dependencyDefinition.name && dependencyDefinition.version)) {\n throw new Error(`\"${dependencyPath}/package.json\" is missing a name or version.`);\n }\n\n const dependencyEntry = ensurePackageEntry({\n packagePath: dependencyPath,\n packageName: dependencyName,\n packageMap,\n definition: dependencyDefinition,\n });\n\n entry.dependencies[dependencyName] = dependencyDefinition.version;\n dependencyEntry.requiredBy[`${name}@${version}`] = versionRequirement;\n\n dependencyPaths.push({\n packagePath: dependencyPath,\n importerContext: depImporterContext,\n });\n }\n\n return dependencyPaths;\n}\n\n/**\n * If `packageMap` doesn't already have an entry for this package name and version, create one\n * and add it to `packageMap`. Returns the entry.\n */\nfunction ensurePackageEntry(options: {\n packagePath: string;\n packageName: string;\n packageMap: PackageMap;\n definition: PackageJson;\n}): ResolveMapEntry {\n const { packagePath = '', packageName: name, packageMap, definition } = options;\n // the caller verified that these are set\n const { version } = definition as Required<PackageJson>;\n\n packageMap[name] ??= {};\n\n let entry = packageMap[name][version];\n if (!entry) {\n entry = packageMap[name][version] = {\n name,\n version,\n path: packagePath,\n dependencies: {},\n requiredBy: {},\n };\n\n if (isExternalPackage(packagePath)) {\n entry.isExternal = true;\n }\n }\n\n return entry;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"findPackagesFromPath.js","sourceRoot":"","sources":["../../src/createResolveMap/findPackagesFromPath.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,O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UAAU,GAAG,IAAI,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { PackageDefinitionsCache, PackageImporterContext, PackageJson } from '@ms-cloudpack/common-types';\nimport { isFolder } from '@ms-cloudpack/path-utilities';\nimport glob from 'fast-glob';\nimport path from 'path';\nimport { findPackage } from '../findPackage.js';\nimport { isExternalPackage } from '../isExternalPackage.js';\nimport type { PackageMap } from '../types/PackageMap.js';\nimport type { ResolveMapEntry } from '../types/ResolveMapEntry.js';\nimport { getDependencies } from '../getDependencies.js';\nimport { findProjectRoot } from '../findProjectRoot.js';\n\ntype PackagePathInfo = { packagePath: string; importerContext?: PackageImporterContext };\n\n/**\n * Find all packages from a given path. We do this by walking the dependency tree\n * of the app to build a map of all packages and their dependencies.\n */\nexport async function findPackagesFromPath(\n options: {\n /**\n * Paths to search for package.json files and traverse their dependencies to build a PackageMap.\n */\n searchPaths: string[];\n\n /**\n * Where to stop looking for packages (mainly used for testing).\n * If not specified, will look for the project/git root of the first path in the paths array.\n */\n rootPath?: string;\n\n /**\n * If true will glob for all package.json files in the path and add them to the map.\n */\n discoverPackages?: boolean;\n },\n context: {\n /**\n * Package cache.\n */\n packages: PackageDefinitionsCache;\n },\n): Promise<PackageMap> {\n const { searchPaths, discoverPackages } = options;\n const packageMap: PackageMap = {};\n const visitedPaths = new Set<string>();\n\n for (const searchPath of searchPaths) {\n if (!(await isFolder(searchPath))) {\n throw new Error(`Path ${searchPath} is not a folder.\\n\\nMake sure the path exists and try again.`);\n }\n\n const rootPath = options.rootPath || findProjectRoot(searchPath, { noPackageRoot: true });\n\n let pathsToVisit: PackagePathInfo[];\n\n if (discoverPackages) {\n const packagePaths = await glob('**/package.json', { cwd: searchPath, ignore: ['**/node_modules/**'] });\n\n pathsToVisit = packagePaths.map((p) => ({ packagePath: path.dirname(path.join(searchPath, p)) }));\n } else {\n pathsToVisit = [{ packagePath: searchPath }];\n }\n\n while (pathsToVisit.length) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- already checked length\n const { packagePath, importerContext } = pathsToVisit.shift()!;\n\n // There's a very slight possibility (not yet observed even in large repos) that both semver\n // and non-semver dependencies could resolve to the same package name and version and therefore\n // be installed at the same path. (If the non-semver dependency was encountered first, the\n // version and possibly name in the package definition would have been transformed to match by\n // getNonSemverTransform.) It would be possible to detect this case if needed, but for now we're\n // not going to bother because it's so rare/theoretical and the effect in practice will probably\n // be the same as a dependency that's forced with `resolutions` to a version violating semver.\n if (!visitedPaths.has(packagePath)) {\n visitedPaths.add(packagePath);\n\n const dependencyPaths = await visitPackage({ packagePath, packageMap, rootPath, importerContext }, context);\n\n for (const dependencyPath of dependencyPaths) {\n // Let's avoid pushing tons of things into the work queue that we'll be skipping later.\n if (!visitedPaths.has(dependencyPath.packagePath)) {\n pathsToVisit.push(dependencyPath);\n }\n }\n }\n }\n }\n\n return packageMap;\n}\n\n/**\n * Visit a package path, and add entries for the package and its dependencies to `packageMap`.\n * Returns a list of additional dependency paths to visit.\n */\nasync function visitPackage(\n options: PackagePathInfo & {\n packageMap: PackageMap;\n rootPath: string | undefined;\n },\n context: Parameters<typeof findPackagesFromPath>[1],\n) {\n const { packagePath, packageMap, rootPath, importerContext } = options;\n const { packages } = context;\n\n const packageDefinition = await packages.get(packagePath, { importerContext });\n if (!packageDefinition) {\n throw new Error(\n `Could not find package definition at \"${packagePath}\".\\n\\n` +\n `Make sure the package.json file exists and try again.\\n`,\n );\n }\n if (!(packageDefinition.name && packageDefinition.version)) {\n return [];\n }\n\n const { name, version } = packageDefinition;\n const entry = ensurePackageEntry({\n packageName: importerContext?.importedName || name,\n packagePath: packagePath,\n packageMap,\n definition: packageDefinition,\n });\n\n // Add all dependencies to the list of paths to visit.\n const dependencies = getDependencies(packageDefinition) as Record<string, string>;\n\n const dependencyPaths: PackagePathInfo[] = [];\n\n for (const [dependencyName, versionRequirement] of Object.entries(dependencies)) {\n const depImporterContext: PackageImporterContext = {\n parentName: name,\n parentVersion: version,\n importedName: dependencyName,\n versionRequirement,\n };\n\n const dependencyPackage = await findPackage(\n { dependencyName, startPath: packagePath, rootPath, importerContext: depImporterContext },\n context,\n );\n if (!dependencyPackage) {\n if (packageDefinition.peerDependencies?.[dependencyName]) {\n // Ignore missing peer dependencies\n continue;\n }\n\n throw new Error(\n `A dependency could not be found:\\n` +\n ` * Dependency: ${dependencyName} (Required: ${versionRequirement})\\n` +\n ` * Required by: ${packagePath}\\n\\n` +\n `Make sure the dependency is installed and try again.`,\n );\n }\n\n const { packagePath: dependencyPath, definition: dependencyDefinition } = dependencyPackage;\n\n if (!(dependencyDefinition.name && dependencyDefinition.version)) {\n throw new Error(`\"${dependencyPath}/package.json\" is missing a name or version.`);\n }\n\n const dependencyEntry = ensurePackageEntry({\n packagePath: dependencyPath,\n packageName: dependencyName,\n packageMap,\n definition: dependencyDefinition,\n });\n\n entry.dependencies[dependencyName] = dependencyDefinition.version;\n dependencyEntry.requiredBy[`${name}@${version}`] = versionRequirement;\n\n dependencyPaths.push({\n packagePath: dependencyPath,\n importerContext: depImporterContext,\n });\n }\n\n return dependencyPaths;\n}\n\n/**\n * If `packageMap` doesn't already have an entry for this package name and version, create one\n * and add it to `packageMap`. Returns the entry.\n */\nfunction ensurePackageEntry(options: {\n packagePath: string;\n packageName: string;\n packageMap: PackageMap;\n definition: PackageJson;\n}): ResolveMapEntry {\n const { packagePath = '', packageName: name, packageMap, definition } = options;\n // the caller verified that these are set\n const { version } = definition as Required<PackageJson>;\n\n packageMap[name] ??= {};\n\n let entry = packageMap[name][version];\n if (!entry) {\n entry = packageMap[name][version] = {\n name,\n version,\n path: packagePath,\n dependencies: {},\n requiredBy: {},\n };\n\n if (isExternalPackage(packagePath)) {\n entry.isExternal = true;\n }\n }\n\n return entry;\n}\n"]}
|
package/lib/findFileInPackage.js
CHANGED
|
@@ -24,7 +24,7 @@ export async function findFileInPackage(options, context) {
|
|
|
24
24
|
let sourcePath;
|
|
25
25
|
let typesPath;
|
|
26
26
|
const isInternal = !isExternalPackage(packagePath);
|
|
27
|
-
const candidates = getCandidates({ filePath: originalPath, packagePath }, context);
|
|
27
|
+
const candidates = await getCandidates({ filePath: originalPath, packagePath }, context);
|
|
28
28
|
// Try and resolve a physical file given the candidates.
|
|
29
29
|
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
30
30
|
filePath = candidates.find((candidate) => isFileSync(path.join(packagePath, candidate)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findFileInPackage.js","sourceRoot":"","sources":["../src/findFileInPackage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,wBAAwB,EAAE,UAAU,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC9G,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACjE,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAarD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAiC,EACjC,OAAuE;IAEvE,MAAM,YAAY,GAAG,qBAAqB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7D,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAC/C,IAAI,QAA4B,CAAC;IACjC,IAAI,UAA8B,CAAC;IACnC,IAAI,SAA6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AC;QACb,MAAM,CAAC,QAAQ,GAAG,qBAAqB,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IAED,IAAI,UAAU,EAAE,CAAC;QACf,MAAM,CAAC,UAAU,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IACxD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { CloudpackConfig, PackageDefinitionsCache } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport { intermediateToSourcePath, isFileSync, sourceToIntermediatePath } from '@ms-cloudpack/path-utilities';\nimport path from 'path';\nimport { flattenExportsMap } from './flattenExportsMap.js';\nimport { getCandidates } from './getCandidates.js';\nimport { getExportsMap } from './getExportsMap.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; config: CloudpackConfig },\n): Promise<FindFileInPackageResult> {\n const originalPath = normalizeRelativePath(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 = await getCandidates({ filePath: originalPath, packagePath }, context);\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: normalizeRelativePath(path.relative(packagePath, path.join(nestedDefinitionPath, entryPath))),\n },\n context,\n )),\n typesPath:\n typesPath && normalizeRelativePath(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 = normalizeRelativePath(typesPath);\n }\n\n if (filePath) {\n result.filePath = normalizeRelativePath(filePath);\n }\n\n if (sourcePath) {\n result.sourcePath = normalizeRelativePath(sourcePath);\n }\n\n return result;\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type PackageDefinitionsCache } from '@ms-cloudpack/common-types';
|
|
2
|
+
/**
|
|
3
|
+
* Given a package path, read the package definition's main and modules, if present,
|
|
4
|
+
* to uncover cjs and esm output paths. If both main and module are present, and they
|
|
5
|
+
* point to the same filename in distinct paths, we can infer the common ancestor to
|
|
6
|
+
* derive cjs/esm output paths. This is particularly useful in getCandidates, where
|
|
7
|
+
* we need to translate a request import file path into a physical file.
|
|
8
|
+
*
|
|
9
|
+
* Paths are normalized - slashes will be forward, paths are relative and start with a period,
|
|
10
|
+
* and they will always end with a trailing slash to make it easy to replace within
|
|
11
|
+
* findCandidates.
|
|
12
|
+
*
|
|
13
|
+
* If there is not enough info to conclude the output paths, return undefined.
|
|
14
|
+
*/
|
|
15
|
+
export declare function findPackageOutputPaths(packagePath: string, context: {
|
|
16
|
+
packages: PackageDefinitionsCache;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
cjsPath: string;
|
|
19
|
+
esmPath: string;
|
|
20
|
+
} | undefined>;
|
|
21
|
+
//# sourceMappingURL=findPackageOutputPaths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findPackageOutputPaths.d.ts","sourceRoot":"","sources":["../src/findPackageOutputPaths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAK1E;;;;;;;;;;;;GAYG;AACH,wBAAsB,sBAAsB,CAC1C,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE;IAAE,QAAQ,EAAE,uBAAuB,CAAA;CAAE,GAC7C,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAC,CA+E3D"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import { isFileSync, isFolderSync } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
/**
|
|
5
|
+
* Given a package path, read the package definition's main and modules, if present,
|
|
6
|
+
* to uncover cjs and esm output paths. If both main and module are present, and they
|
|
7
|
+
* point to the same filename in distinct paths, we can infer the common ancestor to
|
|
8
|
+
* derive cjs/esm output paths. This is particularly useful in getCandidates, where
|
|
9
|
+
* we need to translate a request import file path into a physical file.
|
|
10
|
+
*
|
|
11
|
+
* Paths are normalized - slashes will be forward, paths are relative and start with a period,
|
|
12
|
+
* and they will always end with a trailing slash to make it easy to replace within
|
|
13
|
+
* findCandidates.
|
|
14
|
+
*
|
|
15
|
+
* If there is not enough info to conclude the output paths, return undefined.
|
|
16
|
+
*/
|
|
17
|
+
export async function findPackageOutputPaths(packagePath, context) {
|
|
18
|
+
const packageDefinition = await context.packages.get(packagePath, { disableTransforms: true });
|
|
19
|
+
if (!packageDefinition) {
|
|
20
|
+
throw new Error(`Package definition not found for ${packagePath}`);
|
|
21
|
+
}
|
|
22
|
+
const { exports, main, module } = packageDefinition;
|
|
23
|
+
// If an exports map is provided, or we're missing main or module, don't
|
|
24
|
+
// try and derive an output path. We don't have enough information.
|
|
25
|
+
if (exports || !main || !module || main === module) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
// We should only attempt to find package output paths if main and module
|
|
29
|
+
// are filenames that have extensions.
|
|
30
|
+
const mainExt = path.extname(main);
|
|
31
|
+
const moduleExt = path.extname(module);
|
|
32
|
+
// If main/module have an extension, we need to remove the filename to get the folder.
|
|
33
|
+
let cjsPath = main;
|
|
34
|
+
let esmPath = module;
|
|
35
|
+
// If either are folders, we should assume we're looking for index.js within the folder.
|
|
36
|
+
if (!mainExt) {
|
|
37
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
38
|
+
if (isFolderSync(path.resolve(packagePath, main))) {
|
|
39
|
+
cjsPath = slash(path.join(main, 'index.js'));
|
|
40
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
41
|
+
}
|
|
42
|
+
else if (isFileSync(path.resolve(packagePath, main + '.js'))) {
|
|
43
|
+
cjsPath = main + '.js';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!moduleExt) {
|
|
47
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
48
|
+
if (isFolderSync(path.resolve(packagePath, module))) {
|
|
49
|
+
esmPath = slash(path.join(module, 'index.js'));
|
|
50
|
+
// eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
|
|
51
|
+
}
|
|
52
|
+
else if (isFileSync(path.resolve(packagePath, module + '.js'))) {
|
|
53
|
+
esmPath = module + '.js';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// If the filenames don't match (ignoring extensions because of cjs vs mjs), we could
|
|
57
|
+
// be in an asymmetrical case (e.g. main: /dist/foo.js vs module: /dist-esm/src/bar.js)
|
|
58
|
+
if (path.basename(cjsPath, mainExt) !== path.basename(esmPath, moduleExt)) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
// Ignore the filenames (which might differ on extension, js vs cjs vs mjs.)
|
|
62
|
+
cjsPath = path.dirname(cjsPath);
|
|
63
|
+
esmPath = path.dirname(esmPath);
|
|
64
|
+
// They are different - strip off the common paths at the end to derive
|
|
65
|
+
// the unique cjs/esm output paths.
|
|
66
|
+
// For example, if main is `lib/cjs/foo/bar/index.js' and module is `lib/esm/foo/bar/index.js`,
|
|
67
|
+
// then the cjsOutputPath is `lib/cjs` and the esmOutputPath is `lib/esm`.
|
|
68
|
+
const mainParts = cjsPath?.split('/');
|
|
69
|
+
const moduleParts = esmPath?.split('/');
|
|
70
|
+
const commonParts = [];
|
|
71
|
+
while (mainParts.length &&
|
|
72
|
+
moduleParts.length &&
|
|
73
|
+
mainParts[mainParts.length - 1] === moduleParts[moduleParts.length - 1]) {
|
|
74
|
+
commonParts.unshift(mainParts.pop());
|
|
75
|
+
moduleParts.pop();
|
|
76
|
+
}
|
|
77
|
+
cjsPath = normalizeRelativePath(mainParts.length ? mainParts.join('/') : cjsPath || '') + '/';
|
|
78
|
+
esmPath = normalizeRelativePath(moduleParts.length ? moduleParts.join('/') : esmPath || '') + '/';
|
|
79
|
+
if (cjsPath === esmPath || cjsPath === './' || esmPath === './') {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return { cjsPath, esmPath };
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=findPackageOutputPaths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findPackageOutputPaths.js","sourceRoot":"","sources":["../src/findPackageOutputPaths.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,WAAmB,EACnB,OAA8C;IAE9C,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC,CAAC;IAE/F,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,oCAAoC,WAAW,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,iBAAiB,CAAC;IAEpD,wEAAwE;IACxE,mEAAmE;IACnE,IAAI,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,KAAK,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AAC;IAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import { type PackageDefinitionsCache } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport { isFileSync, isFolderSync } from '@ms-cloudpack/path-utilities';\nimport path from 'path';\n\n/**\n * Given a package path, read the package definition's main and modules, if present,\n * to uncover cjs and esm output paths. If both main and module are present, and they\n * point to the same filename in distinct paths, we can infer the common ancestor to\n * derive cjs/esm output paths. This is particularly useful in getCandidates, where\n * we need to translate a request import file path into a physical file.\n *\n * Paths are normalized - slashes will be forward, paths are relative and start with a period,\n * and they will always end with a trailing slash to make it easy to replace within\n * findCandidates.\n *\n * If there is not enough info to conclude the output paths, return undefined.\n */\nexport async function findPackageOutputPaths(\n packagePath: string,\n context: { packages: PackageDefinitionsCache },\n): Promise<{ cjsPath: string; esmPath: string } | undefined> {\n const packageDefinition = await context.packages.get(packagePath, { disableTransforms: true });\n\n if (!packageDefinition) {\n throw new Error(`Package definition not found for ${packagePath}`);\n }\n\n const { exports, main, module } = packageDefinition;\n\n // If an exports map is provided, or we're missing main or module, don't\n // try and derive an output path. We don't have enough information.\n if (exports || !main || !module || main === module) {\n return undefined;\n }\n\n // We should only attempt to find package output paths if main and module\n // are filenames that have extensions.\n const mainExt = path.extname(main);\n const moduleExt = path.extname(module);\n\n // If main/module have an extension, we need to remove the filename to get the folder.\n let cjsPath = main;\n let esmPath = module;\n\n // If either are folders, we should assume we're looking for index.js within the folder.\n if (!mainExt) {\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n if (isFolderSync(path.resolve(packagePath, main))) {\n cjsPath = slash(path.join(main, 'index.js'));\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n } else if (isFileSync(path.resolve(packagePath, main + '.js'))) {\n cjsPath = main + '.js';\n }\n }\n if (!moduleExt) {\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n if (isFolderSync(path.resolve(packagePath, module))) {\n esmPath = slash(path.join(module, 'index.js'));\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n } else if (isFileSync(path.resolve(packagePath, module + '.js'))) {\n esmPath = module + '.js';\n }\n }\n\n // If the filenames don't match (ignoring extensions because of cjs vs mjs), we could\n // be in an asymmetrical case (e.g. main: /dist/foo.js vs module: /dist-esm/src/bar.js)\n if (path.basename(cjsPath, mainExt) !== path.basename(esmPath, moduleExt)) {\n return undefined;\n }\n\n // Ignore the filenames (which might differ on extension, js vs cjs vs mjs.)\n cjsPath = path.dirname(cjsPath);\n esmPath = path.dirname(esmPath);\n\n // They are different - strip off the common paths at the end to derive\n // the unique cjs/esm output paths.\n // For example, if main is `lib/cjs/foo/bar/index.js' and module is `lib/esm/foo/bar/index.js`,\n // then the cjsOutputPath is `lib/cjs` and the esmOutputPath is `lib/esm`.\n const mainParts = cjsPath?.split('/');\n const moduleParts = esmPath?.split('/');\n const commonParts = [];\n\n while (\n mainParts.length &&\n moduleParts.length &&\n mainParts[mainParts.length - 1] === moduleParts[moduleParts.length - 1]\n ) {\n commonParts.unshift(mainParts.pop());\n moduleParts.pop();\n }\n\n cjsPath = normalizeRelativePath(mainParts.length ? mainParts.join('/') : cjsPath || '') + '/';\n esmPath = normalizeRelativePath(moduleParts.length ? moduleParts.join('/') : esmPath || '') + '/';\n\n if (cjsPath === esmPath || cjsPath === './' || esmPath === './') {\n return undefined;\n }\n\n return { cjsPath, esmPath };\n}\n"]}
|
package/lib/getCandidates.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CloudpackConfig } from '@ms-cloudpack/common-types';
|
|
1
|
+
import { type CloudpackConfig, type PackageDefinitionsCache } from '@ms-cloudpack/common-types';
|
|
2
2
|
/**
|
|
3
3
|
* Given a packagePath and a filePath from its exports map (or main/module), build candidate paths
|
|
4
4
|
* to look for the physical file in order of priority. For internal packages, we try to discover
|
|
@@ -11,5 +11,6 @@ export declare function getCandidates(options: {
|
|
|
11
11
|
packagePath: string;
|
|
12
12
|
}, context: {
|
|
13
13
|
config: CloudpackConfig;
|
|
14
|
-
|
|
14
|
+
packages: PackageDefinitionsCache;
|
|
15
|
+
}): Promise<string[]>;
|
|
15
16
|
//# sourceMappingURL=getCandidates.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getCandidates.d.ts","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"getCandidates.d.ts","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AA2BhG;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,EAClD,OAAO,EAAE;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,QAAQ,EAAE,uBAAuB,CAAA;CAAE,qBA2ExE"}
|
package/lib/getCandidates.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { isExternalPackage } from './isExternalPackage.js';
|
|
4
|
+
import { findPackageOutputPaths } from './findPackageOutputPaths.js';
|
|
4
5
|
// Mappings to possible candidate extensions for a given input extension.
|
|
5
6
|
const externalExtensions = ['.js', '.mjs', '.cjs'];
|
|
6
7
|
const internalExtensions = ['.ts', '.tsx', '.mts', '.cts', '.jsx', '.js', '.mjs', '.cjs'];
|
|
@@ -29,7 +30,8 @@ const externalExtensionMap = {
|
|
|
29
30
|
* and expand folder paths as well for ambiguous references. For external packages, we prioritize
|
|
30
31
|
* intermediate paths.
|
|
31
32
|
*/
|
|
32
|
-
export function getCandidates(options, context) {
|
|
33
|
+
export async function getCandidates(options, context) {
|
|
34
|
+
const outputPaths = await findPackageOutputPaths(options.packagePath, context);
|
|
33
35
|
const { filePath, packagePath } = options;
|
|
34
36
|
const resolveWebExtensions = Boolean(context.config.features?.resolveWebExtensions);
|
|
35
37
|
const isBlankPath = !filePath || filePath === '.';
|
|
@@ -43,9 +45,29 @@ export function getCandidates(options, context) {
|
|
|
43
45
|
? [...defaultExtensions.map((ext) => `.web${ext}`), ...defaultExtensions]
|
|
44
46
|
: defaultExtensions;
|
|
45
47
|
const extensions = (isInternal ? internalExtensionMap : externalExtensionMap)[originalExt] || defaultExtensionsWithSuffixes;
|
|
46
|
-
const pathsToAdd = [
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
const pathsToAdd = [];
|
|
49
|
+
// For internal packages, we try and resolve the source file name first.
|
|
50
|
+
const { esmPath, cjsPath } = outputPaths || {};
|
|
51
|
+
if (isInternal) {
|
|
52
|
+
if (cjsPath && esmPath) {
|
|
53
|
+
if (originalPath.startsWith(esmPath)) {
|
|
54
|
+
pathsToAdd.push(originalPath.replace(esmPath, './src/'));
|
|
55
|
+
}
|
|
56
|
+
else if (originalPath.startsWith(cjsPath)) {
|
|
57
|
+
pathsToAdd.push(originalPath.replace(cjsPath, './src/'));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else if (originalPath.startsWith('./lib/')) {
|
|
61
|
+
pathsToAdd.push(originalPath.replace(/^\.\/lib\//, './src/'));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (esmPath && cjsPath) {
|
|
65
|
+
// We have a distinct esm output path. If the original path refers to the cjsPath,
|
|
66
|
+
// refer only to the esm path.
|
|
67
|
+
pathsToAdd.push(originalPath.replace(cjsPath, esmPath));
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
pathsToAdd.push(originalPath);
|
|
49
71
|
}
|
|
50
72
|
for (let currentPath of pathsToAdd) {
|
|
51
73
|
const unrecognizedExt = originalExt && !extensions.includes(path.extname(currentPath));
|
|
@@ -59,18 +81,13 @@ export function getCandidates(options, context) {
|
|
|
59
81
|
}
|
|
60
82
|
// For non-extension references, we might be referring to a folder, or a barrel file within a folder. Add the
|
|
61
83
|
// appropriate extensions for the barrel file, and fall back to a package.json in the case of nested definitions.
|
|
62
|
-
if (!originalExt) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
else {
|
|
67
|
-
// Trim trailing slash
|
|
68
|
-
if (endsWithSlash) {
|
|
69
|
-
currentPath = currentPath.slice(0, -1);
|
|
70
|
-
}
|
|
71
|
-
candidates.push(...expandExtensions(currentPath + '/index', extensions));
|
|
72
|
-
candidates.push(currentPath + '/package.json');
|
|
84
|
+
if (!originalExt && !isBlankPath) {
|
|
85
|
+
// Trim trailing slash
|
|
86
|
+
if (endsWithSlash) {
|
|
87
|
+
currentPath = currentPath.slice(0, -1);
|
|
73
88
|
}
|
|
89
|
+
candidates.push(...expandExtensions(currentPath + '/index', extensions));
|
|
90
|
+
candidates.push(currentPath + '/package.json');
|
|
74
91
|
}
|
|
75
92
|
// For external packages, we try and resolve the original file name last.
|
|
76
93
|
if (!isInternal && unrecognizedExt) {
|
package/lib/getCandidates.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getCandidates.js","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"getCandidates.js","sourceRoot":"","sources":["../src/getCandidates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,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ACjC,sBAAsB;YACtB,IAAI,aAAa,EAAE,CAAC;gBAClB,WAAW,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACzC,CAAC;YAED,UAAU,CAAC,IAAI,CAAC,GAAG,gBAAgB,CAAC,WAAW,GAAG,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC;YACzE,UAAU,CAAC,IAAI,CAAC,WAAW,GAAG,eAAe,CAAC,CAAC;QACjD,CAAC;QAED,yEAAyE;QACzE,IAAI,CAAC,UAAU,IAAI,eAAe,EAAE,CAAC;YACnC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;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,CAAC;QAC7B,QAAQ,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;IACjD,CAAC;IAED,KAAK,MAAM,UAAU,IAAI,UAAU,EAAE,CAAC;QACpC,MAAM,CAAC,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC;IACrC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing';\nimport { type CloudpackConfig, type PackageDefinitionsCache } from '@ms-cloudpack/common-types';\nimport path from 'path';\nimport { isExternalPackage } from './isExternalPackage.js';\nimport { findPackageOutputPaths } from './findPackageOutputPaths.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': ['.js'],\n '.tsx': ['.js'],\n '.mts': ['.mjs'],\n '.cts': ['.cjs'],\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 async function getCandidates(\n options: { filePath: string; packagePath: string },\n context: { config: CloudpackConfig; packages: PackageDefinitionsCache },\n) {\n const outputPaths = await findPackageOutputPaths(options.packagePath, context);\n const { filePath, packagePath } = options;\n const resolveWebExtensions = Boolean(context.config.features?.resolveWebExtensions);\n const isBlankPath = !filePath || filePath === '.';\n const originalPath = isBlankPath ? './index' : normalizeRelativePath(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 defaultExtensionsWithSuffixes = resolveWebExtensions\n ? [...defaultExtensions.map((ext) => `.web${ext}`), ...defaultExtensions]\n : defaultExtensions;\n const extensions =\n (isInternal ? internalExtensionMap : externalExtensionMap)[originalExt] || defaultExtensionsWithSuffixes;\n\n const pathsToAdd: string[] = [];\n\n // For internal packages, we try and resolve the source file name first.\n const { esmPath, cjsPath } = outputPaths || {};\n\n if (isInternal) {\n if (cjsPath && esmPath) {\n if (originalPath.startsWith(esmPath)) {\n pathsToAdd.push(originalPath.replace(esmPath, './src/'));\n } else if (originalPath.startsWith(cjsPath)) {\n pathsToAdd.push(originalPath.replace(cjsPath, './src/'));\n }\n } else if (originalPath.startsWith('./lib/')) {\n pathsToAdd.push(originalPath.replace(/^\\.\\/lib\\//, './src/'));\n }\n }\n\n if (esmPath && cjsPath) {\n // We have a distinct esm output path. If the original path refers to the cjsPath,\n // refer only to the esm path.\n pathsToAdd.push(originalPath.replace(cjsPath, esmPath));\n } else {\n pathsToAdd.push(originalPath);\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 && !isBlankPath) {\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 // 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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/package-utilities",
|
|
3
|
-
"version": "7.1
|
|
3
|
+
"version": "7.2.1",
|
|
4
4
|
"description": "Utilities for resolving/parsing packages and their imports.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/common-types": "^0.
|
|
17
|
+
"@ms-cloudpack/common-types": "^0.3.0",
|
|
18
18
|
"@ms-cloudpack/json-utilities": "^0.1.4",
|
|
19
|
-
"@ms-cloudpack/package-overrides": "^0.7.
|
|
19
|
+
"@ms-cloudpack/package-overrides": "^0.7.7",
|
|
20
20
|
"@ms-cloudpack/path-string-parsing": "^1.2.1",
|
|
21
|
-
"@ms-cloudpack/path-utilities": "^2.7.
|
|
21
|
+
"@ms-cloudpack/path-utilities": "^2.7.4",
|
|
22
22
|
"acorn": "^8.11.2",
|
|
23
23
|
"acorn-walk": "^8.2.1",
|
|
24
24
|
"fast-glob": "^3.2.12",
|