Skip to content

Commit c5c424a

Browse files
authored
feat: expose createFilter util (#8562)
1 parent d357e33 commit c5c424a

20 files changed

+55
-27
lines changed

docs/guide/api-plugin.md

+4
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ normalizePath('foo\\bar') // 'foo/bar'
517517
normalizePath('foo/bar') // 'foo/bar'
518518
```
519519
520+
## Filtering, include/exclude pattern
521+
522+
Vite exposes [`@rollup/pluginutils`'s `createFilter`](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) function to encourage Vite specific plugins and integrations to use the standard include/exclude filtering pattern, which is also used in Vite core itself.
523+
520524
## Client-server Communication
521525

522526
Since Vite 2.9, we provide some utilities for plugins to help handle the communication with clients.

packages/plugin-react/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"@babel/plugin-transform-react-jsx-development": "^7.16.7",
4545
"@babel/plugin-transform-react-jsx-self": "^7.17.12",
4646
"@babel/plugin-transform-react-jsx-source": "^7.16.7",
47-
"@rollup/pluginutils": "^4.2.1",
4847
"react-refresh": "^0.13.0"
4948
},
5049
"peerDependencies": {

packages/plugin-react/src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import path from 'path'
22
import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
33
import * as babel from '@babel/core'
4-
import { createFilter } from '@rollup/pluginutils'
5-
import { normalizePath } from 'vite'
4+
import { createFilter, normalizePath } from 'vite'
65
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
76
import {
87
addRefreshWrapper,

packages/plugin-vue-jsx/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@babel/core": "^7.18.2",
3939
"@babel/plugin-syntax-import-meta": "^7.10.4",
4040
"@babel/plugin-transform-typescript": "^7.18.4",
41-
"@rollup/pluginutils": "^4.2.1",
4241
"@vue/babel-plugin-jsx": "^1.1.1"
4342
},
4443
"devDependencies": {

packages/plugin-vue-jsx/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as babel from '@babel/core'
55
import jsx from '@vue/babel-plugin-jsx'
66
// @ts-expect-error missing type
77
import importMeta from '@babel/plugin-syntax-import-meta'
8-
import { createFilter, normalizePath } from '@rollup/pluginutils'
8+
import { createFilter, normalizePath } from 'vite'
99
import type { ComponentOptions } from 'vue'
1010
import type { Plugin } from 'vite'
1111
import type { Options } from './types'

packages/plugin-vue-jsx/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { VueJSXPluginOptions } from '@vue/babel-plugin-jsx'
2-
import type { FilterPattern } from '@rollup/pluginutils'
2+
import type { FilterPattern } from 'vite'
33

44
export interface FilterOptions {
55
include?: FilterPattern

packages/plugin-vue/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,5 @@
4747
"source-map": "^0.6.1",
4848
"vite": "workspace:*",
4949
"vue": "^3.2.37"
50-
},
51-
"dependencies": {
52-
"@rollup/pluginutils": "^4.2.1"
5350
}
5451
}

packages/plugin-vue/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs'
22
import type { Plugin, ViteDevServer } from 'vite'
3-
import { createFilter } from '@rollup/pluginutils'
3+
import { createFilter } from 'vite'
44
/* eslint-disable import/no-duplicates */
55
import type {
66
SFCBlock,

packages/plugin-vue/src/main.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import path from 'path'
22
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
33
import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
4-
import { normalizePath } from '@rollup/pluginutils'
54
import type { RawSourceMap } from 'source-map'
65
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
76
import { TraceMap, eachMapping } from '@jridgewell/trace-mapping'
87
import type { EncodedSourceMap as GenEncodedSourceMap } from '@jridgewell/gen-mapping'
98
import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping'
10-
import { transformWithEsbuild } from 'vite'
9+
import { normalizePath, transformWithEsbuild } from 'vite'
1110
import {
1211
createDescriptor,
1312
getPrevDescriptor,

packages/vite/LICENSE.md

+22
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,28 @@ License: MIT
535535
By: Rich Harris
536536
Repository: rollup/plugins
537537

538+
> The MIT License (MIT)
539+
>
540+
> Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
541+
>
542+
> Permission is hereby granted, free of charge, to any person obtaining a copy
543+
> of this software and associated documentation files (the "Software"), to deal
544+
> in the Software without restriction, including without limitation the rights
545+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
546+
> copies of the Software, and to permit persons to whom the Software is
547+
> furnished to do so, subject to the following conditions:
548+
>
549+
> The above copyright notice and this permission notice shall be included in
550+
> all copies or substantial portions of the Software.
551+
>
552+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
553+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
554+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
555+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
556+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
557+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
558+
> THE SOFTWARE.
559+
538560
---------------------------------------
539561

540562
## @vue/compiler-core

packages/vite/rollup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function createCjsConfig(isProduction: boolean) {
208208
...Object.keys(pkg.dependencies),
209209
...(isProduction ? [] : Object.keys(pkg.devDependencies))
210210
],
211-
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(55)]
211+
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(120)]
212212
})
213213
}
214214

packages/vite/src/node/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { performance } from 'perf_hooks'
55
import { createRequire } from 'module'
66
import colors from 'picocolors'
77
import type { Alias, AliasOptions } from 'types/alias'
8-
import { createFilter } from '@rollup/pluginutils'
98
import aliasPlugin from '@rollup/plugin-alias'
109
import { build } from 'esbuild'
1110
import type { RollupOptions } from 'rollup'
@@ -19,6 +18,7 @@ import { resolvePreviewOptions } from './preview'
1918
import type { CSSOptions } from './plugins/css'
2019
import {
2120
createDebugger,
21+
createFilter,
2222
dynamicImport,
2323
isExternalUrl,
2424
isObject,

packages/vite/src/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { resolvePackageData } from './packages'
1010
export * from './publicUtils'
1111

1212
// additional types
13+
export type { FilterPattern } from './utils'
1314
export type { CorsOptions, CorsOrigin, CommonServerOptions } from './http'
1415
export type {
1516
ViteDevServer,

packages/vite/src/node/packages.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import { createFilter } from '@rollup/pluginutils'
4-
import { createDebugger, resolveFrom } from './utils'
3+
import { createDebugger, createFilter, resolveFrom } from './utils'
54
import type { ResolvedConfig } from './config'
65
import type { Plugin } from './plugin'
76

packages/vite/src/node/plugins/dynamicImportVars.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import MagicString from 'magic-string'
33
import { init, parse as parseImports } from 'es-module-lexer'
44
import type { ImportSpecifier } from 'es-module-lexer'
55
import { parse as parseJS } from 'acorn'
6-
import { createFilter } from '@rollup/pluginutils'
76
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
87
import type { Plugin } from '../plugin'
98
import type { ResolvedConfig } from '../config'
109
import {
10+
createFilter,
1111
normalizePath,
1212
parseRequest,
1313
requestQuerySplitRE,

packages/vite/src/node/plugins/esbuild.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import type {
99
import { transform } from 'esbuild'
1010
import type { RawSourceMap } from '@ampproject/remapping'
1111
import type { SourceMap } from 'rollup'
12-
import { createFilter } from '@rollup/pluginutils'
1312
import type { TSConfckParseOptions, TSConfckParseResult } from 'tsconfck'
1413
import { TSConfckParseError, findAll, parse } from 'tsconfck'
1514
import {
1615
cleanUrl,
1716
combineSourcemaps,
1817
createDebugger,
18+
createFilter,
1919
ensureWatchedFile,
2020
generateCodeFrame,
2121
toUpperCaseDriveLetter

packages/vite/src/node/publicUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export {
88
splitVendorChunkPlugin,
99
splitVendorChunk
1010
} from './plugins/splitVendorChunk'
11-
export { normalizePath, mergeConfig, mergeAlias } from './utils'
11+
export { normalizePath, mergeConfig, mergeAlias, createFilter } from './utils'
1212
export { send } from './server/send'
1313
export { createLogger } from './logger'
1414
export { searchForWorkspaceRoot } from './server/searchRoot'

packages/vite/src/node/ssr/ssrExternal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import fs from 'fs'
22
import path from 'path'
33
import { createRequire } from 'module'
4-
import { createFilter } from '@rollup/pluginutils'
54
import type { InternalResolveOptions } from '../plugins/resolve'
65
import { tryNodeResolve } from '../plugins/resolve'
76
import {
87
bareImportRE,
98
createDebugger,
9+
createFilter,
1010
isBuiltin,
1111
isDefined,
1212
lookupFile,

packages/vite/src/node/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { Alias, AliasOptions } from 'types/alias'
1616
import type MagicString from 'magic-string'
1717

1818
import type { TransformResult } from 'rollup'
19+
import { createFilter as _createFilter } from '@rollup/pluginutils'
1920
import {
2021
CLIENT_ENTRY,
2122
CLIENT_PUBLIC_PATH,
@@ -26,6 +27,20 @@ import {
2627
} from './constants'
2728
import type { ResolvedConfig } from '.'
2829

30+
/**
31+
* Inlined to keep `@rollup/pluginutils` in devDependencies
32+
*/
33+
export type FilterPattern =
34+
| ReadonlyArray<string | RegExp>
35+
| string
36+
| RegExp
37+
| null
38+
export const createFilter = _createFilter as (
39+
include?: FilterPattern,
40+
exclude?: FilterPattern,
41+
options?: { resolve?: string | false | null }
42+
) => (id: string | unknown) => boolean
43+
2944
export function slash(p: string): string {
3045
return p.replace(/\\/g, '/')
3146
}

pnpm-lock.yaml

+1-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)