diff --git a/src/helper.ts b/src/helper.ts index 33539bbc..033b44a2 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -9,7 +9,7 @@ export function extractFileNames( cwd: string, provider: string, functions?: Record -): { entry: string; func: any }[] { +): { entry: string; func: any, functionAlias?: string }[] { // The Google provider will use the entrypoint not from the definition of the // handler function, but instead from the package.json:main field, or via a // index.js file. This check reads the current package.json in the same way @@ -36,7 +36,8 @@ export function extractFileNames( } } - return Object.values(functions).map(func => { + return Object.keys(functions).map(functionAlias => { + const func = functions[functionAlias]; const h = func.handler; const fnName = path.extname(h); const fnNameLastAppearanceIndex = h.lastIndexOf(fnName); @@ -45,12 +46,12 @@ export function extractFileNames( // Check if the .ts files exists. If so return that to watch if (fs.existsSync(path.join(cwd, fileName + '.ts'))) { - return { entry: fileName + '.ts', func }; + return { entry: fileName + '.ts', func, functionAlias }; } // Check if the .js files exists. If so return that to watch if (fs.existsSync(path.join(cwd, fileName + '.js'))) { - return { entry: fileName + '.js', func }; + return { entry: fileName + '.js', func, functionAlias }; } // Can't find the files. Watch will have an exception anyway. So throw one with error. diff --git a/src/index.ts b/src/index.ts index 302bc9e9..602508bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,6 +59,7 @@ export class EsbuildPlugin implements Plugin { result: BuildResult; bundlePath: string; func: any; + functionAlias: string; }[]; packExternalModules: () => Promise; pack: () => Promise; @@ -206,7 +207,7 @@ export class EsbuildPlugin implements Plugin { this.serverless.cli.log('Compiling with esbuild...'); return Promise.all( - this.rootFileNames.map(async ({ entry, func }) => { + this.rootFileNames.map(async ({ entry, func, functionAlias }) => { const config: Omit = { ...this.buildOptions, external: [...this.buildOptions.external, ...this.buildOptions.exclude], @@ -231,12 +232,12 @@ export class EsbuildPlugin implements Plugin { if (this.buildResults) { const { result } = this.buildResults.find(({ func: fn }) => fn.name === func.name); await result.rebuild(); - return { result, bundlePath, func }; + return { result, bundlePath, func, functionAlias }; } const result = await build(config); - return { result, bundlePath, func }; + return { result, bundlePath, func, functionAlias }; }) ).then(results => { this.serverless.cli.log('Compiling completed.'); diff --git a/src/pack.ts b/src/pack.ts index 1bf9571a..ba4eb1fd 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -106,8 +106,8 @@ export async function pack(this: EsbuildPlugin) { // package each function await Promise.all( - buildResults.map(async ({ func, bundlePath }) => { - const name = func.name; + buildResults.map(async ({ func, functionAlias, bundlePath }) => { + const name = `${this.serverless.service.getServiceName()}-${this.serverless.service.provider.stage}-${functionAlias}`; const excludedFiles = bundlePathList.filter(p => !bundlePath.startsWith(p));