diff --git a/lib/plugins/esbuild/index.js b/lib/plugins/esbuild/index.js index 7c7c02e72..afde7fac4 100644 --- a/lib/plugins/esbuild/index.js +++ b/lib/plugins/esbuild/index.js @@ -53,6 +53,18 @@ class Esbuild { await this._package() } }, + 'before:esbuild-package': async () => {}, + } + + this.commands = { + 'esbuild-package': { + groupName: 'main', + options: {}, + usage: + 'Internal hook for esbuild to call for packaging logic prior to internal packaging', + lifecycleEvents: ['package'], + type: 'entrypoint', + }, } } @@ -654,8 +666,11 @@ class Esbuild { const limit = pLimit(concurrency) + await this.serverless.pluginManager.spawn('esbuild-package') + const packageIncludes = await globby( this.serverless.service.package?.patterns ?? [], + { cwd: this.serverless.serviceDir }, ) const zipPromises = Object.entries(functions).map( @@ -679,6 +694,7 @@ class Esbuild { output.on('open', async () => { const functionIncludes = await globby( functionObject.package?.patterns ?? [], + { cwd: this.serverless.serviceDir }, ) const includesToPackage = _.union( @@ -721,11 +737,15 @@ class Esbuild { await Promise.all( includesToPackage.map(async (filePath) => { - const stats = await stat(filePath) + const absolutePath = path.join( + this.serverless.config.serviceDir, + filePath, + ) + const stats = await stat(absolutePath) if (stats.isDirectory()) { - zip.directory(filePath, filePath) + zip.directory(absolutePath, filePath) } else { - zip.file(filePath, { name: filePath }) + zip.file(absolutePath, { name: filePath }) } }), ) @@ -757,8 +777,11 @@ class Esbuild { zipName, ) + await this.serverless.pluginManager.spawn('esbuild-package') + const packageIncludes = await globby( this.serverless.service.package.patterns ?? [], + { cwd: this.serverless.serviceDir }, ) const zip = archiver.create('zip') @@ -807,11 +830,12 @@ class Esbuild { await Promise.all( packageIncludes.map(async (filePath) => { - const stats = await stat(filePath) + const absolutePath = path.join(this.serverless.serviceDir, filePath) + const stats = await stat(absolutePath) if (stats.isDirectory()) { - zip.directory(filePath, filePath) + zip.directory(absolutePath, filePath) } else { - zip.file(filePath, { name: filePath }) + zip.file(absolutePath, { name: filePath }) } }), )