Skip to content

Commit f4999af

Browse files
authored
feat(babel): add custom filter option (#767)
* feat(babel): add custom filter option * fix(babel): change type according to code review * feat(babel): can not pass include or exclude and custom filter together
1 parent 7e1be64 commit f4999af

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

packages/babel/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ Type: `String | RegExp | Array[...String|RegExp]`<br>
9292

9393
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there.
9494

95+
### `filter`
96+
97+
Type: (id: string) => boolean<br>
98+
99+
Custom [filter function](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) can be used to determine whether or not certain modules should be operated upon.
100+
101+
Usage:
102+
103+
```js
104+
import { createFilter } from '@rollup/pluginutils';
105+
const include = 'include/**.js';
106+
const exclude = 'exclude/**.js';
107+
const filter = createFilter(include, exclude, {});
108+
```
109+
95110
### `extensions`
96111

97112
Type: `Array[...String]`<br>

packages/babel/src/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,27 @@ function createBabelInputPluginFactory(customCallback = returnObject) {
122122
let exclude;
123123
let include;
124124
let extensions;
125+
let customFilter;
125126

126127
({
127128
exclude,
128129
extensions,
129130
babelHelpers,
130131
include,
132+
filter: customFilter,
131133
skipPreflightCheck,
132134
...babelOptions
133135
} = unpackInputPluginOptions(pluginOptionsWithOverrides, this.meta.rollupVersion));
134136

135137
const extensionRegExp = new RegExp(
136138
`(${extensions.map(escapeRegExpCharacters).join('|')})$`
137139
);
138-
const includeExcludeFilter = createFilter(include, exclude);
139-
filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && includeExcludeFilter(id);
140+
if (customFilter && (include || exclude)) {
141+
throw new Error('Could not handle include or exclude with custom filter together');
142+
}
143+
const userDefinedFilter =
144+
typeof customFilter === 'function' ? customFilter : createFilter(include, exclude);
145+
filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && userDefinedFilter(id);
140146

141147
return null;
142148
},

packages/babel/test/as-input-plugin.js

+60
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { rollup } from 'rollup';
66
import { SourceMapConsumer } from 'source-map';
77
import jsonPlugin from '@rollup/plugin-json';
88
import nodeResolvePlugin from '@rollup/plugin-node-resolve';
9+
import { createFilter } from '@rollup/pluginutils';
910

1011
import { getCode } from '../../../util/test';
1112

@@ -101,6 +102,65 @@ console.log("the answer is ".concat(foo()));
101102
);
102103
});
103104

105+
test('does not babelify excluded code with custom filter', async (t) => {
106+
const filter = createFilter([], '**/foo.js');
107+
const code = await generate('fixtures/exclusions/main.js', { filter });
108+
// eslint-disable-next-line no-template-curly-in-string
109+
t.false(code.includes('${foo()}'));
110+
t.true(code.includes('=> 42'));
111+
t.is(
112+
code,
113+
`'use strict';
114+
115+
const foo = () => 42;
116+
117+
console.log("the answer is ".concat(foo()));
118+
`
119+
);
120+
});
121+
122+
test('does babelify included code with custom filter', async (t) => {
123+
const filter = createFilter('**/foo.js', [], {
124+
resolve: __dirname
125+
});
126+
const code = await generate('fixtures/exclusions/main.js', { filter });
127+
// eslint-disable-next-line no-template-curly-in-string
128+
t.true(code.includes('${foo()}'));
129+
t.false(code.includes('=> 42'));
130+
t.is(
131+
code,
132+
`'use strict';
133+
134+
var foo = function foo() {
135+
return 42;
136+
};
137+
138+
console.log(\`the answer is \${foo()}\`);
139+
`
140+
);
141+
});
142+
143+
test('can not pass include or exclude when custom filter specified', async (t) => {
144+
const filter = createFilter('**/foo.js', [], {
145+
resolve: __dirname
146+
});
147+
let errorWithExclude = '';
148+
try {
149+
await generate('fixtures/exclusions/main.js', { filter, exclude: [] });
150+
} catch (e) {
151+
errorWithExclude = e.message;
152+
}
153+
t.true(!!errorWithExclude);
154+
155+
let errorWithInclude = '';
156+
try {
157+
await generate('fixtures/exclusions/main.js', { filter, include: [] });
158+
} catch (e) {
159+
errorWithInclude = e.message;
160+
}
161+
t.true(!!errorWithInclude);
162+
});
163+
104164
test('generates sourcemap by default', async (t) => {
105165
const bundle = await rollup({
106166
input: 'fixtures/class/main.js',

packages/babel/types/index.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Plugin, PluginContext, TransformPluginContext } from 'rollup';
2-
import { FilterPattern } from '@rollup/pluginutils';
2+
import { FilterPattern, CreateFilter } from '@rollup/pluginutils';
33
import * as babelCore from '@babel/core';
44

55
export interface RollupBabelInputPluginOptions
@@ -14,6 +14,16 @@ export interface RollupBabelInputPluginOptions
1414
* @default undefined;
1515
*/
1616
exclude?: FilterPattern;
17+
/**
18+
* Custom filter function can be used to determine whether or not certain modules should be operated upon.
19+
* Example:
20+
* import { createFilter } from '@rollup/pluginutils';
21+
* const include = 'include/**.js';
22+
* const exclude = 'exclude/**.js';
23+
* const filter = createFilter(include, exclude, {});
24+
* @default undefined;
25+
*/
26+
filter?: ReturnType<CreateFilter>;
1727
/**
1828
* An array of file extensions that Babel should transpile. If you want to transpile TypeScript files with this plugin it's essential to include .ts and .tsx in this option.
1929
* @default ['.js', '.jsx', '.es6', '.es', '.mjs']

0 commit comments

Comments
 (0)