Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add esbuild cache #101

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class EsbuildPlugin implements Plugin {

serverless: Serverless;
options: OptionsExtended;
esbuildCache: Record<string, BuildResult>;
hooks: Plugin.Hooks;
buildOptions: Configuration;
buildResults: {
Expand All @@ -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);
Expand Down Expand Up @@ -152,17 +154,19 @@ export class EsbuildPlugin implements Plugin {
async watch(): Promise<void> {
const options = {
ignored: this.buildOptions.watch.ignore,
awaitWriteFinish: true,
awaitWriteFinish: {
stabilityThreshold: 50,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this it there because before it would wait 2000 ms just until it would start compiling changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds reasonable when taking into account the size of a typical code file. But if half saved code gets compiled we'll have to higher this value a little bit (I think a couple of hundreds should still give a smooth experience but prevent any weirdness)

},
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(() =>
this.serverless.cli.log('Bundle error, waiting for a file change to reload...')
)
);
);
});
}

prepare() {
Expand All @@ -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<BuildOptions, 'watch'> = {
...this.buildOptions,
Expand All @@ -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 */
Expand Down