From c1e1ddaba5d7a84b3a72ce4533634c0a4c938ae8 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Fri, 12 Apr 2019 15:51:41 +0200 Subject: [PATCH 1/2] exclude /node_modules/ from _watch by default --- lib/Server.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Server.js b/lib/Server.js index 1f94a2a19c..8bf2a529ab 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -115,7 +115,14 @@ class Server { this.sockets = []; - this.watchOptions = options.watchOptions || {}; + if (!options.watchOptions) { + options.watchOptions = {}; + } + // ignoring node_modules folder by default + options.watchOptions.ignored = + options.watchOptions.ignored || /node_modules/; + this.watchOptions = options.watchOptions; + this.contentBaseWatchers = []; // Replace leading and trailing slashes to normalize path this.sockPath = `/${ From a684a887be0e249b19ef9a3a7ca6814362749842 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Fri, 12 Apr 2019 17:22:08 +0200 Subject: [PATCH 2/2] add tests --- lib/Server.js | 5 +- test/ContentBase.test.js | 75 +++++++++++++++++++ .../public/node_modules/index.html | 0 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/contentbase-config/public/node_modules/index.html diff --git a/lib/Server.js b/lib/Server.js index 8bf2a529ab..b0f787faec 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -119,8 +119,9 @@ class Server { options.watchOptions = {}; } // ignoring node_modules folder by default - options.watchOptions.ignored = - options.watchOptions.ignored || /node_modules/; + options.watchOptions.ignored = options.watchOptions.ignored || [ + /node_modules/, + ]; this.watchOptions = options.watchOptions; this.contentBaseWatchers = []; diff --git a/test/ContentBase.test.js b/test/ContentBase.test.js index 0854fbd3d4..bf743aac7c 100644 --- a/test/ContentBase.test.js +++ b/test/ContentBase.test.js @@ -64,6 +64,81 @@ describe('ContentBase', () => { }, 1000); }); }); + + describe('test ignoring node_modules folder by Default', () => { + jest.setTimeout(30000); + + beforeAll((done) => { + server = helper.start(config, { + contentBase: contentBasePublic, + watchContentBase: true, + }); + // making sure that chokidar has read all the files + server.contentBaseWatchers[0].on('ready', () => { + done(); + }); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + done(); + }); + }); + + it('Should ignore node_modules & watch bar', (done) => { + const watchedPaths = server.contentBaseWatchers[0].getWatched(); + // check if node_modules folder is not in watched list + const folderWatched = !!watchedPaths[ + path.join(contentBasePublic, 'node_modules') + ]; + expect(folderWatched).toEqual(false); + // check if bar folder is in watched list + expect(watchedPaths[path.join(contentBasePublic, 'bar')]).toEqual([ + 'index.html', + ]); + + done(); + }); + }); + + describe('test not ignoring node_modules folder', () => { + jest.setTimeout(30000); + + beforeAll((done) => { + server = helper.start(config, { + contentBase: contentBasePublic, + watchContentBase: true, + watchOptions: { + ignored: /bar/, + }, + }); + // making sure that chokidar has read all the files + server.contentBaseWatchers[0].on('ready', () => { + done(); + }); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + done(); + }); + }); + + it('Should watch node_modules & ignore bar', (done) => { + const watchedPaths = server.contentBaseWatchers[0].getWatched(); + // check if node_modules folder is in watched list + expect( + watchedPaths[path.join(contentBasePublic, 'node_modules')] + ).toEqual(['index.html']); + // check if bar folder is not in watched list + const folderWatched = !!watchedPaths[path.join(contentBasePublic, 'bar')]; + expect(folderWatched).toEqual(false); + done(); + }); + }); + describe('test listing files in folders without index.html using the option serveIndex:false', () => { beforeAll((done) => { server = helper.start( diff --git a/test/fixtures/contentbase-config/public/node_modules/index.html b/test/fixtures/contentbase-config/public/node_modules/index.html new file mode 100644 index 0000000000..e69de29bb2