Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 02a060d

Browse filesBrowse files
authored
Correct the behavior when multiple transform filter options are specified (#5909)
* Correct the behavior when multiple transform filter options are specified * Refactor "return foo ? false : true" * Remove unnecessary ensureArray
1 parent 051a502 commit 02a060d
Copy full SHA for 02a060d

File tree

Expand file treeCollapse file tree

2 files changed

+13
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-23
lines changed

‎src/utils/pluginFilter.ts

Copy file name to clipboardExpand all lines: src/utils/pluginFilter.ts
+11-21Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import type { StringFilter, StringOrRegExp } from '../rollup/types';
33
import { ensureArray } from './ensureArray';
44
import { isAbsolute, normalize, resolve } from './path';
55

6-
const FALLBACK_TRUE = 1;
7-
const FALLBACK_FALSE = 0;
8-
type FallbackValues = typeof FALLBACK_TRUE | typeof FALLBACK_FALSE;
9-
10-
type PluginFilterWithFallback = (input: string) => boolean | FallbackValues;
11-
126
export type PluginFilter = (input: string) => boolean;
137
export type TransformHookFilter = (id: string, code: string) => boolean;
148

@@ -58,7 +52,7 @@ function patternToCodeFilter(pattern: StringOrRegExp): PluginFilter {
5852
function createFilter(
5953
exclude: PluginFilter[] | undefined,
6054
include: PluginFilter[] | undefined
61-
): PluginFilterWithFallback | undefined {
55+
): PluginFilter | undefined {
6256
if (!exclude && !include) {
6357
return;
6458
}
@@ -70,7 +64,7 @@ function createFilter(
7064
if (include?.some(filter => filter(input))) {
7165
return true;
7266
}
73-
return !!include && include.length > 0 ? FALLBACK_FALSE : FALLBACK_TRUE;
67+
return !(include && include.length > 0);
7468
};
7569
}
7670

@@ -82,7 +76,7 @@ function normalizeFilter(filter: StringFilter): NormalizedStringFilter {
8276
}
8377
if (Array.isArray(filter)) {
8478
return {
85-
include: ensureArray(filter)
79+
include: filter
8680
};
8781
}
8882
return {
@@ -91,15 +85,15 @@ function normalizeFilter(filter: StringFilter): NormalizedStringFilter {
9185
};
9286
}
9387

94-
function createIdFilter(filter: StringFilter | undefined): PluginFilterWithFallback | undefined {
88+
function createIdFilter(filter: StringFilter | undefined): PluginFilter | undefined {
9589
if (!filter) return;
9690
const { exclude, include } = normalizeFilter(filter);
9791
const excludeFilter = exclude?.map(patternToIdFilter);
9892
const includeFilter = include?.map(patternToIdFilter);
9993
return createFilter(excludeFilter, includeFilter);
10094
}
10195

102-
function createCodeFilter(filter: StringFilter | undefined): PluginFilterWithFallback | undefined {
96+
function createCodeFilter(filter: StringFilter | undefined): PluginFilter | undefined {
10397
if (!filter) return;
10498
const { exclude, include } = normalizeFilter(filter);
10599
const excludeFilter = exclude?.map(patternToCodeFilter);
@@ -122,18 +116,14 @@ export function createFilterForTransform(
122116
return (id, code) => {
123117
let fallback = true;
124118
if (idFilterFunction) {
125-
const idResult = idFilterFunction(id);
126-
if (typeof idResult === 'boolean') {
127-
return idResult;
128-
}
129-
fallback &&= !!idResult;
119+
fallback &&= idFilterFunction(id);
130120
}
121+
if (!fallback) {
122+
return false;
123+
}
124+
131125
if (codeFilterFunction) {
132-
const codeResult = codeFilterFunction(code);
133-
if (typeof codeResult === 'boolean') {
134-
return codeResult;
135-
}
136-
fallback &&= !!codeResult;
126+
fallback &&= codeFilterFunction(code);
137127
}
138128
return fallback;
139129
};

‎test/function/samples/plugin-hook-filters/_config.js

Copy file name to clipboardExpand all lines: test/function/samples/plugin-hook-filters/_config.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const expectedCalledHooks = {
7676
'transform-{ code: { include: [ /import\\.\\w+\\.a/ ] } }',
7777
"transform-{ code: { include: 'import.meta.a', exclude: 'import.meta.b' } }",
7878
"transform-{ id: { exclude: '**/ba*.js' }, code: 'import.meta.a' }",
79-
"transform-{\n id: { include: '**/foo.js', exclude: '**/ba*.js' },\n code: 'import.meta.b'\n}"
79+
"transform-{\n id: { include: '**/foo.js', exclude: '**/ba*.js' },\n code: 'import.meta.a'\n}"
8080
]
8181
}
8282
};
@@ -116,7 +116,7 @@ addPlugin('transform', { code: { include: /import\.meta\.\w+/, exclude: /import\
116116
addPlugin('transform', { id: { exclude: '**/ba*.js' }, code: 'import.meta.a' });
117117
addPlugin('transform', {
118118
id: { include: '**/foo.js', exclude: '**/ba*.js' },
119-
code: 'import.meta.b'
119+
code: 'import.meta.a'
120120
});
121121

122122
function addPlugin(hook, filter) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.