Skip to content

Commit

Permalink
fix: use picomatch to align with tinyglobby (#18503)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 30, 2024
1 parent 04f6736 commit 437795d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 122 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@types/estree": "^1.0.6",
"@types/etag": "^1.8.3",
"@types/less": "^3.0.6",
"@types/micromatch": "^4.0.9",
"@types/node": "^20.17.1",
"@types/picomatch": "^3.0.1",
"@types/stylus": "^0.48.43",
Expand Down
70 changes: 0 additions & 70 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,47 +448,6 @@ Repository: git+https://github.com/sapphi-red/artichokie.git
---------------------------------------

## braces, fill-range, is-number, micromatch
License: MIT
By: Jon Schlinkert, Brian Woodward, Elan Shanker, Eugene Sharygin, hemanth.hm
Repository: micromatch/braces

License: MIT
By: Jon Schlinkert, Edo Rivai, Paul Miller, Rouven Weßling
Repository: jonschlinkert/fill-range

License: MIT
By: Jon Schlinkert, Olsten Larck, Rouven Weßling
Repository: jonschlinkert/is-number

License: MIT
By: Jon Schlinkert, Amila Welihinda, Bogdan Chadkin, Brian Woodward, Devon Govett, Elan Shanker, Fabrício Matté, Martin Kolárik, Olsten Larck, Paul Miller, Tom Byrer, Tyler Akins, Peter Bright, Kuba Juszczyk
Repository: micromatch/micromatch

> The MIT License (MIT)
>
> Copyright (c) 2014-present, Jon Schlinkert.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
---------------------------------------

## cac
License: MIT
By: egoist
Expand Down Expand Up @@ -2085,35 +2044,6 @@ Repository: git+https://github.com/SuperchupuDev/tinyglobby.git
---------------------------------------

## to-regex-range
License: MIT
By: Jon Schlinkert, Rouven Weßling
Repository: micromatch/to-regex-range

> The MIT License (MIT)
>
> Copyright (c) 2015-present, Jon Schlinkert.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
---------------------------------------

## tsconfck
License: MIT
By: dominikg
Expand Down
1 change: 0 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
"launch-editor-middleware": "^2.9.1",
"lightningcss": "^1.27.0",
"magic-string": "^0.30.12",
"micromatch": "^4.0.8",
"mlly": "^1.7.2",
"mrmime": "^2.0.0",
"nanoid": "^5.0.7",
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/node/optimizer/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import micromatch from 'micromatch'
import picomatch from 'picomatch'
import { globSync } from 'tinyglobby'
import type { ResolvedConfig } from '../config'
import { escapeRegex, getNpmPackageName } from '../utils'
Expand Down Expand Up @@ -59,7 +59,7 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
const exports = pkgData.data.exports

// if package has exports field, get all possible export paths and apply
// glob on them with micromatch
// glob on them with picomatch
if (exports) {
if (typeof exports === 'string' || Array.isArray(exports)) {
return [pkgName]
Expand Down Expand Up @@ -136,9 +136,9 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
}
}

const matched = micromatch(possibleExportPaths, pattern).map((match) =>
path.posix.join(pkgName, match),
)
const matched = possibleExportPaths
.filter(picomatch(pattern))
.map((match) => path.posix.join(pkgName, match))
matched.unshift(pkgName)
return matched
} else {
Expand Down
51 changes: 24 additions & 27 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAbsolute, posix } from 'node:path'
import micromatch from 'micromatch'
import picomatch from 'picomatch'
import { stripLiteral } from 'strip-literal'
import colors from 'picocolors'
import type {
Expand All @@ -24,8 +24,6 @@ import type { Logger } from '../logger'
import { slash } from '../../shared/utils'
import type { Environment } from '../environment'

const { isMatch, scan } = micromatch

export interface ParsedImportGlob {
index: number
globs: string[]
Expand All @@ -43,7 +41,7 @@ interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions {
export function importGlobPlugin(config: ResolvedConfig): Plugin {
const importGlobMaps = new Map<
Environment,
Map<string, { affirmed: string[]; negated: string[] }[]>
Map<string, Array<(file: string) => boolean>>
>()

return {
Expand All @@ -67,18 +65,25 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
if (!importGlobMaps.has(this.environment)) {
importGlobMaps.set(this.environment, new Map())
}
importGlobMaps.get(this.environment)!.set(
id,
allGlobs.map((globs) => {
const affirmed: string[] = []
const negated: string[] = []

for (const glob of globs) {
;(glob[0] === '!' ? negated : affirmed).push(glob)
}
return { affirmed, negated }
}),
)

const globMatchers = allGlobs.map((globs) => {
const affirmed: string[] = []
const negated: string[] = []
for (const glob of globs) {
;(glob[0] === '!' ? negated : affirmed).push(glob)
}
const affirmedMatcher = picomatch(affirmed)
const negatedMatcher = picomatch(negated)

return (file: string) => {
// (glob1 || glob2) && !(glob3 || glob4)...
return (
(affirmed.length === 0 || affirmedMatcher(file)) &&
!(negated.length > 0 && negatedMatcher(file))
)
}
})
importGlobMaps.get(this.environment)!.set(id, globMatchers)

return transformStableResult(result.s, id, config)
}
Expand All @@ -90,16 +95,8 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
if (!importGlobMap) return

const modules: EnvironmentModuleNode[] = []
for (const [id, allGlobs] of importGlobMap) {
// (glob1 || glob2) && !glob3 && !glob4...
if (
allGlobs.some(
({ affirmed, negated }) =>
(!affirmed.length ||
affirmed.some((glob) => isMatch(file, glob))) &&
(!negated.length || negated.every((glob) => isMatch(file, glob))),
)
) {
for (const [id, globMatchers] of importGlobMap) {
if (globMatchers.some((matcher) => matcher(file))) {
const mod = this.environment.moduleGraph.getModuleById(id)
if (mod) modules.push(mod)
}
Expand Down Expand Up @@ -577,7 +574,7 @@ export function getCommonBase(globsResolved: string[]): null | string {
const bases = globsResolved
.filter((g) => g[0] !== '!')
.map((glob) => {
let { base } = scan(glob)
let { base } = picomatch.scan(glob)
// `scan('a/foo.js')` returns `base: 'a/foo.js'`
if (posix.basename(base).includes('.')) base = posix.dirname(base)

Expand Down
18 changes: 0 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 437795d

Please sign in to comment.