Skip to content

Commit

Permalink
fix: cache import lister for incremental builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
bingnz committed Apr 8, 2016
1 parent 175836b commit 814baa5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ module gulpLessChanged {
changed: boolean
}

let listerCache: { [item: string]: ImportLister } = {};

export function run(options?: gulpLessChanged.PluginOptions) {
options = options || {};
let getOutputFileName = options.getOutputFileName || (input => gutil.replaceExtension(input, '.css'));
let importLister = new ImportLister(options.paths);

let pathsKey = options.paths ? options.paths.join('-') : '';
let importLister = listerCache[pathsKey];
if (!importLister) {
importLister = listerCache[pathsKey] = new ImportLister(options.paths);
}

function transform(file: File, enc: string, callback: (error: any, data: any) => any) {

if (file.isNull()) {
Expand Down
1 change: 0 additions & 1 deletion src/path-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module pathResolver {
fsAsync.statAsync(path)
.then((stat: fs.Stats) => Promise.resolve(path))
.catch((error: any) => {
console.log(error);
return <string>null;
}))
.then(paths => {
Expand Down
94 changes: 94 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,100 @@ describe('gulp-less-changed', () => {
done();
}));
});

it('should reuse import import lister for same paths', done => {
let fs = new FakeFs();
let date = new Date();
const path1 = 'path1';
const path2 = 'path/2/';

let listImports = {
listImports: function() {
return Promise.resolve([]);
}
};
let importLister = {
ImportLister: function() {
return listImports;
}
};

sinon.spy(importLister, 'ImportLister');

fs.file('main.css', { mtime: date });

let lessChanged = getLessChanged({ fs: fs, listImports: importLister });

let fakeFile1 = new File({ path: 'main.less', stat: { mtime: date }, contents: new Buffer('@import \'import.less\';') });
let fakeFile2 = new File({ path: 'main2.less', stat: { mtime: date }, contents: new Buffer('@import \'import.less\';') });

let lessChangedStream = lessChanged({ paths: [path1, path2] });

lessChangedStream.write(fakeFile1);
lessChangedStream.end();

lessChangedStream
.pipe(streamAssert.end(() => {
let lessChangedStream2 = lessChanged({ paths: [path1, path2] });

lessChangedStream2.write(fakeFile2);
lessChangedStream2.end();

lessChangedStream2
.pipe(streamAssert.end(() => {
expect(importLister.ImportLister).to.have.been.called.once;
done();
}));
}));
});

it('should create new import import lister for different paths', done => {
let fs = new FakeFs();
let date = new Date();
const path1 = 'path1';
const path2 = 'path/2/';

let listImports = {
listImports: function() {
return Promise.resolve([]);
}
};
let importLister = {
ImportLister: function() {
return listImports;
}
};

sinon.spy(importLister, 'ImportLister');

fs.file('main.css', { mtime: date });

let lessChanged = getLessChanged({ fs: fs, listImports: importLister });

let fakeFile1 = new File({ path: 'main.less', stat: { mtime: date }, contents: new Buffer('@import \'import.less\';') });
let fakeFile2 = new File({ path: 'main2.less', stat: { mtime: date }, contents: new Buffer('@import \'import.less\';') });

let lessChangedStream = lessChanged({ paths: [path1, path2] });

lessChangedStream.write(fakeFile1);
lessChangedStream.end();

lessChangedStream
.pipe(streamAssert.end(() => {
let lessChangedStream2 = lessChanged();

lessChangedStream2.write(fakeFile2);
lessChangedStream2.end();

lessChangedStream2
.pipe(streamAssert.end(() => {
expect(importLister.ImportLister).to.have.been.calledWith([path1, path2]);
expect(importLister.ImportLister).to.have.been.calledWith(sinon.match.falsy);
expect(importLister.ImportLister).to.have.been.called.twice;
done();
}));
}));
});
});

describe('when there is an error processing an import', () => {
Expand Down

0 comments on commit 814baa5

Please sign in to comment.