Skip to content

Commit 3848da5

Browse files
authored
Merge pull request #5 from vanduynslagerp/fix-watched-matching
Fix watched file filter to match glob
2 parents 153bb90 + 5c97767 commit 3848da5

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"chokidar": "^1.7.0",
1111
"lodash": "^4.17.4",
12+
"minimatch": "^3.0.4",
1213
"nodeify": "^1.0.1",
1314
"source-map-url": "^0.4.0"
1415
},

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {merge} from 'lodash';
33
import {FSWatcher} from 'chokidar';
44
import nodeify from 'nodeify';
55
import sourceMappingURL from 'source-map-url';
6+
import minimatch from 'minimatch';
67
import postcss from 'postcss';
78

89
/**
@@ -65,7 +66,9 @@ function createPostcssPreprocessor(args, config, logger, server) {
6566
.then(result => {
6667
if (
6768
config.autoWatch &&
68-
config.files.find(configFile => configFile.pattern === file.originalPath && configFile.watched)
69+
config.files.find(
70+
configFile => configFile.watched && minimatch(file.originalPath, configFile.pattern, {dot: true})
71+
)
6972
) {
7073
const fullPath = path.resolve(file.originalPath);
7174
const includedFiles = [];

test/integration.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ test('Re-compile css file when dependency is modified', async t => {
6363
copy('test/fixtures/with-partial.css', fixture),
6464
]);
6565

66-
const server = await watch([fixture, 'test/fixtures/styles.test.js'], {
67-
options: {plugins: [atImport({path: [includePath]}), mixins, simpleVars, cssnano]},
68-
});
66+
const server = await watch(
67+
[fixture.replace('fixtures', '*').replace('with', '+(with|nomatch)'), 'test/fixtures/styles.test.js'],
68+
{options: {plugins: [atImport({path: [includePath]}), mixins, simpleVars, cssnano]}}
69+
);
6970

7071
try {
7172
let {success, error, disconnected} = await waitForRunComplete(server);

test/unit.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ test('Add dependency to watcher', async t => {
194194
t.true(add.calledOnce);
195195
});
196196

197+
test('Add dependency to watcher for file added with glob', async t => {
198+
const fixture = 'test/fixtures/with-partial.css';
199+
const glob = 'test/*/+(with|nomatch)*+(partial|nomatch).css';
200+
const partial = path.resolve('test/fixtures/partials/partial.css');
201+
const subPartial = path.resolve('test/fixtures/partials/sub-partial.css');
202+
const options = {plugins: [atImport({path: ['test/fixtures/partials']}), mixins, simpleVars, cssnano]};
203+
const {preprocessor, add, debug} = mockPreprocessor(
204+
{},
205+
{files: [{pattern: glob, watched: true}], autoWatch: true, postcssPreprocessor: {options}}
206+
);
207+
const file = {originalPath: fixture};
208+
209+
await preprocessor(await readFile(fixture), file);
210+
t.true(debug.secondCall.calledWith(match('Watching'), partial));
211+
t.true(debug.thirdCall.calledWith(match('Watching'), subPartial));
212+
t.true(add.firstCall.calledWith(match.array.deepEquals([partial, subPartial])));
213+
t.true(add.calledOnce);
214+
});
215+
197216
test('Do not add dependency to watcher if parent is not watched', async t => {
198217
const fixture = 'test/fixtures/with-partial.css';
199218
const options = {plugins: [atImport({path: ['test/fixtures/partials']}), mixins, simpleVars, cssnano]};

0 commit comments

Comments
 (0)