From 48fd8035a4ac1db3120ac64692a20c84e73f5a69 Mon Sep 17 00:00:00 2001 From: Thomas Schaaf Date: Tue, 16 Mar 2021 21:09:12 +0100 Subject: [PATCH 1/3] Add esbuild cache --- src/index.ts | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index cc61cd1d..21342e38 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,7 @@ export class EsbuildPlugin implements Plugin { serverless: Serverless; options: OptionsExtended; + esbuildCache: any; hooks: Plugin.Hooks; buildOptions: Configuration; buildResults: { @@ -70,6 +71,7 @@ export class EsbuildPlugin implements Plugin { this.pack = pack.bind(this); this.preOffline = preOffline.bind(this); this.preLocal = preLocal.bind(this); + this.esbuildCache = {}; this.workDirPath = path.join(this.serverless.config.servicePath, WORK_FOLDER); this.buildDirPath = path.join(this.workDirPath, BUILD_FOLDER); @@ -152,17 +154,19 @@ export class EsbuildPlugin implements Plugin { async watch(): Promise { const options = { ignored: this.buildOptions.watch.ignore, - awaitWriteFinish: true, + awaitWriteFinish: { + stabilityThreshold: 50, + }, ignoreInitial: true, }; - chokidar.watch(this.buildOptions.watch.pattern, options).on('all', () => + chokidar.watch(this.buildOptions.watch.pattern, options).on('all', () => { this.bundle(true) .then(() => this.serverless.cli.log('Watching files for changes...')) - .catch(() => + .catch(e => this.serverless.cli.log('Bundle error, waiting for a file change to reload...') - ) - ); + ); + }); } prepare() { @@ -187,7 +191,7 @@ export class EsbuildPlugin implements Plugin { this.prepare(); this.serverless.cli.log('Compiling with esbuild...'); - return Promise.all( + const results = await Promise.all( this.rootFileNames.map(async ({ entry, func }) => { const config: Omit = { ...this.buildOptions, @@ -204,16 +208,26 @@ export class EsbuildPlugin implements Plugin { delete config['packagePath']; delete config['watch']; - const result = await build(config); + let result; + + if (this.esbuildCache[entry] && this.esbuildCache[entry].rebuild) { + result = await this.esbuildCache[entry].rebuild(); + } else { + result = await build(config); + + if (incremental) { + this.esbuildCache[entry] = result; + } + } const bundlePath = entry.substr(0, entry.lastIndexOf('.')) + '.js'; return { result, bundlePath, func }; }) - ).then(results => { - this.serverless.cli.log('Compiling completed.'); - this.buildResults = results; - return results.map(r => r.result); - }); + ); + + this.serverless.cli.log('Compiling completed.'); + this.buildResults = results; + return results.map(r => r.result); } /** Link or copy extras such as node_modules or package.include definitions */ From e9071e196045678d061be2a7876ed13a67e4e325 Mon Sep 17 00:00:00 2001 From: Thomas Schaaf Date: Tue, 16 Mar 2021 21:12:01 +0100 Subject: [PATCH 2/3] Update index.ts --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 21342e38..68cc4855 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,7 @@ export class EsbuildPlugin implements Plugin { serverless: Serverless; options: OptionsExtended; - esbuildCache: any; + esbuildCache: Record; hooks: Plugin.Hooks; buildOptions: Configuration; buildResults: { @@ -163,7 +163,7 @@ export class EsbuildPlugin implements Plugin { chokidar.watch(this.buildOptions.watch.pattern, options).on('all', () => { this.bundle(true) .then(() => this.serverless.cli.log('Watching files for changes...')) - .catch(e => + .catch(() => this.serverless.cli.log('Bundle error, waiting for a file change to reload...') ); }); @@ -199,7 +199,7 @@ export class EsbuildPlugin implements Plugin { entryPoints: [entry], outdir: path.join(this.buildDirPath, path.dirname(entry)), platform: 'node', - incremental, + // incremental, }; // esbuild v0.7.0 introduced config options validation, so I have to delete plugin specific options from esbuild config. From 7ffe0feebeef29cd8e5bc596d19baaad3f0dc855 Mon Sep 17 00:00:00 2001 From: Thomas Schaaf Date: Tue, 16 Mar 2021 21:13:14 +0100 Subject: [PATCH 3/3] Update index.ts --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 68cc4855..5811d108 100644 --- a/src/index.ts +++ b/src/index.ts @@ -199,7 +199,7 @@ export class EsbuildPlugin implements Plugin { entryPoints: [entry], outdir: path.join(this.buildDirPath, path.dirname(entry)), platform: 'node', - // incremental, + incremental, }; // esbuild v0.7.0 introduced config options validation, so I have to delete plugin specific options from esbuild config.