Skip to content

Commit ba6248f

Browse files
authored
prevent multiple identical compilations via watch startTime (#250)
1 parent f17765f commit ba6248f

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ end_of_line = lf
88
insert_final_newline = true
99
trim_trailing_whitespace = true
1010

11-
[.md]
11+
[*.md]
1212
insert_final_newline = false
1313
trim_trailing_whitespace = false

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ _Note: The `publicPath` property is required, whereas all other options are opti
6464

6565
### headers
6666

67-
Type: `Object`
67+
Type: `Object`
6868
Default: `undefined`
6969

7070
This property allows a user to pass custom HTTP headers on each request. eg.
7171
`{ "X-Custom-Header": "yes" }`
7272

7373
### index
7474

75-
Type: `String`
75+
Type: `String`
7676
Default: `undefined`
7777

7878
"index.html",
@@ -82,21 +82,21 @@ Default: `undefined`
8282

8383
### lazy
8484

85-
Type: `Boolean`
85+
Type: `Boolean`
8686
Default: `undefined`
8787

8888
This option instructs the module to operate in 'lazy' mode, meaning that it won't
8989
recompile when files change, but rather on each request.
9090

9191
### logger
9292

93-
Type: `Object`
93+
Type: `Object`
9494
Default: [`webpack-log`](https://github.com/webpack-contrib/webpack-log/blob/master/index.js)
9595

9696
In the rare event that a user would like to provide a custom logging interface,
9797
this property allows the user to assign one. The module leverages
9898
[`webpack-log`](https://github.com/webpack-contrib/webpack-log#readme)
99-
for creating the [`loglevelnext`](https://github.com/shellscape/loglevelnext#readme)
99+
for creating the [`loglevelnext`](https://github.com/shellscape/loglevelnext#readme)
100100
logging management by default. Any custom logger must adhere to the same
101101
exports for compatibility. Specifically, all custom loggers must have the
102102
following exported methods at a minimum:
@@ -111,7 +111,7 @@ Please see the documentation for `loglevel` for more information.
111111

112112
### logLevel
113113

114-
Type: `String`
114+
Type: `String`
115115
Default: `'info'`
116116

117117
This property defines the level of messages that the module will log. Valid levels
@@ -131,15 +131,15 @@ for logging management, and more information can be found on its page.
131131

132132
### logTime
133133

134-
Type: `Boolean`
134+
Type: `Boolean`
135135
Default: `false`
136136

137137
If `true` the log output of the module will be prefixed by a timestamp in the
138138
`HH:mm:ss` format.
139139

140140
### mimeTypes
141141

142-
Type: `Object`
142+
Type: `Object`
143143
Default: `null`
144144

145145
This property allows a user to register custom mime types or extension mappings.
@@ -148,15 +148,15 @@ eg. `{ 'text/html': [ 'phtml' ] }`. Please see the documentation for
148148

149149
### publicPath
150150

151-
Type: `String`
151+
Type: `String`
152152
_Required_
153153

154154
The public path that the middleware is bound to. _Best Practice: use the same
155155
`publicPath` defined in your webpack config._
156156

157157
### reporter
158158

159-
Type: `Object`
159+
Type: `Object`
160160
Default: `undefined`
161161

162162
Allows users to provide a custom reporter to handle logging within the module.
@@ -165,23 +165,39 @@ for an example.
165165

166166
### serverSideRender
167167

168-
Type: `Boolean`
168+
Type: `Boolean`
169169
Default: `undefined`
170170

171171
Instructs the module to enable or disable the server-side rendering mode. Please
172172
see [Server-Side Rendering](#server-side-rendering) for more information.
173173

174174
### stats
175-
Type: `Object`
175+
176+
Type: `Object`
176177
Default: `{ context: process.cwd() }`
177178

178179
Options for formatting statistics displayed during and after compile. For more
179180
information and property details, please see the
180181
[webpack documentation](https://webpack.js.org/configuration/stats/#stats).
181182

183+
### watchOffset
184+
185+
Type: `Number`
186+
Default: `11000`
187+
188+
Watching (by means of `lazy: false`) will frequently cause multiple compilations
189+
as the bundle changes during compilation. This is due in part to cross-platform
190+
differences in file watchers, so that webpack doesn't loose file changes when
191+
watched files change rapidly. Since that scenario is more an edge case than not,
192+
this option serves as a means to prevent multiple needless, identical compilations
193+
by advancing start-time of a watcher by a number of seconds, which keeps generated
194+
files from triggering the watch cycle.
195+
196+
_To disable this prevention, set this option to a value of `0`._
197+
182198
### watchOptions
183199

184-
Type: `Object`
200+
Type: `Object`
185201
Default: `{ aggregateTimeout: 200 }`
186202

187203
The module accepts an `Object` containing options for file watching, which is

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const defaults = {
1818
colors: true,
1919
context: process.cwd()
2020
},
21+
watchOffset: 11000,
2122
watchOptions: {
2223
aggregateTimeout: 200
2324
}

lib/context.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,27 @@ module.exports = function ctx(compiler, options) {
9494
}
9595

9696
context.rebuild = rebuild;
97-
context.compiler.plugin('done', done);
9897
context.compiler.plugin('invalid', invalid);
99-
context.compiler.plugin('watch-run', invalid);
10098
context.compiler.plugin('run', invalid);
10199

100+
context.compiler.plugin('done', (stats) => {
101+
// clean up the time offset
102+
if (options.watchOffset > 0) {
103+
stats.startTime -= options.watchOffset;
104+
}
105+
106+
done(stats);
107+
});
108+
109+
context.compiler.plugin('watch-run', (watcher, callback) => {
110+
// apply a fix for compiler.watch, if watchOffset is greater than 0:
111+
// ff0000-ad-tech/wp-plugin-watch-offset
112+
// offset start-time
113+
if (options.watchOffset > 0) {
114+
watcher.startTime += options.watchOffset;
115+
}
116+
invalid(watcher, callback);
117+
});
118+
102119
return context;
103120
};

0 commit comments

Comments
 (0)