1 | import process from 'node:process';
|
2 | import path from 'node:path';
|
3 | import PluginError from 'plugin-error';
|
4 | import multimatch from 'multimatch';
|
5 | import streamfilter from 'streamfilter';
|
6 | import toAbsoluteGlob from 'to-absolute-glob';
|
7 | import slash from 'slash';
|
8 |
|
9 | export default function plugin(pattern, options = {}) {
|
10 | pattern = typeof pattern === 'string' ? [pattern] : pattern;
|
11 |
|
12 | if (!Array.isArray(pattern) && typeof pattern !== 'function') {
|
13 | throw new PluginError('gulp-filter', '`pattern` should be a string, array, or function');
|
14 | }
|
15 |
|
16 |
|
17 | return streamfilter((file, encoding, callback) => {
|
18 | let match;
|
19 |
|
20 | if (typeof pattern === 'function') {
|
21 | match = pattern(file);
|
22 | } else {
|
23 | const base = path.dirname(file.path);
|
24 |
|
25 | let patterns = pattern.map(pattern => {
|
26 |
|
27 | if (!pattern.includes('/')) {
|
28 | if (pattern[0] === '!') {
|
29 | return '!' + path.resolve(base, pattern.slice(1));
|
30 | }
|
31 |
|
32 | return path.resolve(base, pattern);
|
33 | }
|
34 |
|
35 | pattern = toAbsoluteGlob(pattern, {cwd: file.cwd, root: options.root});
|
36 |
|
37 |
|
38 |
|
39 | if (pattern[0] === '!') {
|
40 | return '!' + path.resolve(pattern.slice(1));
|
41 | }
|
42 |
|
43 | return path.resolve(pattern);
|
44 | });
|
45 |
|
46 | if (process.platform === 'win32') {
|
47 | patterns = patterns.map(pattern => slash(pattern));
|
48 | }
|
49 |
|
50 | match = multimatch(path.resolve(file.cwd, file.path), patterns, options).length > 0;
|
51 | }
|
52 |
|
53 | callback(!match);
|
54 | }, {
|
55 | objectMode: true,
|
56 | passthrough: options.passthrough !== false,
|
57 | restore: options.restore,
|
58 | });
|
59 | }
|