serverless-plugin-typescript icon indicating copy to clipboard operation
serverless-plugin-typescript copied to clipboard

Support packaging functions individually

Open zjye opened this issue 5 years ago • 7 comments

to fix issue #42

zjye avatar Apr 03 '19 02:04 zjye

Looks like a few people are trying to resolve this issue now (https://github.com/prisma/serverless-plugin-typescript/pull/107 is another one). The relevant code looks like it's on src/index.ts:238 and looks good but there's quite a few lines and 2 commits that aren't so relevant to #42. Are you happy to rebase and trim those away?

I'll try to repro and confirm this fix sometime this week, thanks for helping out!

JackCuthbert avatar Apr 29 '19 23:04 JackCuthbert

@JackCuthbert , i have reapplied the changes, please review

zjye avatar Apr 30 '19 02:04 zjye

Nice @zjye ! Can you drop the merge commit as well and modify the commit with the fix to be a message like:

feat: Support packaging functions individually (closes #42 #107)

This will cause semantic-release to do it's magic and publish a minor version update (as this is adding support for something it didn't do before) and GitHub to automatically close out any related issues/PRs once this is merged.

@divyenduz I'll be attempting to repro this within the next few days, I'll ping again once this is ready for merge!

JackCuthbert avatar Apr 30 '19 05:04 JackCuthbert

Nice @zjye ! Can you drop the merge commit as well and modify the commit with the fix to be a message like:

feat: Support packaging functions individually (closes #42 #107)

Done

zjye avatar Apr 30 '19 05:04 zjye

Hey @zjye I've tested your fix and it looks like it resolves part of the problem defined in the #42 use case where one function is defined as being packaged individually.

What it is not doing is copying anything defined in the include option for each package. I have a small patch here that seems to work but is a little messy, happy for you to include it in your PR if you like:

diff --git a/src/index.ts b/src/index.ts
index 27e1769..9442508 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -149,7 +149,7 @@ export class TypeScriptPlugin {
       await this.linkOrCopy(path.resolve('package.json'), outPkgPath, 'file')
     }
 
-    // include any "extras" from the "include" section
+    // include any "extras" from the "include" section of the main service
     if (this.serverless.service.package.include && this.serverless.service.package.include.length > 0) {
       const files = await globby(this.serverless.service.package.include)
 
@@ -166,6 +166,25 @@ export class TypeScriptPlugin {
         }
       }
     }
+
+    // include any "extras" from the "include" section of each function
+    for (const fnName in this.functions) {
+      const fn = this.functions[fnName]
+      if (fn.package.include !== undefined) {
+        for (const filename of fn.package.include) {
+          const destFileName = path.resolve(path.join(buildFolder, filename))
+          const dirname = path.dirname(destFileName)
+
+          if (!fs.existsSync(dirname)) {
+            fs.mkdirpSync(dirname)
+          }
+
+          if (!fs.existsSync(destFileName)) {
+            fs.copySync(path.resolve(filename), path.resolve(path.join(buildFolder, filename)))
+          }
+        }
+      }
+    }

Additionally, it seems that packaging individually (both at a single function or service-wide level) only duplicates the resulting .zip file with a new name. All code and includes are packaged into each archive, so if you need more control over what goes into each package make sure to use include or exclude options (related documentation), it could get a little finicky.

JackCuthbert avatar May 11 '19 01:05 JackCuthbert

Hey @divyenduz, how do you feel about this one?

JackCuthbert avatar May 14 '19 22:05 JackCuthbert

Do we have an update on this PR? It's such a needed feature.

argjentsahiti avatar Aug 25 '22 14:08 argjentsahiti