Skip to content

Commit

Permalink
feat(esbuild): support esbuild plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
olup committed Apr 11, 2021
1 parent 353ecd5 commit 628bdf1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions examples/individually/plugins.js
Original file line number Diff line number Diff line change
@@ -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];
1 change: 1 addition & 0 deletions examples/individually/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ provider:

custom:
esbuild:
plugins : ./plugins.js
packager: yarn
bundle: true
minify: true
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export interface WatchConfiguration {
ignore?: string[] | string;
}

export interface Configuration extends Omit<BuildOptions, 'watch'> {
export interface Configuration extends Omit<BuildOptions, 'watch' | 'plugins'> {
packager: 'npm' | 'yarn';
packagePath: string;
exclude: string[];
watch: WatchConfiguration;
plugins?: string;
}

const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
Expand Down Expand Up @@ -196,13 +197,17 @@ 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.
delete config['exclude'];
delete config['packager'];
delete config['packagePath'];
delete config['watch'];
delete config['pugins'];

const result = await build(config);

Expand Down

0 comments on commit 628bdf1

Please sign in to comment.