@@ -3,7 +3,7 @@ import * as fs from 'fs-p'
33import * as _ from 'lodash'
44import * as globby from 'globby'
55
6- import { ServerlessOptions , ServerlessInstance } from './types'
6+ import { ServerlessOptions , ServerlessInstance , ServerlessFunction } from './types'
77import * as typescript from './typescript'
88
99// Folders
@@ -13,6 +13,7 @@ const buildFolder = '.build'
1313class ServerlessPlugin {
1414
1515 private originalServicePath : string
16+ private originalFunctions : { [ key : string ] : ServerlessFunction } | { }
1617
1718 serverless : ServerlessInstance
1819 options : ServerlessOptions
@@ -25,8 +26,10 @@ class ServerlessPlugin {
2526
2627 this . hooks = {
2728 'before:offline:start:init' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
28- 'before:package:createDeploymentArtifacts' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
29- 'after:package:createDeploymentArtifacts' : this . afterCreateDeploymentArtifacts . bind ( this ) ,
29+ 'before:package:createDeploymentArtifacts' : this . beforeCreateDeploymentArtifacts . bind ( this , 'service' ) ,
30+ 'after:package:createDeploymentArtifacts' : this . afterCreateDeploymentArtifacts . bind ( this , 'service' ) ,
31+ 'before:deploy:function:packageFunction' : this . beforeCreateDeploymentArtifacts . bind ( this , 'function' ) ,
32+ 'after:deploy:function:packageFunction' : this . afterCreateDeploymentArtifacts . bind ( this , 'function' ) ,
3033 'before:invoke:local:invoke' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
3134 'after:invoke:local:invoke' : this . cleanup . bind ( this ) ,
3235 }
@@ -55,24 +58,27 @@ class ServerlessPlugin {
5558 }
5659 }
5760
58- async beforeCreateDeploymentArtifacts ( ) : Promise < void > {
61+ async beforeCreateDeploymentArtifacts ( type : string ) : Promise < void > {
5962 this . serverless . cli . log ( 'Compiling with Typescript...' )
6063
6164 // Save original service path and functions
6265 this . originalServicePath = this . serverless . config . servicePath
66+ this . originalFunctions = type === 'function'
67+ ? _ . pick ( this . serverless . service . functions , [ this . options . function ] )
68+ : this . serverless . service . functions
6369
6470 // Fake service path so that serverless will know what to zip
6571 this . serverless . config . servicePath = path . join ( this . originalServicePath , buildFolder )
6672
67- const tsFileNames = typescript . extractFileNames ( this . serverless . service . functions )
73+ const tsFileNames = typescript . extractFileNames ( this . originalFunctions )
6874 const tsconfig = typescript . getTypescriptConfig ( this . originalServicePath )
6975
70- for ( const fnName in this . serverless . service . functions ) {
71- const fn = this . serverless . service . functions [ fnName ]
76+ for ( const fnName in this . originalFunctions ) {
77+ const fn = this . originalFunctions [ fnName ]
7278 fn . package = fn . package || {
73- exclude : [ ] ,
74- include : [ ] ,
75- }
79+ exclude : [ ] ,
80+ include : [ ] ,
81+ }
7682 fn . package . exclude = _ . uniq ( [ ...fn . package . exclude , 'node_modules/serverless-plugin-typescript' ] )
7783 }
7884
@@ -104,14 +110,17 @@ class ServerlessPlugin {
104110 }
105111 }
106112
107- async afterCreateDeploymentArtifacts ( ) : Promise < void > {
113+ async afterCreateDeploymentArtifacts ( type : string ) : Promise < void > {
108114 // Copy .build to .serverless
109115 await fs . copy (
110116 path . join ( this . originalServicePath , buildFolder , serverlessFolder ) ,
111117 path . join ( this . originalServicePath , serverlessFolder )
112118 )
113119
114- this . serverless . service . package . artifact = path . join ( this . originalServicePath , serverlessFolder , path . basename ( this . serverless . service . package . artifact ) )
120+ const basename = type === 'function'
121+ ? path . basename ( this . originalFunctions [ this . options . function ] . artifact )
122+ : path . basename ( this . serverless . service . package . artifact )
123+ this . serverless . service . package . artifact = path . join ( this . originalServicePath , serverlessFolder , basename )
115124
116125 // Cleanup after everything is copied
117126 await this . cleanup ( )
0 commit comments