diff --git a/src/index.ts b/src/index.ts index 95968411..8e4df0fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import fs from 'fs-extra'; import globby from 'globby'; import path from 'path'; -import { concat, always, memoizeWith, mergeRight } from 'ramda'; +import { concat, mergeRight } from 'ramda'; import Serverless from 'serverless'; import ServerlessPlugin from 'serverless/classes/Plugin'; import chokidar from 'chokidar'; @@ -54,6 +54,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { serverless: Serverless; options: Serverless.Options; hooks: ServerlessPlugin.Hooks; + buildOptions: Configuration; buildResults: FunctionBuildResult[]; /** Used for storing previous esbuild build results so we can rebuild more efficiently */ buildCache: Record; @@ -82,13 +83,8 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { this.preLocal = preLocal.bind(this); this.bundle = bundle.bind(this); - this.outputWorkFolder = this.buildOptions.outputWorkFolder || WORK_FOLDER; - this.outputBuildFolder = this.buildOptions.outputBuildFolder || BUILD_FOLDER; - - this.workDirPath = path.join(this.serviceDirPath, this.outputWorkFolder); - this.buildDirPath = path.join(this.workDirPath, this.outputBuildFolder); - this.hooks = { + initialize: () => this.init(), 'before:run:run': async () => { await this.bundle(); await this.packExternalModules(); @@ -135,6 +131,15 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { }; } + private init() { + this.buildOptions = this.getBuildOptions(); + + this.outputWorkFolder = this.buildOptions.outputWorkFolder || WORK_FOLDER; + this.outputBuildFolder = this.buildOptions.outputBuildFolder || BUILD_FOLDER; + this.workDirPath = path.join(this.serviceDirPath, this.outputWorkFolder); + this.buildDirPath = path.join(this.workDirPath, this.outputBuildFolder); + } + /** * Checks if the runtime for the given function is nodejs. * If the runtime is not set , checks the global runtime. @@ -225,7 +230,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { return { patterns, ignored }; } - private getCachedOptions = memoizeWith(always('cache'), () => { + private getBuildOptions() { const DEFAULT_BUILD_OPTIONS: Partial = { concurrency: Infinity, bundle: true, @@ -260,10 +265,6 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { return withResolvedOptions( config ? config(this.serverless) : this.serverless.service.custom?.esbuild ?? {} ); - }); - - get buildOptions() { - return this.getCachedOptions(); } get functionEntries() { diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index f39e15a3..15c88bd5 100644 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -70,6 +70,7 @@ afterEach(() => { describe('Move Artifacts', () => { it('should copy files from the esbuild folder to the serverless folder', async () => { const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(), mockOptions); + plugin.hooks.initialize(); await plugin.moveArtifacts(); @@ -83,6 +84,7 @@ describe('Move Artifacts', () => { ...mockOptions, function: 'hello1', }); + plugin.hooks.initialize(); await plugin.moveArtifacts(); @@ -103,6 +105,7 @@ describe('Move Artifacts', () => { describe('package individually', () => { it('should update function package artifacts base path to the serverless folder', async () => { const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(), mockOptions); + plugin.hooks.initialize(); await plugin.moveArtifacts(); @@ -136,6 +139,7 @@ describe('Move Artifacts', () => { }), mockOptions ); + plugin.hooks.initialize(); await plugin.moveArtifacts(); @@ -163,6 +167,7 @@ describe('Move Artifacts', () => { describe('service package', () => { it('should update the service package artifact base path to the serverless folder', async () => { const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(packageService), mockOptions); + plugin.hooks.initialize(); await plugin.moveArtifacts();