Unverified Commit 53a0a0fb authored by Sukka's avatar Sukka Committed by GitHub

chore: replace micromatch w/ picomatch (#60699)

The PR continues from #58038 aiming to reduce the installation size
further, and **improve performance in doing so**.

Most of `micromatch`'s APIs are essentially wrappers/alias of
`picomatch`, supplemented with additional features facilitated by a
transitive dependency `braces`. However, Next.js doesn't use those
"extra features". Switching from `micromatch` to `picomatch` can avoid
introducing the transitive dependency `braces`.

The PR also improves some globs' performance by hoisting.

- `micromatch.makeRe` is just an alias of `picomatch.makeRe`
-
https://github.com/micromatch/micromatch/blob/90dc0cd3e186b8ab4dd05bfc41e2b98f5067a49b/index.js#L387C30-L387C30
- `micromatch.isMatch(str, patterns, options)` equals
`picomatch(patterns, options)(str)`
-
https://github.com/micromatch/micromatch/blob/90dc0cd3e186b8ab4dd05bfc41e2b98f5067a49b/index.js#L123
- `micromatch.matcher` is just an alias of `picomatch`
-
https://github.com/micromatch/micromatch/blob/90dc0cd3e186b8ab4dd05bfc41e2b98f5067a49b/index.js#L104C1-L104C40

---------
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 4a744921
......@@ -177,8 +177,8 @@
"@types/lodash": "4.14.198",
"@types/lodash.curry": "4.1.6",
"@types/lru-cache": "5.1.0",
"@types/micromatch": "4.0.2",
"@types/path-to-regexp": "1.7.0",
"@types/picomatch": "2.3.3",
"@types/platform": "1.3.4",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.15",
......@@ -250,7 +250,6 @@
"loader-utils3": "npm:loader-utils@3.1.3",
"lodash.curry": "4.1.1",
"lru-cache": "5.1.1",
"micromatch": "4.0.4",
"mini-css-extract-plugin": "2.4.3",
"msw": "1.3.0",
"nanoid": "3.1.32",
......@@ -262,6 +261,7 @@
"p-limit": "3.1.0",
"path-browserify": "1.0.1",
"path-to-regexp": "6.1.0",
"picomatch": "3.0.1",
"platform": "1.3.6",
"postcss-flexbugs-fixes": "5.0.2",
"postcss-modules-extract-imports": "3.0.0",
......
......@@ -3,7 +3,7 @@ import type { Middleware, RouteHas } from '../../lib/load-custom-routes'
import { promises as fs } from 'fs'
import LRUCache from 'next/dist/compiled/lru-cache'
import { matcher } from 'next/dist/compiled/micromatch'
import picomatch from 'next/dist/compiled/picomatch'
import type { ServerRuntime } from 'next/types'
import {
extractExportedConstValue,
......@@ -395,7 +395,7 @@ function getMiddlewareConfig(
: [config.unstable_allowDynamic]
for (const glob of result.unstable_allowDynamicGlobs ?? []) {
try {
matcher(glob)
picomatch(glob)
} catch (err) {
throw new Error(
`${pageFilePath} exported 'config.unstable_allowDynamic' contains invalid pattern '${glob}': ${
......
......@@ -23,7 +23,7 @@ import { loadBindings } from './swc'
import { nonNullable } from '../lib/non-nullable'
import * as ciEnvironment from '../telemetry/ci-info'
import debugOriginal from 'next/dist/compiled/debug'
import { isMatch } from 'next/dist/compiled/micromatch'
import picomatch from 'next/dist/compiled/picomatch'
import { defaultOverrides } from '../server/require-hook'
import { nodeFileTrace } from 'next/dist/compiled/@vercel/nft'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
......@@ -273,22 +273,27 @@ export async function collectBuildTraces({
const additionalIgnores = new Set<string>()
for (const glob of excludeGlobKeys) {
if (isMatch('next-server', glob)) {
if (picomatch(glob)('next-server')) {
outputFileTracingExcludes[glob].forEach((exclude) => {
additionalIgnores.add(exclude)
})
}
}
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}
return isMatch(pathname, ignores, {
const makeIgnoreFn = (ignores: string[]) => {
// pre compile the ignore globs
const isMatch = picomatch(ignores, {
contains: true,
dot: true,
})
return (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}
return isMatch(pathname)
}
}
const sharedIgnores = [
......@@ -699,7 +704,8 @@ export async function collectBuildTraces({
const combinedIncludes = new Set<string>()
const combinedExcludes = new Set<string>()
for (const curGlob of includeGlobKeys) {
if (isMatch(route, [curGlob], { dot: true, contains: true })) {
const isMatch = picomatch(curGlob, { dot: true, contains: true })
if (isMatch(route)) {
for (const include of outputFileTracingIncludes[curGlob]) {
combinedIncludes.add(include.replace(/\\/g, '/'))
}
......@@ -707,7 +713,8 @@ export async function collectBuildTraces({
}
for (const curGlob of excludeGlobKeys) {
if (isMatch(route, [curGlob], { dot: true, contains: true })) {
const isMatch = picomatch(curGlob, { dot: true, contains: true })
if (isMatch(route)) {
for (const exclude of outputFileTracingExcludes[curGlob]) {
combinedExcludes.add(exclude)
}
......@@ -750,13 +757,15 @@ export async function collectBuildTraces({
const resolvedGlobs = [...combinedExcludes].map((exclude) =>
path.join(dir, exclude)
)
// pre compile before forEach
const isMatch = picomatch(resolvedGlobs, {
dot: true,
contains: true,
})
combined.forEach((file) => {
if (
isMatch(path.join(pageDir, file), resolvedGlobs, {
dot: true,
contains: true,
})
) {
if (isMatch(path.join(pageDir, file))) {
combined.delete(file)
}
})
......
......@@ -11,7 +11,7 @@ import '../lib/setup-exception-listeners'
import { loadEnvConfig, type LoadedEnvFiles } from '@next/env'
import { bold, yellow } from '../lib/picocolors'
import crypto from 'crypto'
import { makeRe } from 'next/dist/compiled/micromatch'
import { makeRe } from 'next/dist/compiled/picomatch'
import { existsSync, promises as fs } from 'fs'
import os from 'os'
import { Worker } from '../lib/worker'
......
......@@ -8,7 +8,7 @@ import { getNamedMiddlewareRegex } from '../../../shared/lib/router/utils/route-
import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { isMatch } from 'next/dist/compiled/micromatch'
import picomatch from 'next/dist/compiled/picomatch'
import path from 'path'
import {
EDGE_RUNTIME_WEBPACK,
......@@ -277,7 +277,8 @@ function isDynamicCodeEvaluationAllowed(
}
const name = fileName.replace(rootDir ?? '', '')
return isMatch(name, middlewareConfig?.unstable_allowDynamicGlobs ?? [])
return picomatch(middlewareConfig?.unstable_allowDynamicGlobs ?? [])(name)
}
function buildUnsupportedApiError({
......
......@@ -15,7 +15,7 @@ import {
} from '../../webpack-config'
import type { NextConfigComplete } from '../../../server/config-shared'
import { loadBindings } from '../../swc'
import { isMatch } from 'next/dist/compiled/micromatch'
import picomatch from 'next/dist/compiled/picomatch'
import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getPageFilePath } from '../../entries'
import { resolveExternal } from '../../handle-externals'
......@@ -462,8 +462,14 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
...this.traceIgnores,
'**/node_modules/**',
]
// pre-compile the ignore matcher to avoid repeating on every ignoreFn call
const isIgnoreMatcher = picomatch(ignores, {
contains: true,
dot: true,
})
const ignoreFn = (path: string) => {
return isMatch(path, ignores, { contains: true, dot: true })
return isIgnoreMatcher(path)
}
await finishModulesSpan
......
This diff is collapsed.
{"name":"micromatch","main":"index.js","author":"Jon Schlinkert (https://github.com/jonschlinkert)","license":"MIT"}
The MIT License (MIT)
Copyright (c) 2014-present, Jon Schlinkert.
Copyright (c) 2017-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
......
This diff is collapsed.
{"name":"picomatch","main":"index.js","author":"Jon Schlinkert (https://github.com/jonschlinkert)","license":"MIT"}
import type { RemotePattern } from './image-config'
import { makeRe } from 'next/dist/compiled/micromatch'
import { makeRe } from 'next/dist/compiled/picomatch'
export function matchRemotePattern(pattern: RemotePattern, url: URL): boolean {
if (pattern.protocol !== undefined) {
......
......@@ -2045,12 +2045,12 @@ export async function ncc_webpack_sources3(task, opts) {
}
// eslint-disable-next-line camelcase
externals['micromatch'] = 'next/dist/compiled/micromatch'
externals['picomatch'] = 'next/dist/compiled/picomatch'
export async function ncc_minimatch(task, opts) {
await task
.source(relative(__dirname, require.resolve('micromatch')))
.ncc({ packageName: 'micromatch', externals })
.target('src/compiled/micromatch')
.source(relative(__dirname, require.resolve('picomatch')))
.ncc({ packageName: 'picomatch', externals })
.target('src/compiled/picomatch')
}
// eslint-disable-next-line camelcase
......
......@@ -272,8 +272,8 @@ declare module 'next/dist/compiled/lru-cache' {
import m from 'lru-cache'
export = m
}
declare module 'next/dist/compiled/micromatch' {
import m from 'micromatch'
declare module 'next/dist/compiled/picomatch' {
import m from 'picomatch'
export = m
}
declare module 'next/dist/compiled/nanoid/index.cjs' {
......
......@@ -1025,12 +1025,12 @@ importers:
'@types/lru-cache':
specifier: 5.1.0
version: 5.1.0
'@types/micromatch':
specifier: 4.0.2
version: 4.0.2
'@types/path-to-regexp':
specifier: 1.7.0
version: 1.7.0
'@types/picomatch':
specifier: 2.3.3
version: 2.3.3
'@types/platform':
specifier: 1.3.4
version: 1.3.4
......@@ -1244,9 +1244,6 @@ importers:
lru-cache:
specifier: 5.1.1
version: 5.1.1
micromatch:
specifier: 4.0.4
version: 4.0.4
mini-css-extract-plugin:
specifier: 2.4.3
version: 2.4.3(webpack@5.86.0)
......@@ -1280,6 +1277,9 @@ importers:
path-to-regexp:
specifier: 6.1.0
version: 6.1.0
picomatch:
specifier: 3.0.1
version: 3.0.1
platform:
specifier: 1.3.6
version: 1.3.6
......@@ -6744,10 +6744,6 @@ packages:
'@types/node': 20.2.5
dev: true
/@types/braces@3.0.1:
resolution: {integrity: sha512-+euflG6ygo4bn0JHtn4pYqcXwRtLvElQ7/nnjDu7iYG56H0+OhCd7d6Ug0IE3WcFpZozBKW2+80FUbv5QGk5AQ==}
dev: true
/@types/btoa-lite@1.0.0:
resolution: {integrity: sha512-wJsiX1tosQ+J5+bY5LrSahHxr2wT+uME5UDwdN1kg4frt40euqA+wzECkmq4t5QbveHiJepfdThgQrPw6KiSlg==}
dev: true
......@@ -7007,12 +7003,6 @@ packages:
/@types/mdx@2.0.3:
resolution: {integrity: sha512-IgHxcT3RC8LzFLhKwP3gbMPeaK7BM9eBH46OdapPA7yvuIUJ8H6zHZV53J8hGZcTSnt95jANt+rTBNUUc22ACQ==}
/@types/micromatch@4.0.2:
resolution: {integrity: sha512-oqXqVb0ci19GtH0vOA/U2TmHTcRY9kuZl4mqUxe0QmJAlIW13kzhuK5pi1i9+ngav8FjpSb9FVS/GE00GLX1VA==}
dependencies:
'@types/braces': 3.0.1
dev: true
/@types/mime@2.0.1:
resolution: {integrity: sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==}
dev: true
......@@ -7062,6 +7052,10 @@ packages:
path-to-regexp: 6.1.0
dev: true
/@types/picomatch@2.3.3:
resolution: {integrity: sha512-Yll76ZHikRFCyz/pffKGjrCwe/le2CDwOP5F210KQo27kpRE46U2rDnzikNlVn6/ezH3Mhn46bJMTfeVTtcYMg==}
dev: true
/@types/platform@1.3.4:
resolution: {integrity: sha512-U0o4K+GNiK0PNxoDwd8xRnvLVe4kzei6opn3/FCjAriqaP+rfrDdSl1kP/hLL6Y3/Y3hhGnBwD4dCkkAqs1W/Q==}
dev: true
......@@ -17690,7 +17684,7 @@ packages:
engines: {node: '>=8.6'}
dependencies:
braces: 3.0.2
picomatch: 2.2.3
picomatch: 2.3.1
dev: true
/micromatch@4.0.5:
......@@ -19316,15 +19310,15 @@ packages:
/picocolors@1.0.0:
resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==}
/picomatch@2.2.3:
resolution: {integrity: sha512-KpELjfwcCDUb9PeigTs2mBJzXUPzAuP2oPcA989He8Rte0+YUAjw1JVedDhuTKPkHjSYzMN3npC9luThGYEKdg==}
engines: {node: '>=8.6'}
dev: true
/picomatch@2.3.1:
resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==}
engines: {node: '>=8.6'}
/picomatch@3.0.1:
resolution: {integrity: sha512-I3EurrIQMlRc9IaAZnqRR044Phh2DXY+55o7uJ0V+hYZAcQYSuFWsc9q5PvyDHUSCe1Qxn/iBz+78s86zWnGag==}
engines: {node: '>=10'}
dev: true
/pidtree@0.3.0:
resolution: {integrity: sha512-9CT4NFlDcosssyg8KVFltgokyKZIFjoBxw8CTGy+5F38Y1eQWrt8tRayiUOXE+zVKQnYu5BR8JjCtvK3BcnBhg==}
engines: {node: '>=0.10'}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment