Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/taro-vite-runner/src/utils/createFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ export default function createFilter(include, exclude, options?) {
const pathId = normalizePath(id)
for (let i = 0; i < excludeMatchers.length; ++i) {
const matcher = excludeMatchers[i]
if (matcher instanceof RegExp) matcher.lastIndex = 0
if (matcher.test(pathId)) {
return false
}
}
for (let i = 0; i < includeMatchers.length; ++i) {
const matcher = includeMatchers[i]
if (matcher instanceof RegExp) matcher.lastIndex = 0
if (matcher.test(pathId)) {
return true
}
Expand Down
44 changes: 44 additions & 0 deletions packages/taro-vite-runner/tests/utils/createFilter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, test } from 'vitest'

import createFilter, { createFilterWithCompileOptions } from '../../src/utils/createFilter'

describe('createFilter', () => {
test.each(['g', 'y', 'gy'])('repeated include matching with /%s is deterministic', flags => {
const filter = createFilter([new RegExp('.*\\.tsx$', flags)], [])
expect([filter('/src/page.tsx'), filter('/src/page.tsx'), filter('/src/other.tsx')]).toEqual([true, true, true])
expect(filter('/src/page.css')).toBe(false)
})

test.each(['g', 'y', 'gy'])('repeated exclude matching with /%s is deterministic', flags => {
const filter = createFilter([], [new RegExp('.*\\.tsx$', flags)])
expect([filter('/src/page.tsx'), filter('/src/page.tsx'), filter('/src/other.tsx')]).toEqual([false, false, false])
expect(filter('/src/page.css')).toBe(true)
})

test('ignores an existing regex cursor and keeps exclusions ahead of inclusions', () => {
const include = /.*\.tsx$/g
const exclude = /.*excluded\.tsx$/y
include.lastIndex = 100
exclude.lastIndex = 100
const filter = createFilter([include], [exclude])
expect(filter('/src/excluded.tsx')).toBe(false)
expect(filter('/src/page.tsx')).toBe(true)
expect(filter('/src/excluded.tsx')).toBe(false)
})

test('applies stateful compile include/exclude rules consistently', () => {
const filter = createFilterWithCompileOptions({ include: [/.*\.tsx$/g], exclude: [/.*vendor.*/g] })
expect(filter('/src/page.tsx')).toBe(true)
expect(filter('/src/page.tsx')).toBe(true)
expect(filter('/vendor/page.tsx')).toBe(false)
expect(filter('/vendor/page.tsx')).toBe(false)
})

test('preserves glob matching and Windows path normalization', () => {
const filter = createFilter(['**/*.tsx'], ['**/vendor/**'])
expect(filter('C:\\src\\page.tsx')).toBe(true)
expect(filter('C:\\src\\vendor\\page.tsx')).toBe(false)
expect(filter('/src/page.css')).toBe(false)
expect(filter(null)).toBe(false)
})
})
Loading