Skip to content

Commit caddfaa

Browse files
authored
Fixes (#3)
* Use custom preprocessor * Fix dependency management
1 parent 5b8daa1 commit caddfaa

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,22 @@ function createPostcssPreprocessor(args, config, logger, server) {
8282
log.debug('Watching "%s"', includedFile);
8383
dependencies[includedFile] = [fullPath];
8484
} else if (dependencies[includedFile].indexOf(fullPath) === -1) {
85-
dependencies[includedFile].push(includedFile);
85+
dependencies[includedFile].push(fullPath);
8686
}
8787
}
8888
}
8989

9090
for (let i = 0, keys = Object.keys(dependencies), {length} = keys; i < length; i++) {
9191
if (includedFiles.indexOf(keys[i]) === -1) {
92-
dependencies[keys[i]].splice(i, 1);
93-
if (!dependencies[keys[i]].length) {
94-
stopWatching.push(keys[i]);
95-
log.debug('Stop watching "%s"', keys[i]);
96-
delete dependencies[keys[i]];
92+
const index = dependencies[keys[i]].indexOf(fullPath);
93+
94+
if (index !== -1) {
95+
dependencies[keys[i]].splice(index, 1);
96+
if (!dependencies[keys[i]].length) {
97+
stopWatching.push(keys[i]);
98+
log.debug('Stop watching "%s"', keys[i]);
99+
delete dependencies[keys[i]];
100+
}
97101
}
98102
}
99103
}

test/unit.test.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ test('Compile css file with sourcemap (options.map)', async t => {
5353
});
5454

5555
test('Compile scss file with sourcemap (options.sourceMap) and custom preprocessor', async t => {
56-
const fixture = 'test/fixtures/basic.css';
56+
const fixture = 'test/fixtures/basic.custom.css';
5757
const options = {plugins: [atImport, mixins, simpleVars, cssnano], sourceMap: true};
5858
const {preprocessor, debug} = mockPreprocessor({options});
5959
const file = {originalPath: fixture};
6060

6161
t.is((await preprocessor(await readFile(fixture), file)).toString(), await compile(fixture, options));
6262
t.true(debug.firstCall.calledWith(match('Processing'), fixture));
63-
t.is(path.resolve(file.path), path.resolve('test/fixtures/basic.css'));
63+
t.is(path.resolve(file.path), path.resolve('test/fixtures/basic.custom.css'));
6464
});
6565

6666
test('Compile scss file with sourcemap (options.map) and custom preprocessor', async t => {
67-
const fixture = 'test/fixtures/basic.css';
67+
const fixture = 'test/fixtures/basic.custom.css';
6868
const options = {plugins: [atImport, mixins, simpleVars, cssnano], map: true};
6969
const {preprocessor, debug} = mockPreprocessor({options});
7070
const file = {originalPath: fixture};
7171

7272
t.is((await preprocessor(await readFile(fixture), file)).toString(), await compile(fixture, options));
7373
t.true(debug.firstCall.calledWith(match('Processing'), fixture));
74-
t.is(path.resolve(file.path), path.resolve('test/fixtures/basic.css'));
74+
t.is(path.resolve(file.path), path.resolve('test/fixtures/basic.custom.css'));
7575
});
7676

7777
test('Compile scss file with partial import', async t => {
@@ -121,7 +121,7 @@ test('Compile css file with custom transformPath', async t => {
121121
});
122122

123123
test('Compile css file with custom transformPath and custom preprocessor', async t => {
124-
const fixture = 'test/fixtures/basic.txt';
124+
const fixture = 'test/fixtures/basic.custom.txt';
125125
const options = {plugins: [atImport, mixins, simpleVars, cssnano]};
126126
const transformPath = spy(filePath => filePath.replace(/\.(txt)$/, '.css').replace('fixtures/', ''));
127127
const {preprocessor, debug} = mockPreprocessor({transformPath, options});
@@ -130,7 +130,7 @@ test('Compile css file with custom transformPath and custom preprocessor', async
130130
t.is((await preprocessor(await readFile(fixture), file)).toString(), await compile(fixture, options));
131131
t.true(debug.firstCall.calledWith(match('Processing'), fixture));
132132
t.true(transformPath.calledOnce);
133-
t.is(path.resolve(file.path), path.resolve('test/basic.css'));
133+
t.is(path.resolve(file.path), path.resolve('test/basic.custom.css'));
134134
});
135135

136136
test('Log error on invalid css file', async t => {
@@ -305,6 +305,46 @@ test('Do not remove dependency from watcher when unreferenced, if another file s
305305
t.true(debug.calledTwice);
306306
});
307307

308+
test('Do not remove dependency from watcher when different files have differents childs', async t => {
309+
const dir = path.resolve(tmp());
310+
const fixture = path.join(dir, 'with-partial.css');
311+
const otherFixture = path.join(dir, 'other-with-partial.css');
312+
const includePath = path.join(dir, 'partials');
313+
const partial = path.join(includePath, 'partial.css');
314+
const partialAlt = path.join(includePath, 'partial-alt.css');
315+
const subPartial = path.join(includePath, 'sub-partial.css');
316+
const options = {plugins: [atImport({path: [includePath]}), mixins, simpleVars, cssnano]};
317+
const {preprocessor, add, debug, unwatch} = mockPreprocessor(
318+
{},
319+
{
320+
autoWatch: true,
321+
files: [{pattern: fixture, watched: true}, {pattern: otherFixture, watched: true}],
322+
postcssPreprocessor: {options},
323+
}
324+
);
325+
const file = {originalPath: fixture};
326+
const otherFile = {originalPath: otherFixture};
327+
328+
await Promise.all([
329+
copy('test/fixtures/partials/partial.css', partial),
330+
copy('test/fixtures/partials/partial.css', partialAlt),
331+
copy('test/fixtures/partials/sub-partial.css', subPartial),
332+
copy('test/fixtures/with-partial.css', fixture),
333+
copy('test/fixtures/with-partial.css', otherFixture),
334+
]);
335+
await outputFile(
336+
fixture,
337+
(await readFile(fixture)).toString().replace(`@import 'partial';`, `@import 'partial-alt';`)
338+
);
339+
await preprocessor(await readFile(fixture), file);
340+
add.reset();
341+
debug.reset();
342+
await preprocessor(await readFile(otherFixture), otherFile);
343+
t.true(add.calledOnce);
344+
t.true(unwatch.notCalled);
345+
t.true(debug.calledTwice);
346+
});
347+
308348
test('Call refreshFiles when dependency is modified', async t => {
309349
const dir = path.resolve(tmp());
310350
const fixture = path.join(dir, 'with-partial.css');
@@ -366,7 +406,6 @@ test('Call refreshFiles when dependency is deleted and added', async t => {
366406
info.reset();
367407
refreshFiles.reset();
368408
await t.throws(preprocessor(await readFile(fixture), file), Error);
369-
370409
const cpy = waitFor(watcher, 'add');
371410

372411
await copy('test/fixtures/partials/partial.css', partial);

0 commit comments

Comments
 (0)