Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
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
14 changes: 7 additions & 7 deletions src/createFilter.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { resolve, sep } from 'path';
import { makeRe } from 'minimatch';
import { Minimatch } from 'minimatch';
import ensureArray from './utils/ensureArray';

export default function createFilter ( include, exclude ) {
include = ensureArray( include ).map( id => resolve( id ) ).map( makeRe );
exclude = ensureArray( exclude ).map( id => resolve( id ) ).map( makeRe );
include = ensureArray( include ).map( id => resolve( id ) ).map( id => new Minimatch(id) );
exclude = ensureArray( exclude ).map( id => resolve( id ) ).map( id => new Minimatch(id) );

return function ( id ) {
var included = !include.length;
id = id.split(sep).join('/');

include.forEach( pattern => {
if ( pattern.test( id ) ) included = true;
include.forEach( minimatch => {
if ( minimatch.match( id ) ) included = true;
});

exclude.forEach( pattern => {
if ( pattern.test( id ) ) included = false;
exclude.forEach( minimatch => {
if ( minimatch.match( id ) ) included = false;
});

return included;
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe( 'rollup-pluginutils', function () {
assert.ok( filter( path.resolve( 'foo/bar' ) ) );
assert.ok( !filter( path.resolve( 'foo/baz' ) ) );
});

it( 'negation patterns', function () {
var filter = createFilter([ 'a/!(b)/c' ]);
assert.ok( filter( path.resolve( 'a/d/c' ) ) );
assert.ok( !filter( path.resolve( 'a/b/c' ) ) );
});
});

describe( 'addExtension', function () {
Expand Down