Skip to content

Commit

Permalink
fix(sls): fix function artifact name in case of indifidually option u…
Browse files Browse the repository at this point in the history
…sage

individually option usega togather with custom function name could lead file name issues

fix #128
  • Loading branch information
floydspace committed May 22, 2021
1 parent a295818 commit e504e48
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function extractFileNames(
cwd: string,
provider: string,
functions?: Record<string, Serverless.FunctionDefinitionHandler>
): { 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
Expand All @@ -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);
Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class EsbuildPlugin implements Plugin {
result: BuildResult;
bundlePath: string;
func: any;
functionAlias: string;
}[];
packExternalModules: () => Promise<void>;
pack: () => Promise<void>;
Expand Down Expand Up @@ -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<BuildOptions, 'watch'> = {
...this.buildOptions,
external: [...this.buildOptions.external, ...this.buildOptions.exclude],
Expand All @@ -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.');
Expand Down
4 changes: 2 additions & 2 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit e504e48

Please sign in to comment.