Skip to content

Commit 3903193

Browse files
authored
refactor: use util functions from @rolldown/pluginutils (#166)
1 parent 9933779 commit 3903193

File tree

13 files changed

+59
-37
lines changed

13 files changed

+59
-37
lines changed

packages/vite/LICENSE.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,38 @@ Repository: lukeed/polka
360360

361361
---------------------------------------
362362

363+
## @rolldown/pluginutils
364+
License: MIT
365+
Repository: git+https://github.com/rolldown/rolldown.git
366+
367+
> MIT License
368+
>
369+
> Copyright (c) 2024-present VoidZero Inc. & Contributors
370+
>
371+
> Permission is hereby granted, free of charge, to any person obtaining a copy
372+
> of this software and associated documentation files (the "Software"), to deal
373+
> in the Software without restriction, including without limitation the rights
374+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
375+
> copies of the Software, and to permit persons to whom the Software is
376+
> furnished to do so, subject to the following conditions:
377+
>
378+
> The above copyright notice and this permission notice shall be included in all
379+
> copies or substantial portions of the Software.
380+
>
381+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
382+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
383+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
384+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
385+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
386+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
387+
> SOFTWARE.
388+
>
389+
> end of terms and conditions
390+
>
391+
> The licenses of externally maintained libraries from which parts of the Software is derived are listed [here](https://github.com/rolldown/rolldown/blob/main/THIRD-PARTY-LICENSE).
392+
393+
---------------------------------------
394+
363395
## @rollup/plugin-alias, @rollup/plugin-dynamic-import-vars, @rollup/pluginutils
364396
License: MIT
365397
By: Johannes Stein

packages/vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"@jridgewell/trace-mapping": "^0.3.25",
103103
"@oxc-project/types": "0.69.0",
104104
"@polka/compression": "^1.0.0-next.25",
105+
"@rolldown/pluginutils": "1.0.0-beta.8-commit.8951737",
105106
"@rollup/plugin-alias": "^5.1.1",
106107
"@rollup/plugin-commonjs": "^28.0.3",
107108
"@rollup/plugin-dynamic-import-vars": "2.1.4",

packages/vite/src/node/optimizer/rolldownDepPlugin.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import type { ImportKind, Plugin, RolldownPlugin } from 'rolldown'
3+
import { prefixRegex } from '@rolldown/pluginutils'
34
import { JS_TYPES_RE, KNOWN_ASSET_TYPES } from '../constants'
45
import type { PackageCache } from '../packages'
56
import {
@@ -189,7 +190,7 @@ export function rolldownDepPlugin(
189190
},
190191
load: {
191192
filter: {
192-
id: new RegExp(`^${externalWithConversionNamespace}`),
193+
id: prefixRegex(externalWithConversionNamespace),
193194
},
194195
handler(id) {
195196
const path = id.slice(externalWithConversionNamespace.length)
@@ -246,8 +247,8 @@ export function rolldownDepPlugin(
246247
load: {
247248
filter: {
248249
id: [
249-
new RegExp(`^${browserExternalNamespace}`),
250-
new RegExp(`^${optionalPeerDepNamespace}`),
250+
prefixRegex(browserExternalNamespace),
251+
prefixRegex(optionalPeerDepNamespace),
251252
],
252253
},
253254
handler(id) {
@@ -324,7 +325,7 @@ export function rolldownCjsExternalPlugin(
324325
return {
325326
name: 'cjs-external',
326327
resolveId: {
327-
filter: { id: [new RegExp(`^${nonFacadePrefix}`), filter] },
328+
filter: { id: [prefixRegex(nonFacadePrefix), filter] },
328329
handler(id, _importer, options) {
329330
if (id.startsWith(nonFacadePrefix)) {
330331
return {
@@ -350,7 +351,7 @@ export function rolldownCjsExternalPlugin(
350351
},
351352
},
352353
load: {
353-
filter: { id: [new RegExp(`^${cjsExternalFacadeNamespace}`)] },
354+
filter: { id: prefixRegex(cjsExternalFacadeNamespace) },
354355
handler(id) {
355356
if (id.startsWith(cjsExternalFacadeNamespace)) {
356357
return {

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
import MagicString from 'magic-string'
1111
import colors from 'picocolors'
1212
import picomatch from 'picomatch'
13+
import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'
1314
import {
1415
createToImportMetaURLBasedRelativeRuntime,
1516
toOutputFilePathInJS,
@@ -162,10 +163,8 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
162163
id: [
163164
urlRE,
164165
DEFAULT_ASSETS_RE,
165-
...config.rawAssetsInclude.map((v) =>
166-
typeof v === 'string'
167-
? picomatch.makeRe(`${v}{?*,}`, { dot: true })
168-
: addQueryToRegex(v),
166+
...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude).map((v) =>
167+
typeof v === 'string' ? picomatch.makeRe(v, { dot: true }) : v,
169168
),
170169
],
171170
},
@@ -189,9 +188,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
189188
rawRE,
190189
urlRE,
191190
DEFAULT_ASSETS_RE,
192-
...config.rawAssetsInclude.map((v) =>
193-
typeof v === 'string' ? `${v}{?*,}` : addQueryToRegex(v),
194-
),
191+
...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude),
195192
],
196193
// Rollup convention, this id should be handled by the
197194
// plugin that marked it with \0
@@ -307,14 +304,6 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
307304
}
308305
}
309306

310-
function addQueryToRegex(input: RegExp) {
311-
return new RegExp(
312-
// replace `$` with `(?:\?.*)?$` (ignore `\$`)
313-
input.source.replace(/(?<!\\)\$/g, '(?:\\?.*)?$'),
314-
input.flags,
315-
)
316-
}
317-
318307
export async function fileToUrl(
319308
pluginContext: PluginContext,
320309
id: string,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path'
22
import MagicString from 'magic-string'
33
import { stripLiteral } from 'strip-literal'
4+
import { exactRegex } from '@rolldown/pluginutils'
45
import type { Plugin } from '../plugin'
56
import type { ResolvedConfig } from '../config'
67
import {
@@ -52,7 +53,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
5253
transform: {
5354
filter: {
5455
id: {
55-
exclude: [preloadHelperId, CLIENT_ENTRY],
56+
exclude: [exactRegex(preloadHelperId), exactRegex(CLIENT_ENTRY)],
5657
},
5758
code: /new\s+URL.+import\.meta\.url/,
5859
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { init, parse as parseImports } from 'es-module-lexer'
44
import type { ImportSpecifier } from 'es-module-lexer'
55
import { parseAst } from 'rolldown/parseAst'
66
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
7+
import { exactRegex } from '@rolldown/pluginutils'
78
import type { Plugin } from '../plugin'
89
import type { ResolvedConfig } from '../config'
910
import { CLIENT_ENTRY } from '../constants'
1011
import { createBackCompatIdResolver } from '../idResolver'
1112
import {
1213
createFilter,
13-
exactRegex,
1414
normalizePath,
1515
rawRE,
1616
requestQueryMaybeEscapedSplitRE,
@@ -201,7 +201,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
201201

202202
transform: {
203203
filter: {
204-
id: { exclude: CLIENT_ENTRY },
204+
id: { exclude: exactRegex(CLIENT_ENTRY) },
205205
code: hasDynamicImportRE,
206206
},
207207
async handler(source, importer) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import type { SourceMap } from 'rolldown'
99
import { buildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental'
1010
import type { RawSourceMap } from '@ampproject/remapping'
1111
import convertSourceMap from 'convert-source-map'
12+
import { exactRegex } from '@rolldown/pluginutils'
1213
import {
1314
combineSourcemaps,
14-
exactRegex,
1515
generateCodeFrame,
1616
isInNodeModules,
1717
numberToPos,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { exactRegex } from '@rolldown/pluginutils'
12
import type { ResolvedConfig } from '..'
23
import type { Plugin } from '../plugin'
3-
import { exactRegex } from '../utils'
44
import { isModernFlag } from './importAnalysisBuild'
55

66
export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { rolldown } from 'rolldown'
1111
import type { FSWatcher } from 'dep-types/chokidar'
1212
import { TSConfckParseError } from 'tsconfck'
1313
import colors from 'picocolors'
14+
import { exactRegex, prefixRegex } from '@rolldown/pluginutils'
1415
import {
1516
combineSourcemaps,
1617
createFilter,
@@ -318,7 +319,7 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
318319
},
319320
resolveId: {
320321
filter: {
321-
id: /^@oxc-project\/runtime\//,
322+
id: prefixRegex('@oxc-project/runtime/'),
322323
},
323324
async handler(id, _importer, opts) {
324325
// @oxc-project/runtime imports will be injected by OXC transform
@@ -501,11 +502,11 @@ async function generateRuntimeHelpers(
501502
{
502503
name: 'entrypoint',
503504
resolveId: {
504-
filter: { id: /^entrypoint$/ },
505+
filter: { id: exactRegex('entrypoint') },
505506
handler: (id) => id,
506507
},
507508
load: {
508-
filter: { id: /^entrypoint$/ },
509+
filter: { id: exactRegex('entrypoint') },
509510
handler() {
510511
return runtimeHelpers
511512
.map(

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { PartialResolvedId } from 'rolldown'
66
import { viteResolvePlugin } from 'rolldown/experimental'
77
import { exports, imports } from 'resolve.exports'
88
import { hasESMSyntax } from 'mlly'
9+
import { prefixRegex } from '@rolldown/pluginutils'
910
import type { Plugin } from '../plugin'
1011
import {
1112
CLIENT_ENTRY,
@@ -750,10 +751,7 @@ export function resolvePlugin(
750751

751752
load: {
752753
filter: {
753-
id: [
754-
new RegExp(`^${browserExternalId}`),
755-
new RegExp(`^${optionalPeerDepId}`),
756-
],
754+
id: [prefixRegex(browserExternalId), prefixRegex(optionalPeerDepId)],
757755
},
758756
handler(id) {
759757
if (id.startsWith(browserExternalId)) {

0 commit comments

Comments
 (0)