@@ -24,21 +24,41 @@ export class TypeScriptPlugin {
24
24
this . hooks = {
25
25
'before:run:run' : async ( ) => {
26
26
await this . compileTs ( )
27
+ await this . copyExtras ( )
28
+ await this . copyDependencies ( )
27
29
} ,
28
30
'before:offline:start' : async ( ) => {
29
31
await this . compileTs ( )
32
+ await this . copyExtras ( )
33
+ await this . copyDependencies ( )
30
34
this . watchAll ( )
31
35
} ,
32
36
'before:offline:start:init' : async ( ) => {
33
37
await this . compileTs ( )
38
+ await this . copyExtras ( )
39
+ await this . copyDependencies ( )
34
40
this . watchAll ( )
35
41
} ,
36
- 'before:package:createDeploymentArtifacts' : this . compileTs . bind ( this ) ,
37
- 'after:package:createDeploymentArtifacts' : this . cleanup . bind ( this ) ,
38
- 'before:deploy:function:packageFunction' : this . compileTs . bind ( this ) ,
39
- 'after:deploy:function:packageFunction' : this . cleanup . bind ( this ) ,
42
+ 'before:package:createDeploymentArtifacts' : async ( ) => {
43
+ await this . compileTs ( )
44
+ await this . copyExtras ( )
45
+ await this . copyDependencies ( true )
46
+ } ,
47
+ 'after:package:createDeploymentArtifacts' : async ( ) => {
48
+ await this . cleanup ( )
49
+ } ,
50
+ 'before:deploy:function:packageFunction' : async ( ) => {
51
+ await this . compileTs ( )
52
+ await this . copyExtras ( )
53
+ await this . copyDependencies ( true )
54
+ } ,
55
+ 'after:deploy:function:packageFunction' : async ( ) => {
56
+ await this . cleanup ( )
57
+ } ,
40
58
'before:invoke:local:invoke' : async ( ) => {
41
59
const emitedFiles = await this . compileTs ( )
60
+ await this . copyExtras ( )
61
+ await this . copyDependencies ( )
42
62
if ( this . isWatching ) {
43
63
emitedFiles . forEach ( filename => {
44
64
const module = require . resolve ( path . resolve ( this . originalServicePath , filename ) )
@@ -133,29 +153,17 @@ export class TypeScriptPlugin {
133
153
tsconfig . outDir = BUILD_FOLDER
134
154
135
155
const emitedFiles = await typescript . run ( this . rootFileNames , tsconfig )
136
- await this . copyExtras ( )
137
156
this . serverless . cli . log ( 'Typescript compiled.' )
138
157
return emitedFiles
139
158
}
140
159
141
160
/** Link or copy extras such as node_modules or package.include definitions */
142
161
async copyExtras ( ) {
143
- const outPkgPath = path . resolve ( path . join ( BUILD_FOLDER , 'package.json' ) )
144
- const outModulesPath = path . resolve ( path . join ( BUILD_FOLDER , 'node_modules' ) )
145
-
146
- // Link or copy node_modules and package.json to .build so Serverless can
147
- // exlcude devDeps during packaging
148
- if ( ! fs . existsSync ( outModulesPath ) ) {
149
- await this . linkOrCopy ( path . resolve ( 'node_modules' ) , outModulesPath , 'junction' )
150
- }
151
-
152
- if ( ! fs . existsSync ( outPkgPath ) ) {
153
- await this . linkOrCopy ( path . resolve ( 'package.json' ) , outPkgPath , 'file' )
154
- }
162
+ const { service } = this . serverless
155
163
156
164
// include any "extras" from the "include" section
157
- if ( this . serverless . service . package . include && this . serverless . service . package . include . length > 0 ) {
158
- const files = await globby ( this . serverless . service . package . include )
165
+ if ( service . package . include && service . package . include . length > 0 ) {
166
+ const files = await globby ( service . package . include )
159
167
160
168
for ( const filename of files ) {
161
169
const destFileName = path . resolve ( path . join ( BUILD_FOLDER , filename ) )
@@ -172,6 +180,37 @@ export class TypeScriptPlugin {
172
180
}
173
181
}
174
182
183
+ /**
184
+ * Copy the `node_modules` folder and `package.json` files to the output
185
+ * directory.
186
+ * @param isPackaging Provided if serverless is packaging the service for deployment
187
+ */
188
+ async copyDependencies ( isPackaging = false ) {
189
+ const outPkgPath = path . resolve ( path . join ( BUILD_FOLDER , 'package.json' ) )
190
+ const outModulesPath = path . resolve ( path . join ( BUILD_FOLDER , 'node_modules' ) )
191
+
192
+ // copy development dependencies during packaging
193
+ if ( isPackaging ) {
194
+ if ( fs . existsSync ( outModulesPath ) ) {
195
+ fs . unlinkSync ( outModulesPath )
196
+ }
197
+
198
+ fs . copySync (
199
+ path . resolve ( 'node_modules' ) ,
200
+ path . resolve ( path . join ( BUILD_FOLDER , 'node_modules' ) )
201
+ )
202
+ } else {
203
+ if ( ! fs . existsSync ( outModulesPath ) ) {
204
+ await this . linkOrCopy ( path . resolve ( 'node_modules' ) , outModulesPath , 'junction' )
205
+ }
206
+ }
207
+
208
+ // copy/link package.json
209
+ if ( ! fs . existsSync ( outPkgPath ) ) {
210
+ await this . linkOrCopy ( path . resolve ( 'package.json' ) , outPkgPath , 'file' )
211
+ }
212
+ }
213
+
175
214
/**
176
215
* Move built code to the serverless folder, taking into account individual
177
216
* packaging preferences.
0 commit comments