Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use micromatch for consistent glob matching #5610

Merged
merged 4 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/playground/glob-import/dir/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const modules = import.meta.globEager('./*.js')
const modules = import.meta.globEager('./*.(js|ts)')

export { modules }
3 changes: 2 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/estree": "^0.0.50",
"@types/etag": "^1.8.1",
"@types/less": "^3.0.3",
"@types/micromatch": "^4.0.0",
"@types/mime": "^2.0.3",
"@types/node": "^16.11.7",
"@types/resolve": "^1.20.1",
Expand Down Expand Up @@ -98,8 +99,8 @@
"http-proxy": "^1.18.1",
"launch-editor-middleware": "^2.2.1",
"magic-string": "^0.25.7",
"micromatch": "^4.0.4",
frandiox marked this conversation as resolved.
Show resolved Hide resolved
"mime": "^3.0.0",
"minimatch": "^3.0.4",
"okie": "^1.0.1",
"open": "^8.4.0",
"periscopic": "^2.0.3",
Expand Down
11 changes: 8 additions & 3 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ModuleNode } from './moduleGraph'
import { Update } from 'types/hmrPayload'
import { CLIENT_DIR } from '../constants'
import { RollupError } from 'rollup'
import match from 'minimatch'
import { isMatch } from 'micromatch'
import { Server } from 'http'
import { isCSSRequest } from '../plugins/css'
import { performance } from 'perf_hooks'
Expand Down Expand Up @@ -50,7 +50,9 @@ export async function handleHMRUpdate(
const isConfigDependency = config.configFileDependencies.some(
(name) => file === path.resolve(name)
)
const isEnv = config.inlineConfig.envFile !== false && (file === '.env' || file.startsWith('.env.'))
const isEnv =
config.inlineConfig.envFile !== false &&
(file === '.env' || file.startsWith('.env.'))
if (isConfig || isConfigDependency || isEnv) {
// auto restart server
debugHmr(`[config change] ${chalk.dim(shortFile)}`)
Expand Down Expand Up @@ -189,7 +191,10 @@ export async function handleFileAddUnlink(
for (const i in server._globImporters) {
const { module, importGlobs } = server._globImporters[i]
for (const { base, pattern } of importGlobs) {
if (match(file, pattern) || match(path.relative(base, file), pattern)) {
if (
isMatch(file, pattern) ||
isMatch(path.relative(base, file), pattern)
) {
modules.push(module)
// We use `onFileChange` to invalidate `module.file` so that subsequent `ssrLoadModule()`
// calls get fresh glob import results with(out) the newly added(/removed) `file`.
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
slash,
isFileReadable
} from '../../utils'
import match from 'minimatch'
import { isMatch } from 'micromatch'

const sirvOptions: Options = {
dev: true,
Expand Down Expand Up @@ -149,7 +149,7 @@ export function isFileServingAllowed(
const cleanedUrl = cleanUrl(url)
const file = ensureLeadingSlash(normalizePath(cleanedUrl))

if (server.config.server.fs.deny.some((i) => match(file, i, _matchOptions)))
if (server.config.server.fs.deny.some((i) => isMatch(file, i, _matchOptions)))
return false

if (server.moduleGraph.safeModulesPath.has(file)) return true
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/types/commonjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
export interface RollupCommonJSOptions {
/**
* A minimatch pattern, or array of patterns, which specifies the files in
* A picomatch pattern, or array of patterns, which specifies the files in
* the build the plugin should operate on. By default, all files with
* extension `".cjs"` or those in `extensions` are included, but you can narrow
* this list by only including specific files. These files will be analyzed
Expand All @@ -17,7 +17,7 @@ export interface RollupCommonJSOptions {
*/
include?: string | RegExp | readonly (string | RegExp)[]
/**
* A minimatch pattern, or array of patterns, which specifies the files in
* A picomatch pattern, or array of patterns, which specifies the files in
* the build the plugin should _ignore_. By default, all files with
* extensions other than those in `extensions` or `".cjs"` are ignored, but you
* can exclude additional files. See also the `include` option.
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/types/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ declare module 'rollup-plugin-web-worker-loader' {
export default p
}

declare module 'minimatch' {
function match(
declare module 'micromatch' {
export function isMatch(
path: string,
pattern: string,
options?: { matchBase?: boolean }
): boolean
export default match
}

declare module 'compression' {
Expand Down
16 changes: 14 additions & 2 deletions pnpm-lock.yaml

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