From 05f2951b8752dcf0fe1088a2c6a9867cbab66c58 Mon Sep 17 00:00:00 2001 From: Matt Jennings Date: Tue, 20 Apr 2021 15:30:12 -0500 Subject: [PATCH] feat(externals): add option to disable packing of external modules --- README.md | 10 +++++++++- src/index.ts | 11 ++++++++--- src/pack-externals.ts | 3 +-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4cbbb185..28b91d6a 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,17 @@ custom: packagePath: absolute/path/to/package.json # optional - by default it looks for a package.json in the working directory ``` +This behavior can be disabled via `packExternals`: + +```yml +custom: + esbuild: + packExternals: false +``` + ### Usign esbuild plugins -*Note that the plugins API is still experimental : see [the documentation page](https://esbuild.github.io/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: diff --git a/src/index.ts b/src/index.ts index 3709bb23..363eb38f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,6 +32,7 @@ export interface Configuration extends Omit { exclude: string[]; watch: WatchConfiguration; plugins?: string; + packExternals?: boolean; } const DEFAULT_BUILD_OPTIONS: Partial = { @@ -40,6 +41,7 @@ const DEFAULT_BUILD_OPTIONS: Partial = { external: [], exclude: ['aws-sdk'], packager: 'npm', + packExternals: true, watch: { pattern: './**/*.(js|ts)', ignore: [WORK_FOLDER, 'dist', 'node_modules', SERVERLESS_FOLDER], @@ -179,7 +181,7 @@ export class EsbuildPlugin implements Plugin { ...(this.serverless.service.package?.patterns || []), '!node_modules/serverless-esbuild', ]), - ] + ], }; for (const fnName in this.functions) { @@ -192,7 +194,7 @@ export class EsbuildPlugin implements Plugin { ...(fn.package?.exclude || []).map(concat('!')), ...(fn.package?.patterns || []), ]), - ] + ], }; } } @@ -221,6 +223,7 @@ export class EsbuildPlugin implements Plugin { delete config['packagePath']; delete config['watch']; delete config['pugins']; + delete config['packExternals']; const result = await build(config); @@ -264,7 +267,9 @@ export class EsbuildPlugin implements Plugin { } const files = await globby(fn.package.patterns); for (const filename of files) { - const destFileName = path.resolve(path.join(this.buildDirPath, `__only_${fn.name}`, filename)); + const destFileName = path.resolve( + path.join(this.buildDirPath, `__only_${fn.name}`, filename) + ); const dirname = path.dirname(destFileName); if (!fs.existsSync(dirname)) { diff --git a/src/pack-externals.ts b/src/pack-externals.ts index 03d9d18b..a7c3f1fc 100644 --- a/src/pack-externals.ts +++ b/src/pack-externals.ts @@ -190,7 +190,7 @@ function getProdModules( export async function packExternalModules(this: EsbuildPlugin) { const externals = without(this.buildOptions.exclude, this.buildOptions.external); - if (!externals || !externals.length) { + if (!externals || !externals.length || !this.buildOptions.packExternals) { return; } @@ -294,7 +294,6 @@ export async function packExternalModules(this: EsbuildPlugin) { this.serverless.cli.log('Packing external modules: ' + compositeModules.join(', ')); await packager.install(compositeModulePath, exists); this.options.verbose && this.serverless.cli.log(`Package took [${Date.now() - start} ms]`); - // Prune extraneous packages - removes not needed ones const startPrune = Date.now(); await packager.prune(compositeModulePath);