From 5c75cad25eaa0a3f4adb1b741fb401f06a2bf509 Mon Sep 17 00:00:00 2001 From: Vamsi Dharmavarapu Date: Sat, 18 Sep 2021 16:58:45 +0530 Subject: [PATCH] fix(exclude): exclude node_mdoules packaging with * (#167) Co-authored-by: Victor Korzunin closes #181 --- README.md | 2 +- src/index.ts | 7 +++++-- src/pack-externals.ts | 9 +++++++-- src/pack.ts | 9 +++++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b3756ca7..0377dcb1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/index.ts b/src/index.ts index c327b0e4..a599567f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,7 +37,7 @@ export interface PackagerOptions { export interface Configuration extends Omit { packager: 'npm' | 'yarn'; packagePath: string; - exclude: string[]; + exclude: '*' | string[]; nativeZip: boolean; watch: WatchConfiguration; plugins?: string; @@ -248,7 +248,10 @@ export class EsbuildServerlessPlugin implements ServerlessPlugin { this.rootFileNames.map(async ({ entry, func, functionAlias }) => { const config: Omit = { ...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', diff --git a/src/pack-externals.ts b/src/pack-externals.ts index 55f34228..642940cd 100644 --- a/src/pack-externals.ts +++ b/src/pack-externals.ts @@ -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; diff --git a/src/pack.ts b/src/pack.ts index bd99946d..47cb81ef 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -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(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(this.buildOptions.exclude, this.buildOptions.external); + } + const hasExternals = !!externals?.length; // get a tree of all production dependencies