From 628bdf110a8fdba296e88092f3a32ef3d7449215 Mon Sep 17 00:00:00 2001 From: Olup Date: Sun, 11 Apr 2021 22:31:49 +0200 Subject: [PATCH] feat(esbuild): support esbuild plugins --- README.md | 14 ++++++++++++++ examples/individually/plugins.js | 12 ++++++++++++ examples/individually/serverless.yml | 1 + src/index.ts | 7 ++++++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 examples/individually/plugins.js diff --git a/README.md b/README.md index a3c8d223..c161988e 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,20 @@ custom: packagePath: absolute/path/to/package.json # optional - by default it looks for a package.json in the working directory ``` +### Usign esbuild plugins + +*Note that the plugins API is still experimental : see [the documentation page](https://esbuild.github.io/plugins/)* + +You can configure esbuild plugins by passing a plugins' configuration file: + +```yml +custom: + esbuild: + plugins: plugins.js +``` + +The plugins' configuration file must be a javascript file exporting an array of plugins (see `examples/individually/plugins.js` for a dummy plugin example) + ## Usage ### Automatic compilation diff --git a/examples/individually/plugins.js b/examples/individually/plugins.js new file mode 100644 index 00000000..2740b724 --- /dev/null +++ b/examples/individually/plugins.js @@ -0,0 +1,12 @@ +let envPlugin = { + name: 'log-lodash', + setup(build) { + // test interception : log all lodash inports + build.onResolve({ filter: /^lodash$/ }, args => { + console.log(args); + }); + }, +}; + +// default export should be an array of plugins +module.exports = [envPlugin]; diff --git a/examples/individually/serverless.yml b/examples/individually/serverless.yml index 13fc1fd9..35973ed9 100644 --- a/examples/individually/serverless.yml +++ b/examples/individually/serverless.yml @@ -13,6 +13,7 @@ provider: custom: esbuild: + plugins : ./plugins.js packager: yarn bundle: true minify: true diff --git a/src/index.ts b/src/index.ts index cc61cd1d..3f4236ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,11 +26,12 @@ export interface WatchConfiguration { ignore?: string[] | string; } -export interface Configuration extends Omit { +export interface Configuration extends Omit { packager: 'npm' | 'yarn'; packagePath: string; exclude: string[]; watch: WatchConfiguration; + plugins?: string; } const DEFAULT_BUILD_OPTIONS: Partial = { @@ -196,6 +197,9 @@ export class EsbuildPlugin implements Plugin { outdir: path.join(this.buildDirPath, path.dirname(entry)), platform: 'node', incremental, + plugins: + this.buildOptions.plugins && + require(path.join(this.serverless.config.servicePath, this.buildOptions.plugins)), }; // esbuild v0.7.0 introduced config options validation, so I have to delete plugin specific options from esbuild config. @@ -203,6 +207,7 @@ export class EsbuildPlugin implements Plugin { delete config['packager']; delete config['packagePath']; delete config['watch']; + delete config['pugins']; const result = await build(config);