@ms-cloudpack/package-utilities 10.4.0 → 10.4.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/flattenExportsMap.js
CHANGED
|
@@ -2,7 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import { cleanEntry } from './cleanEntry.js';
|
|
3
3
|
import glob from 'fast-glob';
|
|
4
4
|
import { mergeArrayDefaults } from './mergeArrayDefaults.js';
|
|
5
|
-
const defaultConditions = ['browser', 'module', 'import', 'default'];
|
|
5
|
+
const defaultConditions = ['browser', 'module', 'import', 'density', 'default'];
|
|
6
6
|
/**
|
|
7
7
|
* Given a package definition and applicable conditions, return a flat map of package import to physical (relative) path.
|
|
8
8
|
* Optionally return the exports map fully qualified with conditions intact using the `fullExports` option.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flattenExportsMap.js","sourceRoot":"","sources":["../src/flattenExportsMap.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,iBAAiB,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAErE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAC/B,UAA8B,EAC9B,UAMI,EAAE;IAEN,MAAM,EAAE,kBAAkB,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAEpD,MAAM,UAAU,GAAG,kBAAkB,CAAC,OAAO,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;IAE7E,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QACrE,OAAO;YACL,GAAG,EAAE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AC/E,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,GAAG,MAAM,CAAC;IACxB,CAAC;SAAM,IAAI,aAAa,KAAK,CAAC,CAAC,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,aAAa,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC,aAAa,GAAG,CAAC,CAAC,CAAC;IACzF,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAS,WAAW,CAAC,KAAa,EAAE,gBAAwB,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IAC/E,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;IACnC,CAAC;SAAM,IAAI,aAAa,KAAK,CAAC,CAAC,EAAE,CAAC;QAChC,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,aAAa,CAAC;YACxC,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,aAAa,GAAG,CAAC,CAAC;SACxC,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import path from 'path';\nimport type { PackageJsonExportsObject, PackageJsonExports } from '@ms-cloudpack/common-types';\nimport { cleanEntry } from './cleanEntry.js';\nimport glob from 'fast-glob';\nimport { mergeArrayDefaults } from './mergeArrayDefaults.js';\n\nconst defaultConditions = ['browser', 'module', 'import', 'default'];\n\n/**\n * Given a package definition and applicable conditions, return a flat map of package import to physical (relative) path.\n * Optionally return the exports map fully qualified with conditions intact using the `fullExports` option.\n * Optionally takes in a `packagePath` to resolve entries and values that end in `/` and with wildcards `*`.\n *\n * Notes:\n * - This diverges from the behavior of Node in that conditions are considered in order of\n * the `conditions` array, NOT in the order keys are defined in the exports map.\n * - Null export values are supported only for generated config. A null export signals to Cloudpack\n * that the entry should be completely dropped from the flattened exports map.\n * - Following the behavior of Node, for perf reasons, we do NOT verify whether any entries\n * exist on disk except wildcard or trailing slash exports since it is required.\n * (This differs from the behavior of typescript and webpack.)\n */\nexport function flattenExportsMap(\n exportsMap: PackageJsonExports,\n options: {\n /** The conditions to check for in the exports object. */\n conditions?: string[];\n /** The conditions that must be matched to add the export. */\n requiredConditions?: string[];\n packagePath?: string;\n } = {},\n): Record<string, string> {\n const { requiredConditions, packagePath } = options;\n\n const conditions = mergeArrayDefaults(options.conditions, defaultConditions);\n\n if (typeof exportsMap === 'string' && conditions.includes('default')) {\n return {\n '.': cleanEntry(exportsMap),\n };\n }\n\n return extractExports({\n exports: exportsMap,\n conditions,\n requiredConditions,\n flatExports: {},\n importMatch: '.',\n matchedConditions: [],\n packagePath,\n });\n}\n\n/**\n * Extract the exports from an exports object or sub-object and add them to the flat exports map.\n *\n * @returns The cumulative flat exports map.\n */\nfunction extractExports(params: {\n /** The exports object to extract or a sub-object. */\n exports: PackageJsonExports;\n /** The conditions to check for in the exports object. */\n conditions: string[];\n /** The conditions that must be matched to add the export. */\n requiredConditions?: string[];\n /** The cumulative flat exports map to add the exports to. */\n flatExports: Record<string, string>;\n /** The import path to match */\n importMatch: string;\n /** The conditions that have been matched so far. */\n matchedConditions: string[];\n /** The path to the package. */\n packagePath?: string | undefined;\n}) {\n const { exports, conditions, requiredConditions, flatExports, matchedConditions, packagePath, importMatch } = params;\n\n if (\n typeof exports === 'string' &&\n (!requiredConditions || requiredConditions.every((condition) => matchedConditions.includes(condition)))\n ) {\n addExportsEntry({ flatExports, key: importMatch, value: exports, packagePath });\n } else if (Array.isArray(exports)) {\n for (const exportEntry of exports) {\n extractExports({ ...params, exports: exportEntry });\n }\n } else if (exports && typeof exports === 'object') {\n // Iterate through condition matches and go deeper if a match is found.\n for (const condition of conditions) {\n const conditionExports = (exports as PackageJsonExportsObject)[condition];\n\n if (conditionExports) {\n extractExports({\n ...params,\n exports: conditionExports,\n matchedConditions: matchedConditions.concat(condition),\n });\n // Stop at the first match.\n return flatExports;\n }\n }\n\n // No conditions are available. Check for import entries.\n for (const [key, value] of Object.entries(exports)) {\n if (!key.startsWith('.')) {\n continue; // not a path\n }\n\n if (typeof value === 'string') {\n if (!flatExports[key] && (!requiredConditions || requiredConditions?.includes(key))) {\n addExportsEntry({ flatExports, key, value, packagePath });\n }\n } else if (value) {\n extractExports({\n ...params,\n exports: value,\n importMatch: key,\n matchedConditions: [...matchedConditions, key],\n });\n } else {\n delete flatExports[key];\n }\n }\n }\n\n return flatExports;\n}\n\n/**\n * Add an entry to the flattened exports map if the key and value are valid\n * and the key is not already in the map.\n *\n * Does nothing if the key contains a wildcard or a trailing slash and the value does not, such as it being `null`.\n */\nfunction addExportsEntry(params: {\n /** The path to the package. */\n flatExports: Record<string, string>;\n /** The flattened exports map. */\n key: string;\n /** The key to add to the map. */\n value: string;\n /** The relative physical path to add to the map. */\n packagePath: string | undefined;\n}) {\n const { flatExports, key, value, packagePath } = params;\n\n const basename = path.basename(value);\n const isValid = basename !== '' && basename !== '.';\n const isMany = value.endsWith('/') || value.indexOf('*') !== -1 || key.indexOf('*') !== -1 || key.endsWith('/');\n\n // Skip if the key is already in the map.\n if (flatExports[key]) return;\n\n if (isValid && !isMany) {\n flatExports[key] = cleanEntry(value);\n return;\n }\n\n if (value === null) {\n // Debug log for null values in the exports map.\n console.debug(`Skipping null value for key \"${key}\" in exports map${packagePath ? ` of \"${packagePath}\"` : ''}.`);\n return;\n }\n\n // Debug log for export patterns found in the exports map.\n console.debug(`Found export patterns in ${packagePath ? `\"${packagePath}\" ` : ''}exports map: \"${key}\": \"${value}\"`);\n\n // A package path is required to resolve entries and values that end in `/` or that include a wildcard `*`.\n if (!packagePath) return;\n\n // Check for wildcards in the key and value.\n // The wildcards\n const keyWildcard = key.indexOf('*', 1);\n const valueWildcard = value.indexOf('*', 1);\n\n if ((key.endsWith('/') && value.endsWith('/')) || (keyWildcard !== -1 && valueWildcard !== -1)) {\n const searchGlob = valueToGlob(value, valueWildcard);\n const keySplit = splitByGlob(key, keyWildcard);\n const valueSplit = splitByGlob(value, valueWildcard);\n if (!searchGlob || !keySplit || !valueSplit) return;\n // We only want to search for files.\n const matches = glob.sync(searchGlob, { cwd: packagePath, onlyFiles: true });\n for (const match of matches) {\n // Skip files that don't have an extension or have a `.` extension.\n if (path.extname(match).length <= 1) continue;\n // Get the glob match shared between the key and value.\n const globMatch = match.substring(\n // Globs that start with `./` don't return matches with `./` in the beginning.\n match.startsWith(valueSplit.start) ? valueSplit.start.length : 0,\n match.length - valueSplit.end.length,\n );\n // Replace the glob match in the key with the resolved glob match in the value.\n const matchKey = keySplit.start + globMatch + keySplit.end;\n if (!flatExports[matchKey]) {\n flatExports[matchKey] = cleanEntry(match);\n }\n }\n }\n}\n\n/**\n * Convert a value to a glob pattern.\n *\n * @param value The value to convert.\n * @param valueWildcard The index of the wildcard in the value.\n * @returns A value converted to a glob pattern.\n */\nfunction valueToGlob(value: string, valueWildcard: number = value.indexOf('*', 1)) {\n if (value.endsWith('/')) {\n return value + '**/*';\n } else if (valueWildcard !== -1) {\n return value.substring(0, valueWildcard) + '**/*' + value.substring(valueWildcard + 1);\n }\n}\n\n/**\n * Split a value by the location of the trailing slash or wildcard.\n *\n * @param value The value to split.\n * @param valueWildcard The index of the wildcard in the value.\n * @returns An object with the start and end substring of the split value.\n */\nfunction splitByGlob(value: string, valueWildcard: number = value.indexOf('*', 1)) {\n if (value.endsWith('/')) {\n return { start: value, end: '' };\n } else if (valueWildcard !== -1) {\n return {\n start: value.substring(0, valueWildcard),\n end: value.substring(valueWildcard + 1),\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"flattenExportsMap.js","sourceRoot":"","sources":["../src/flattenExportsMap.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,IAAI,MAAM,WAAW,CAA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AAC,SAAS,CAAC,aAAa,GAAG,CAAC,CAAC;SACxC,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import path from 'path';\nimport type { PackageJsonExportsObject, PackageJsonExports } from '@ms-cloudpack/common-types';\nimport { cleanEntry } from './cleanEntry.js';\nimport glob from 'fast-glob';\nimport { mergeArrayDefaults } from './mergeArrayDefaults.js';\n\nconst defaultConditions = ['browser', 'module', 'import', 'density', 'default'];\n\n/**\n * Given a package definition and applicable conditions, return a flat map of package import to physical (relative) path.\n * Optionally return the exports map fully qualified with conditions intact using the `fullExports` option.\n * Optionally takes in a `packagePath` to resolve entries and values that end in `/` and with wildcards `*`.\n *\n * Notes:\n * - This diverges from the behavior of Node in that conditions are considered in order of\n * the `conditions` array, NOT in the order keys are defined in the exports map.\n * - Null export values are supported only for generated config. A null export signals to Cloudpack\n * that the entry should be completely dropped from the flattened exports map.\n * - Following the behavior of Node, for perf reasons, we do NOT verify whether any entries\n * exist on disk except wildcard or trailing slash exports since it is required.\n * (This differs from the behavior of typescript and webpack.)\n */\nexport function flattenExportsMap(\n exportsMap: PackageJsonExports,\n options: {\n /** The conditions to check for in the exports object. */\n conditions?: string[];\n /** The conditions that must be matched to add the export. */\n requiredConditions?: string[];\n packagePath?: string;\n } = {},\n): Record<string, string> {\n const { requiredConditions, packagePath } = options;\n\n const conditions = mergeArrayDefaults(options.conditions, defaultConditions);\n\n if (typeof exportsMap === 'string' && conditions.includes('default')) {\n return {\n '.': cleanEntry(exportsMap),\n };\n }\n\n return extractExports({\n exports: exportsMap,\n conditions,\n requiredConditions,\n flatExports: {},\n importMatch: '.',\n matchedConditions: [],\n packagePath,\n });\n}\n\n/**\n * Extract the exports from an exports object or sub-object and add them to the flat exports map.\n *\n * @returns The cumulative flat exports map.\n */\nfunction extractExports(params: {\n /** The exports object to extract or a sub-object. */\n exports: PackageJsonExports;\n /** The conditions to check for in the exports object. */\n conditions: string[];\n /** The conditions that must be matched to add the export. */\n requiredConditions?: string[];\n /** The cumulative flat exports map to add the exports to. */\n flatExports: Record<string, string>;\n /** The import path to match */\n importMatch: string;\n /** The conditions that have been matched so far. */\n matchedConditions: string[];\n /** The path to the package. */\n packagePath?: string | undefined;\n}) {\n const { exports, conditions, requiredConditions, flatExports, matchedConditions, packagePath, importMatch } = params;\n\n if (\n typeof exports === 'string' &&\n (!requiredConditions || requiredConditions.every((condition) => matchedConditions.includes(condition)))\n ) {\n addExportsEntry({ flatExports, key: importMatch, value: exports, packagePath });\n } else if (Array.isArray(exports)) {\n for (const exportEntry of exports) {\n extractExports({ ...params, exports: exportEntry });\n }\n } else if (exports && typeof exports === 'object') {\n // Iterate through condition matches and go deeper if a match is found.\n for (const condition of conditions) {\n const conditionExports = (exports as PackageJsonExportsObject)[condition];\n\n if (conditionExports) {\n extractExports({\n ...params,\n exports: conditionExports,\n matchedConditions: matchedConditions.concat(condition),\n });\n // Stop at the first match.\n return flatExports;\n }\n }\n\n // No conditions are available. Check for import entries.\n for (const [key, value] of Object.entries(exports)) {\n if (!key.startsWith('.')) {\n continue; // not a path\n }\n\n if (typeof value === 'string') {\n if (!flatExports[key] && (!requiredConditions || requiredConditions?.includes(key))) {\n addExportsEntry({ flatExports, key, value, packagePath });\n }\n } else if (value) {\n extractExports({\n ...params,\n exports: value,\n importMatch: key,\n matchedConditions: [...matchedConditions, key],\n });\n } else {\n delete flatExports[key];\n }\n }\n }\n\n return flatExports;\n}\n\n/**\n * Add an entry to the flattened exports map if the key and value are valid\n * and the key is not already in the map.\n *\n * Does nothing if the key contains a wildcard or a trailing slash and the value does not, such as it being `null`.\n */\nfunction addExportsEntry(params: {\n /** The path to the package. */\n flatExports: Record<string, string>;\n /** The flattened exports map. */\n key: string;\n /** The key to add to the map. */\n value: string;\n /** The relative physical path to add to the map. */\n packagePath: string | undefined;\n}) {\n const { flatExports, key, value, packagePath } = params;\n\n const basename = path.basename(value);\n const isValid = basename !== '' && basename !== '.';\n const isMany = value.endsWith('/') || value.indexOf('*') !== -1 || key.indexOf('*') !== -1 || key.endsWith('/');\n\n // Skip if the key is already in the map.\n if (flatExports[key]) return;\n\n if (isValid && !isMany) {\n flatExports[key] = cleanEntry(value);\n return;\n }\n\n if (value === null) {\n // Debug log for null values in the exports map.\n console.debug(`Skipping null value for key \"${key}\" in exports map${packagePath ? ` of \"${packagePath}\"` : ''}.`);\n return;\n }\n\n // Debug log for export patterns found in the exports map.\n console.debug(`Found export patterns in ${packagePath ? `\"${packagePath}\" ` : ''}exports map: \"${key}\": \"${value}\"`);\n\n // A package path is required to resolve entries and values that end in `/` or that include a wildcard `*`.\n if (!packagePath) return;\n\n // Check for wildcards in the key and value.\n // The wildcards\n const keyWildcard = key.indexOf('*', 1);\n const valueWildcard = value.indexOf('*', 1);\n\n if ((key.endsWith('/') && value.endsWith('/')) || (keyWildcard !== -1 && valueWildcard !== -1)) {\n const searchGlob = valueToGlob(value, valueWildcard);\n const keySplit = splitByGlob(key, keyWildcard);\n const valueSplit = splitByGlob(value, valueWildcard);\n if (!searchGlob || !keySplit || !valueSplit) return;\n // We only want to search for files.\n const matches = glob.sync(searchGlob, { cwd: packagePath, onlyFiles: true });\n for (const match of matches) {\n // Skip files that don't have an extension or have a `.` extension.\n if (path.extname(match).length <= 1) continue;\n // Get the glob match shared between the key and value.\n const globMatch = match.substring(\n // Globs that start with `./` don't return matches with `./` in the beginning.\n match.startsWith(valueSplit.start) ? valueSplit.start.length : 0,\n match.length - valueSplit.end.length,\n );\n // Replace the glob match in the key with the resolved glob match in the value.\n const matchKey = keySplit.start + globMatch + keySplit.end;\n if (!flatExports[matchKey]) {\n flatExports[matchKey] = cleanEntry(match);\n }\n }\n }\n}\n\n/**\n * Convert a value to a glob pattern.\n *\n * @param value The value to convert.\n * @param valueWildcard The index of the wildcard in the value.\n * @returns A value converted to a glob pattern.\n */\nfunction valueToGlob(value: string, valueWildcard: number = value.indexOf('*', 1)) {\n if (value.endsWith('/')) {\n return value + '**/*';\n } else if (valueWildcard !== -1) {\n return value.substring(0, valueWildcard) + '**/*' + value.substring(valueWildcard + 1);\n }\n}\n\n/**\n * Split a value by the location of the trailing slash or wildcard.\n *\n * @param value The value to split.\n * @param valueWildcard The index of the wildcard in the value.\n * @returns An object with the start and end substring of the split value.\n */\nfunction splitByGlob(value: string, valueWildcard: number = value.indexOf('*', 1)) {\n if (value.endsWith('/')) {\n return { start: value, end: '' };\n } else if (valueWildcard !== -1) {\n return {\n start: value.substring(0, valueWildcard),\n end: value.substring(valueWildcard + 1),\n };\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/package-utilities",
|
|
3
|
-
"version": "10.4.
|
|
3
|
+
"version": "10.4.1",
|
|
4
4
|
"description": "Utilities for resolving/parsing packages and their imports.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/common-types": "^0.23.
|
|
17
|
+
"@ms-cloudpack/common-types": "^0.23.5",
|
|
18
18
|
"@ms-cloudpack/json-utilities": "^0.1.8",
|
|
19
19
|
"@ms-cloudpack/path-string-parsing": "^1.2.5",
|
|
20
|
-
"@ms-cloudpack/path-utilities": "^2.7.
|
|
20
|
+
"@ms-cloudpack/path-utilities": "^2.7.55",
|
|
21
21
|
"@ms-cloudpack/task-reporter": "^0.14.6",
|
|
22
22
|
"acorn": "^8.11.2",
|
|
23
23
|
"acorn-walk": "^8.2.1",
|