MCPcopy
hub / github.com/vitejs/vite / expandGlobIds

Function expandGlobIds

packages/vite/src/node/optimizer/resolve.ts:46–158  ·  view source on GitHub ↗
(id: string, config: ResolvedConfig)

Source from the content-addressed store, hash-verified

44 * Expand the glob syntax in `optimizeDeps.include` to proper import paths
45 */
46export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
47 const pkgName = getNpmPackageName(id)
48 if (!pkgName) return []
49
50 const pkgData = resolvePackageData(
51 pkgName,
52 config.root,
53 config.resolve.preserveSymlinks,
54 config.packageCache,
55 )
56 if (!pkgData) return []
57
58 const pattern = '.' + id.slice(pkgName.length)
59 const exports = pkgData.data.exports
60
61 // if package has exports field, get all possible export paths and apply
62 // glob on them with picomatch
63 if (exports) {
64 if (typeof exports === 'string' || Array.isArray(exports)) {
65 return [pkgName]
66 }
67
68 const possibleExportPaths: string[] = []
69 for (const key in exports) {
70 if (key[0] === '.') {
71 if (key.includes('*')) {
72 // "./glob/*": {
73 // "browser": "./dist/glob/*-browser/*.js", <-- get this one
74 // "default": "./dist/glob/*/*.js"
75 // }
76 // NOTE: theoretically the "default" condition could map to a different
77 // set of files, but that complicates the resolve logic, so we assume
78 // all conditions map to the same set of files, and get the first one.
79 const exportsValue = getFirstExportStringValue(exports[key])
80 if (!exportsValue) continue
81
82 // "./dist/glob/*-browser/*.js" => "./dist/glob/**/*-browser/**/*.js"
83 // NOTE: in some cases, this could expand to consecutive /**/*/**/* etc
84 // but it's fine since `tinyglobby` handles it the same.
85 const exportValuePattern = exportsValue.replace(/\*/g, '**/*')
86 // "./dist/glob/*-browser/*.js" => /dist\/glob\/(.*)-browser\/(.*)\.js/
87 const exportsValueGlobRe = new RegExp(
88 exportsValue.split('*').map(escapeRegex).join('(.*)'),
89 )
90
91 possibleExportPaths.push(
92 ...globSync(exportValuePattern, {
93 cwd: pkgData.dir,
94 expandDirectories: false,
95 ignore: ['node_modules'],
96 })
97 .map((filePath) => {
98 // `tinyglobby` returns paths as they are formatted by the underlying `fdir`.
99 // Both `globSync("./some-dir/**/*")` and `globSync("./**/*")` result in
100 // `"some-dir/somefile"` being returned, so we ensure the correct prefix manually.
101 if (exportsValue.startsWith('./')) {
102 filePath = './' + filePath
103 }

Callers 2

resolve.spec.tsFile · 0.90

Calls 5

getNpmPackageNameFunction · 0.90
resolvePackageDataFunction · 0.90
slashFunction · 0.90
isMatchFunction · 0.50

Tested by

no test coverage detected