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

feat: expose createFilter util #8562

Merged
merged 3 commits into from Jun 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case, Every plugin using needs to bump peerDeps after Vite's release.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already requiring this for all internal plugins for v3, no? If not, yes, we should.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are requiring ^3.0.0-alpha which means 3.0.0-alpha.1 can be used.
But now we need to require ^3.0.0-alpha.11 (next version). (since createFilter is not exported in Vite 3.0.0-alpha.10, current version)

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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question: How much leeway do you give the bundle size limit?

})
}

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

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