Skip to content

Commit

Permalink
fix(exclude): exclude node_mdoules packaging with * (#167)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Korzunin <[email protected]>

closes #181
  • Loading branch information
vamche authored Sep 18, 2021
1 parent 10f7b99 commit 5c75cad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ custom:
```

Check [esbuild](https://github.com/evanw/esbuild#command-line-usage) documentation for the full list of available options. Note that some options like `entryPoints` or `outdir` cannot be overwritten.
The package specified in the `exclude` option is passed to esbuild as `external`, but it is not included in the function bundle either. The default value for this option is `['aws-sdk']`.
The package specified in the `exclude` option is passed to esbuild as `external`, but it is not included in the function bundle either. The default value for this option is `['aws-sdk']`. You can set `exclude` to `*` to disable packaging `node_modules`.

See [example folder](examples) for a minimal example.

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface PackagerOptions {
export interface Configuration extends Omit<BuildOptions, 'nativeZip' | 'watch' | 'plugins'> {
packager: 'npm' | 'yarn';
packagePath: string;
exclude: string[];
exclude: '*' | string[];
nativeZip: boolean;
watch: WatchConfiguration;
plugins?: string;
Expand Down Expand Up @@ -248,7 +248,10 @@ export class EsbuildServerlessPlugin implements ServerlessPlugin {
this.rootFileNames.map(async ({ entry, func, functionAlias }) => {
const config: Omit<BuildOptions, 'watch'> = {
...this.buildOptions,
external: [...this.buildOptions.external, ...this.buildOptions.exclude],
external: [
...this.buildOptions.external,
...(this.buildOptions.exclude === '*' || this.buildOptions.exclude.includes('*') ? [] : this.buildOptions.exclude)
],
entryPoints: [entry],
outdir: path.join(this.buildDirPath, path.dirname(entry)),
platform: 'node',
Expand Down
9 changes: 7 additions & 2 deletions src/pack-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ export async function packExternalModules(this: EsbuildServerlessPlugin) {
packagePaths: findPackagePaths(),
allowList: [],
});
}
}

let externals = [];

const externals = without(this.buildOptions.exclude, this.buildOptions.external);
// get the list of externals only if exclude is not set to *
if (this.buildOptions.exclude !== '*' && !this.buildOptions.exclude.includes('*')) {
externals = without(this.buildOptions.exclude, this.buildOptions.external);
}

if (!externals || !externals.length) {
return;
Expand Down
9 changes: 7 additions & 2 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ export async function pack(this: EsbuildServerlessPlugin) {
const buildResults = this.buildResults;
const bundlePathList = buildResults.map((b) => b.bundlePath);

// get a list of externals
const externals = without<string>(this.buildOptions.exclude, this.buildOptions.external);
let externals = [];

// get the list of externals to include only if exclude is not set to *
if (this.buildOptions.exclude !== '*' && !this.buildOptions.exclude.includes('*')) {
externals = without<string>(this.buildOptions.exclude, this.buildOptions.external);
}

const hasExternals = !!externals?.length;

// get a tree of all production dependencies
Expand Down

0 comments on commit 5c75cad

Please sign in to comment.