Skip to content

Commit

Permalink
feat: allow chokidar options to be overridden
Browse files Browse the repository at this point in the history
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 #290
  • Loading branch information
trygveaa authored and floydspace committed Feb 20, 2023
1 parent 1e9de51 commit e3597fe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ The following `esbuild` options are automatically set.

#### Watch Options

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

#### Function Options

Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
watch: {
pattern: './**/*.(js|ts)',
ignore: [WORK_FOLDER, 'dist', 'node_modules', BUILD_FOLDER],
chokidar: {
awaitWriteFinish: true,
ignoreInitial: true,
},
},
keepOutputDirectory: false,
platform: 'node',
Expand Down Expand Up @@ -313,18 +317,17 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
assert(this.buildOptions, 'buildOptions is not defined');

const defaultPatterns = asArray(this.buildOptions.watch.pattern).filter(isString);

const options = {
ignored: asArray(this.buildOptions.watch.ignore).filter(isString),
awaitWriteFinish: true,
ignoreInitial: true,
};
const defaultIgnored = asArray(this.buildOptions.watch.ignore).filter(isString);

const { patterns, ignored } = this.packagePatterns;

const allPatterns: string[] = [...defaultPatterns, ...patterns];
const allIgnored: string[] = [...defaultIgnored, ...ignored];

options.ignored = [...options.ignored, ...ignored];
const options = {
ignored: allIgnored,
...this.buildOptions.watch.chokidar,
};

chokidar.watch(allPatterns, options).on('all', (eventName, srcPath) =>
this.bundle(true)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WatchOptions } from 'chokidar';
import type { BuildOptions, BuildResult, Plugin } from 'esbuild';
import type Serverless from 'serverless';

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

export interface PackagerOptions {
Expand Down

0 comments on commit e3597fe

Please sign in to comment.