Skip to content

Commit e3597fe

Browse files
trygveaafloydspace
authored andcommitted
feat: allow chokidar options to be overridden
This allows you to specify/override any chokidar options, so e.g. awaitWriteFinish can be set to what's best for your system (according to the chokidar docs, the appropriate value for this option is heavily dependent on the OS and hardware). Fixes floydspace#290
1 parent 1e9de51 commit e3597fe

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ The following `esbuild` options are automatically set.
112112

113113
#### Watch Options
114114

115-
| Option | Description | Default |
116-
| --------- | ---------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
117-
| `pattern` | An [anymatch-compatible definition](https://github.com/es128/anymatch) for the watcher to respond to | `./**/*.(js\|ts)` (watches all `.js` and `.ts` files) |
118-
| `ignore` | An [anymatch-compatible definition](https://github.com/es128/anymatch) for the watcher to ignore | `['.esbuild', 'dist', 'node_modules', '.build']` |
115+
| Option | Description | Default |
116+
| ---------- | ---------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
117+
| `pattern` | An [anymatch-compatible definition](https://github.com/es128/anymatch) for the watcher to respond to | `./**/*.(js\|ts)` (watches all `.js` and `.ts` files) |
118+
| `ignore` | An [anymatch-compatible definition](https://github.com/es128/anymatch) for the watcher to ignore | `['.esbuild', 'dist', 'node_modules', '.build']` |
119+
| `chokidar` | Any [Chokidar option](https://github.com/paulmillr/chokidar#api) | `{ awaitWriteFinish: true, ignoreInitial: true }` |
119120

120121
#### Function Options
121122

src/index.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
277277
watch: {
278278
pattern: './**/*.(js|ts)',
279279
ignore: [WORK_FOLDER, 'dist', 'node_modules', BUILD_FOLDER],
280+
chokidar: {
281+
awaitWriteFinish: true,
282+
ignoreInitial: true,
283+
},
280284
},
281285
keepOutputDirectory: false,
282286
platform: 'node',
@@ -313,18 +317,17 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
313317
assert(this.buildOptions, 'buildOptions is not defined');
314318

315319
const defaultPatterns = asArray(this.buildOptions.watch.pattern).filter(isString);
316-
317-
const options = {
318-
ignored: asArray(this.buildOptions.watch.ignore).filter(isString),
319-
awaitWriteFinish: true,
320-
ignoreInitial: true,
321-
};
320+
const defaultIgnored = asArray(this.buildOptions.watch.ignore).filter(isString);
322321

323322
const { patterns, ignored } = this.packagePatterns;
324323

325324
const allPatterns: string[] = [...defaultPatterns, ...patterns];
325+
const allIgnored: string[] = [...defaultIgnored, ...ignored];
326326

327-
options.ignored = [...options.ignored, ...ignored];
327+
const options = {
328+
ignored: allIgnored,
329+
...this.buildOptions.watch.chokidar,
330+
};
328331

329332
chokidar.watch(allPatterns, options).on('all', (eventName, srcPath) =>
330333
this.bundle(true)

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { WatchOptions } from 'chokidar';
12
import type { BuildOptions, BuildResult, Plugin } from 'esbuild';
23
import type Serverless from 'serverless';
34

@@ -9,6 +10,7 @@ export type ReturnPluginsFn = (sls: Serverless) => Plugins;
910
export interface WatchConfiguration {
1011
pattern?: string[] | string;
1112
ignore?: string[] | string;
13+
chokidar?: WatchOptions;
1214
}
1315

1416
export interface PackagerOptions {

0 commit comments

Comments
 (0)