@@ -3,7 +3,7 @@ import * as fs from 'fs-p'
3
3
import * as _ from 'lodash'
4
4
import * as globby from 'globby'
5
5
6
- import { ServerlessOptions , ServerlessInstance } from './types'
6
+ import { ServerlessOptions , ServerlessInstance , ServerlessFunction } from './types'
7
7
import * as typescript from './typescript'
8
8
9
9
// Folders
@@ -13,6 +13,7 @@ const buildFolder = '.build'
13
13
class ServerlessPlugin {
14
14
15
15
private originalServicePath : string
16
+ private originalFunctions : { [ key : string ] : ServerlessFunction } | { }
16
17
17
18
serverless : ServerlessInstance
18
19
options : ServerlessOptions
@@ -25,8 +26,10 @@ class ServerlessPlugin {
25
26
26
27
this . hooks = {
27
28
'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' ) ,
30
33
'before:invoke:local:invoke' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
31
34
'after:invoke:local:invoke' : this . cleanup . bind ( this ) ,
32
35
}
@@ -55,24 +58,27 @@ class ServerlessPlugin {
55
58
}
56
59
}
57
60
58
- async beforeCreateDeploymentArtifacts ( ) : Promise < void > {
61
+ async beforeCreateDeploymentArtifacts ( type : string ) : Promise < void > {
59
62
this . serverless . cli . log ( 'Compiling with Typescript...' )
60
63
61
64
// Save original service path and functions
62
65
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
63
69
64
70
// Fake service path so that serverless will know what to zip
65
71
this . serverless . config . servicePath = path . join ( this . originalServicePath , buildFolder )
66
72
67
- const tsFileNames = typescript . extractFileNames ( this . serverless . service . functions )
73
+ const tsFileNames = typescript . extractFileNames ( this . originalFunctions )
68
74
const tsconfig = typescript . getTypescriptConfig ( this . originalServicePath )
69
75
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 ]
72
78
fn . package = fn . package || {
73
- exclude : [ ] ,
74
- include : [ ] ,
75
- }
79
+ exclude : [ ] ,
80
+ include : [ ] ,
81
+ }
76
82
fn . package . exclude = _ . uniq ( [ ...fn . package . exclude , 'node_modules/serverless-plugin-typescript' ] )
77
83
}
78
84
@@ -104,14 +110,17 @@ class ServerlessPlugin {
104
110
}
105
111
}
106
112
107
- async afterCreateDeploymentArtifacts ( ) : Promise < void > {
113
+ async afterCreateDeploymentArtifacts ( type : string ) : Promise < void > {
108
114
// Copy .build to .serverless
109
115
await fs . copy (
110
116
path . join ( this . originalServicePath , buildFolder , serverlessFolder ) ,
111
117
path . join ( this . originalServicePath , serverlessFolder )
112
118
)
113
119
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 )
115
124
116
125
// Cleanup after everything is copied
117
126
await this . cleanup ( )
0 commit comments