@@ -24,21 +24,41 @@ export class TypeScriptPlugin {
2424 this . hooks = {
2525 'before:run:run' : async ( ) => {
2626 await this . compileTs ( )
27+ await this . copyExtras ( )
28+ await this . copyDependencies ( )
2729 } ,
2830 'before:offline:start' : async ( ) => {
2931 await this . compileTs ( )
32+ await this . copyExtras ( )
33+ await this . copyDependencies ( )
3034 this . watchAll ( )
3135 } ,
3236 'before:offline:start:init' : async ( ) => {
3337 await this . compileTs ( )
38+ await this . copyExtras ( )
39+ await this . copyDependencies ( )
3440 this . watchAll ( )
3541 } ,
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+ } ,
4058 'before:invoke:local:invoke' : async ( ) => {
4159 const emitedFiles = await this . compileTs ( )
60+ await this . copyExtras ( )
61+ await this . copyDependencies ( )
4262 if ( this . isWatching ) {
4363 emitedFiles . forEach ( filename => {
4464 const module = require . resolve ( path . resolve ( this . originalServicePath , filename ) )
@@ -133,29 +153,17 @@ export class TypeScriptPlugin {
133153 tsconfig . outDir = BUILD_FOLDER
134154
135155 const emitedFiles = await typescript . run ( this . rootFileNames , tsconfig )
136- await this . copyExtras ( )
137156 this . serverless . cli . log ( 'Typescript compiled.' )
138157 return emitedFiles
139158 }
140159
141160 /** Link or copy extras such as node_modules or package.include definitions */
142161 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
155163
156164 // 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 )
159167
160168 for ( const filename of files ) {
161169 const destFileName = path . resolve ( path . join ( BUILD_FOLDER , filename ) )
@@ -172,6 +180,37 @@ export class TypeScriptPlugin {
172180 }
173181 }
174182
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+
175214 /**
176215 * Move built code to the serverless folder, taking into account individual
177216 * packaging preferences.
0 commit comments