Skip to content

Commit 4c52153

Browse files
EslamHikohiroppy
authored andcommitted
exclude /node_modules/ from _watch by default (#1794)
1 parent 7f3b8c9 commit 4c52153

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

lib/Server.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ class Server {
115115

116116
this.sockets = [];
117117

118-
this.watchOptions = options.watchOptions || {};
118+
if (!options.watchOptions) {
119+
options.watchOptions = {};
120+
}
121+
// ignoring node_modules folder by default
122+
options.watchOptions.ignored = options.watchOptions.ignored || [
123+
/node_modules/,
124+
];
125+
this.watchOptions = options.watchOptions;
126+
119127
this.contentBaseWatchers = [];
120128
// Replace leading and trailing slashes to normalize path
121129
this.sockPath = `/${

test/ContentBase.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,81 @@ describe('ContentBase', () => {
6464
}, 1000);
6565
});
6666
});
67+
68+
describe('test ignoring node_modules folder by Default', () => {
69+
jest.setTimeout(30000);
70+
71+
beforeAll((done) => {
72+
server = helper.start(config, {
73+
contentBase: contentBasePublic,
74+
watchContentBase: true,
75+
});
76+
// making sure that chokidar has read all the files
77+
server.contentBaseWatchers[0].on('ready', () => {
78+
done();
79+
});
80+
req = request(server.app);
81+
});
82+
83+
afterAll((done) => {
84+
helper.close(() => {
85+
done();
86+
});
87+
});
88+
89+
it('Should ignore node_modules & watch bar', (done) => {
90+
const watchedPaths = server.contentBaseWatchers[0].getWatched();
91+
// check if node_modules folder is not in watched list
92+
const folderWatched = !!watchedPaths[
93+
path.join(contentBasePublic, 'node_modules')
94+
];
95+
expect(folderWatched).toEqual(false);
96+
// check if bar folder is in watched list
97+
expect(watchedPaths[path.join(contentBasePublic, 'bar')]).toEqual([
98+
'index.html',
99+
]);
100+
101+
done();
102+
});
103+
});
104+
105+
describe('test not ignoring node_modules folder', () => {
106+
jest.setTimeout(30000);
107+
108+
beforeAll((done) => {
109+
server = helper.start(config, {
110+
contentBase: contentBasePublic,
111+
watchContentBase: true,
112+
watchOptions: {
113+
ignored: /bar/,
114+
},
115+
});
116+
// making sure that chokidar has read all the files
117+
server.contentBaseWatchers[0].on('ready', () => {
118+
done();
119+
});
120+
req = request(server.app);
121+
});
122+
123+
afterAll((done) => {
124+
helper.close(() => {
125+
done();
126+
});
127+
});
128+
129+
it('Should watch node_modules & ignore bar', (done) => {
130+
const watchedPaths = server.contentBaseWatchers[0].getWatched();
131+
// check if node_modules folder is in watched list
132+
expect(
133+
watchedPaths[path.join(contentBasePublic, 'node_modules')]
134+
).toEqual(['index.html']);
135+
// check if bar folder is not in watched list
136+
const folderWatched = !!watchedPaths[path.join(contentBasePublic, 'bar')];
137+
expect(folderWatched).toEqual(false);
138+
done();
139+
});
140+
});
141+
67142
describe('test listing files in folders without index.html using the option serveIndex:false', () => {
68143
beforeAll((done) => {
69144
server = helper.start(

test/fixtures/contentbase-config/public/node_modules/index.html

Whitespace-only changes.

0 commit comments

Comments
 (0)