Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: expose createFilter util (#8562)
  • Loading branch information
patak-dev committed Jun 13, 2022
1 parent d357e33 commit c5c424a
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 27 deletions.
4 changes: 4 additions & 0 deletions docs/guide/api-plugin.md
Expand Up @@ -517,6 +517,10 @@ normalizePath('foo\\bar') // 'foo/bar'
normalizePath('foo/bar') // 'foo/bar'
```
## Filtering, include/exclude pattern
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.
## Client-server Communication
Since Vite 2.9, we provide some utilities for plugins to help handle the communication with clients.
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-react/package.json
Expand Up @@ -44,7 +44,6 @@
"@babel/plugin-transform-react-jsx-development": "^7.16.7",
"@babel/plugin-transform-react-jsx-self": "^7.17.12",
"@babel/plugin-transform-react-jsx-source": "^7.16.7",
"@rollup/pluginutils": "^4.2.1",
"react-refresh": "^0.13.0"
},
"peerDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-react/src/index.ts
@@ -1,8 +1,7 @@
import path from 'path'
import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
import * as babel from '@babel/core'
import { createFilter } from '@rollup/pluginutils'
import { normalizePath } from 'vite'
import { createFilter, normalizePath } from 'vite'
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
import {
addRefreshWrapper,
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-vue-jsx/package.json
Expand Up @@ -38,7 +38,6 @@
"@babel/core": "^7.18.2",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-typescript": "^7.18.4",
"@rollup/pluginutils": "^4.2.1",
"@vue/babel-plugin-jsx": "^1.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/src/index.ts
Expand Up @@ -5,7 +5,7 @@ import * as babel from '@babel/core'
import jsx from '@vue/babel-plugin-jsx'
// @ts-expect-error missing type
import importMeta from '@babel/plugin-syntax-import-meta'
import { createFilter, normalizePath } from '@rollup/pluginutils'
import { createFilter, normalizePath } from 'vite'
import type { ComponentOptions } from 'vue'
import type { Plugin } from 'vite'
import type { Options } from './types'
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/src/types.ts
@@ -1,5 +1,5 @@
import type { VueJSXPluginOptions } from '@vue/babel-plugin-jsx'
import type { FilterPattern } from '@rollup/pluginutils'
import type { FilterPattern } from 'vite'

export interface FilterOptions {
include?: FilterPattern
Expand Down
3 changes: 0 additions & 3 deletions packages/plugin-vue/package.json
Expand Up @@ -47,8 +47,5 @@
"source-map": "^0.6.1",
"vite": "workspace:*",
"vue": "^3.2.37"
},
"dependencies": {
"@rollup/pluginutils": "^4.2.1"
}
}
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/index.ts
@@ -1,6 +1,6 @@
import fs from 'fs'
import type { Plugin, ViteDevServer } from 'vite'
import { createFilter } from '@rollup/pluginutils'
import { createFilter } from 'vite'
/* eslint-disable import/no-duplicates */
import type {
SFCBlock,
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-vue/src/main.ts
@@ -1,13 +1,12 @@
import path from 'path'
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
import { normalizePath } from '@rollup/pluginutils'
import type { RawSourceMap } from 'source-map'
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
import { TraceMap, eachMapping } from '@jridgewell/trace-mapping'
import type { EncodedSourceMap as GenEncodedSourceMap } from '@jridgewell/gen-mapping'
import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping'
import { transformWithEsbuild } from 'vite'
import { normalizePath, transformWithEsbuild } from 'vite'
import {
createDescriptor,
getPrevDescriptor,
Expand Down
22 changes: 22 additions & 0 deletions packages/vite/LICENSE.md
Expand Up @@ -535,6 +535,28 @@ License: MIT
By: Rich Harris
Repository: rollup/plugins

> The MIT License (MIT)
>
> Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
>
> 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.
---------------------------------------

## @vue/compiler-core
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/rollup.config.ts
Expand Up @@ -208,7 +208,7 @@ function createCjsConfig(isProduction: boolean) {
...Object.keys(pkg.dependencies),
...(isProduction ? [] : Object.keys(pkg.devDependencies))
],
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(55)]
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(120)]
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -5,7 +5,6 @@ import { performance } from 'perf_hooks'
import { createRequire } from 'module'
import colors from 'picocolors'
import type { Alias, AliasOptions } from 'types/alias'
import { createFilter } from '@rollup/pluginutils'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'
import type { RollupOptions } from 'rollup'
Expand All @@ -19,6 +18,7 @@ import { resolvePreviewOptions } from './preview'
import type { CSSOptions } from './plugins/css'
import {
createDebugger,
createFilter,
dynamicImport,
isExternalUrl,
isObject,
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Expand Up @@ -10,6 +10,7 @@ export { resolvePackageData } from './packages'
export * from './publicUtils'

// additional types
export type { FilterPattern } from './utils'
export type { CorsOptions, CorsOrigin, CommonServerOptions } from './http'
export type {
ViteDevServer,
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/packages.ts
@@ -1,7 +1,6 @@
import fs from 'fs'
import path from 'path'
import { createFilter } from '@rollup/pluginutils'
import { createDebugger, resolveFrom } from './utils'
import { createDebugger, createFilter, resolveFrom } from './utils'
import type { ResolvedConfig } from './config'
import type { Plugin } from './plugin'

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Expand Up @@ -3,11 +3,11 @@ import MagicString from 'magic-string'
import { init, parse as parseImports } from 'es-module-lexer'
import type { ImportSpecifier } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { createFilter } from '@rollup/pluginutils'
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import {
createFilter,
normalizePath,
parseRequest,
requestQuerySplitRE,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -9,13 +9,13 @@ import type {
import { transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import type { SourceMap } from 'rollup'
import { createFilter } from '@rollup/pluginutils'
import type { TSConfckParseOptions, TSConfckParseResult } from 'tsconfck'
import { TSConfckParseError, findAll, parse } from 'tsconfck'
import {
cleanUrl,
combineSourcemaps,
createDebugger,
createFilter,
ensureWatchedFile,
generateCodeFrame,
toUpperCaseDriveLetter
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/publicUtils.ts
Expand Up @@ -8,7 +8,7 @@ export {
splitVendorChunkPlugin,
splitVendorChunk
} from './plugins/splitVendorChunk'
export { normalizePath, mergeConfig, mergeAlias } from './utils'
export { normalizePath, mergeConfig, mergeAlias, createFilter } from './utils'
export { send } from './server/send'
export { createLogger } from './logger'
export { searchForWorkspaceRoot } from './server/searchRoot'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
@@ -1,12 +1,12 @@
import fs from 'fs'
import path from 'path'
import { createRequire } from 'module'
import { createFilter } from '@rollup/pluginutils'
import type { InternalResolveOptions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import {
bareImportRE,
createDebugger,
createFilter,
isBuiltin,
isDefined,
lookupFile,
Expand Down
15 changes: 15 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -16,6 +16,7 @@ import type { Alias, AliasOptions } from 'types/alias'
import type MagicString from 'magic-string'

import type { TransformResult } from 'rollup'
import { createFilter as _createFilter } from '@rollup/pluginutils'
import {
CLIENT_ENTRY,
CLIENT_PUBLIC_PATH,
Expand All @@ -26,6 +27,20 @@ import {
} from './constants'
import type { ResolvedConfig } from '.'

/**
* Inlined to keep `@rollup/pluginutils` in devDependencies
*/
export type FilterPattern =
| ReadonlyArray<string | RegExp>
| string
| RegExp
| null
export const createFilter = _createFilter as (
include?: FilterPattern,
exclude?: FilterPattern,
options?: { resolve?: string | false | null }
) => (id: string | unknown) => boolean

export function slash(p: string): string {
return p.replace(/\\/g, '/')
}
Expand Down
8 changes: 1 addition & 7 deletions pnpm-lock.yaml
Expand Up @@ -162,7 +162,6 @@ importers:
'@babel/plugin-transform-react-jsx-development': ^7.16.7
'@babel/plugin-transform-react-jsx-self': ^7.17.12
'@babel/plugin-transform-react-jsx-source': ^7.16.7
'@rollup/pluginutils': ^4.2.1
react-refresh: ^0.13.0
vite: workspace:*
dependencies:
Expand All @@ -171,7 +170,6 @@ importers:
'@babel/plugin-transform-react-jsx-development': 7.16.7_@babel+core@7.18.2
'@babel/plugin-transform-react-jsx-self': 7.17.12_@babel+core@7.18.2
'@babel/plugin-transform-react-jsx-source': 7.16.7_@babel+core@7.18.2
'@rollup/pluginutils': 4.2.1
react-refresh: 0.13.0
devDependencies:
vite: link:../vite
Expand All @@ -180,15 +178,12 @@ importers:
specifiers:
'@jridgewell/gen-mapping': ^0.3.1
'@jridgewell/trace-mapping': ^0.3.13
'@rollup/pluginutils': ^4.2.1
debug: ^4.3.4
rollup: ^2.72.1
slash: ^4.0.0
source-map: ^0.6.1
vite: workspace:*
vue: ^3.2.37
dependencies:
'@rollup/pluginutils': 4.2.1
devDependencies:
'@jridgewell/gen-mapping': 0.3.1
'@jridgewell/trace-mapping': 0.3.13
Expand All @@ -204,14 +199,12 @@ importers:
'@babel/core': ^7.18.2
'@babel/plugin-syntax-import-meta': ^7.10.4
'@babel/plugin-transform-typescript': ^7.18.4
'@rollup/pluginutils': ^4.2.1
'@vue/babel-plugin-jsx': ^1.1.1
vite: workspace:*
dependencies:
'@babel/core': 7.18.2
'@babel/plugin-syntax-import-meta': 7.10.4_@babel+core@7.18.2
'@babel/plugin-transform-typescript': 7.18.4_@babel+core@7.18.2
'@rollup/pluginutils': 4.2.1
'@vue/babel-plugin-jsx': 1.1.1_@babel+core@7.18.2
devDependencies:
vite: link:../vite
Expand Down Expand Up @@ -2168,6 +2161,7 @@ packages:
dependencies:
estree-walker: 2.0.2
picomatch: 2.3.1
dev: true

/@rushstack/node-core-library/3.45.5:
resolution: {integrity: sha512-KbN7Hp9vH3bD3YJfv6RnVtzzTAwGYIBl7y2HQLY4WEQqRbvE3LgI78W9l9X+cTAXCX//p0EeoiUYNTFdqJrMZg==}
Expand Down

0 comments on commit c5c424a

Please sign in to comment.