diff --git a/lib/ignored-paths.js b/lib/ignored-paths.js index 0d9152495eca..e5ba8450ff9e 100644 --- a/lib/ignored-paths.js +++ b/lib/ignored-paths.js @@ -37,7 +37,6 @@ const DEFAULT_OPTIONS = { cwd: process.cwd() }; - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -80,6 +79,7 @@ class IgnoredPaths { */ constructor(options) { options = mergeDefaultOptions(options); + this.cache = {}; /** * add pattern to node-ignore instance @@ -91,17 +91,29 @@ class IgnoredPaths { return ig.addPattern(pattern); } + /** + * read ignore filepath + * @param {string} filepath, file to add to ig + * @returns {array} raw ignore rules + */ + function readIgnoreFile(filepath) { + return fs.readFileSync(filepath, "utf8"); + } + /** * add ignore file to node-ignore instance * @param {Object} ig, instance of node-ignore * @param {string} filepath, file to add to ig + * @param {string} cache, file object to add to ig * @returns {array} raw ignore rules */ - function addIgnoreFile(ig, filepath) { + function addIgnoreFile(ig, filepath, cache) { ig.ignoreFiles.push(filepath); - return ig.add(fs.readFileSync(filepath, "utf8")); + return ig.add(cache[filepath]); } + + this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []); this.baseDir = options.cwd; @@ -155,8 +167,11 @@ class IgnoredPaths { if (ignorePath) { debug(`Adding ${ignorePath}`); this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath)); - addIgnoreFile(this.ig.custom, ignorePath); - addIgnoreFile(this.ig.default, ignorePath); + if (!this.cache[ignorePath]) { + this.cache[ignorePath] = readIgnoreFile(ignorePath); + } + addIgnoreFile(this.ig.custom, ignorePath, this.cache); + addIgnoreFile(this.ig.default, ignorePath, this.cache); } if (options.ignorePattern) {