From 78799b0bf61a1fad7eaf316c347178a6745e5d35 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Dec 2019 11:06:18 +0000 Subject: [PATCH] fix(box): always ignore node_modules folder of themes --- lib/box/index.js | 10 ++++++--- lib/hexo/default_config.js | 3 +-- test/scripts/box/box.js | 43 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/box/index.js b/lib/box/index.js index 2f71e9cc46..2f0a468212 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -8,6 +8,7 @@ const fs = require('hexo-fs'); const chalk = require('chalk'); const { EventEmitter } = require('events'); const micromatch = require('micromatch'); +const { ignore } = require('../hexo/default_config'); const defaultPattern = new Pattern(() => ({})); @@ -29,11 +30,14 @@ function Box(ctx, base, options) { this.watcher = null; this.Cache = ctx.model('Cache'); this.File = this._createFileClass(); - this.ignore = ctx.config.ignore; + + let ignoreCfg = ignore; if (ctx.config.ignore) { - const targets = Array.isArray(ctx.config.ignore) ? ctx.config.ignore : [ctx.config.ignore]; - this.options.ignored = (this.options.ignored || []).concat(targets.map(s => toRegExp(ctx, s)).filter(x => x)); + if (ctx.config.ignore.length) ignoreCfg = ignoreCfg.concat(ctx.config.ignore); } + this.ignore = ignoreCfg; + const targets = ignoreCfg; + this.options.ignored = (this.options.ignored || []).concat(targets.map(s => toRegExp(ctx, s)).filter(x => x)); } require('util').inherits(Box, EventEmitter); diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index e1cd17d470..a2da406830 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -65,9 +65,8 @@ module.exports = { deploy: {}, // ignore files from processing - ignore: [], + ignore: ['**/themes/*/node_modules/**'], // Category & Tag meta_generator: true }; - diff --git a/test/scripts/box/box.js b/test/scripts/box/box.js index 3292b6eac3..846c49df83 100644 --- a/test/scripts/box/box.js +++ b/test/scripts/box/box.js @@ -264,7 +264,7 @@ describe('Box', () => { }); it('process() - skip files if they match a glob epression in ignore', () => { - const box = newBox('test', {ignore: '**/ignore_me'}); + const box = newBox('test', { ignore: '**/ignore_me' }); const data = {}; box.addProcessor(file => { @@ -283,7 +283,7 @@ describe('Box', () => { }); it('process() - skip files if they match any of the glob expressions in ignore', () => { - const box = newBox('test', {ignore: ['**/ignore_me', '**/ignore_me_too.txt']}); + const box = newBox('test', { ignore: ['**/ignore_me', '**/ignore_me_too.txt'] }); const data = {}; box.addProcessor(file => { @@ -302,6 +302,45 @@ describe('Box', () => { }).finally(() => fs.rmdir(box.base)); }); + it('process() - skip node_modules of theme by default', () => { + const box = newBox('test', { ignore: null }); + const data = {}; + + box.addProcessor(file => { + data[file.path] = file; + }); + + return Promise.all([ + fs.writeFile(pathFn.join(box.base, 'foo.txt'), 'foo'), + fs.writeFile(pathFn.join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes') + ]).then(() => box.process()).then(() => { + const keys = Object.keys(data); + + keys.length.should.eql(1); + keys[0].should.eql('foo.txt'); + }).finally(() => fs.rmdir(box.base)); + }); + + it('process() - always skip node_modules of theme', () => { + const box = newBox('test', { ignore: '**/ignore_me' }); + const data = {}; + + box.addProcessor(file => { + data[file.path] = file; + }); + + return Promise.all([ + fs.writeFile(pathFn.join(box.base, 'foo.txt'), 'foo'), + fs.writeFile(pathFn.join(box.base, 'ignore_me', 'bar.txt'), 'ignore_me'), + fs.writeFile(pathFn.join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes') + ]).then(() => box.process()).then(() => { + const keys = Object.keys(data); + + keys.length.should.eql(1); + keys[0].should.eql('foo.txt'); + }).finally(() => fs.rmdir(box.base)); + }); + it('watch() - create', () => { const box = newBox('test'); const path = 'a.txt';