@@ -11,6 +11,8 @@ const semver = require('semver');
11
11
function setArtifactPath ( funcName , func , artifactPath ) {
12
12
const version = this . serverless . getVersion ( ) ;
13
13
14
+ this . options . verbose && this . serverless . cli . log ( `Setting artifact for function '${ funcName } ' to '${ artifactPath } '` ) ;
15
+
14
16
// Serverless changed the artifact path location in version 1.18
15
17
if ( semver . lt ( version , '1.18.0' ) ) {
16
18
func . artifact = artifactPath ;
@@ -26,7 +28,8 @@ function setArtifactPath(funcName, func, artifactPath) {
26
28
function zip ( directory , name ) {
27
29
const zip = archiver . create ( 'zip' ) ;
28
30
// Create artifact in temp path and move it to the package path (if any) later
29
- const artifactFilePath = path . join ( this . serverless . config . servicePath , '.serverless' , name ) ;
31
+ // This allows us to persist the webpackOutputPath and re-use the compiled output
32
+ const artifactFilePath = path . join ( this . webpackOutputPath , name ) ;
30
33
this . serverless . utils . writeFileDir ( artifactFilePath ) ;
31
34
32
35
const output = fs . createWriteStream ( artifactFilePath ) ;
@@ -69,13 +72,47 @@ function zip(directory, name) {
69
72
} ) ;
70
73
}
71
74
75
+ function getArtifactLocations ( name ) {
76
+ const archiveName = `${ name } .zip` ;
77
+
78
+ const webpackArtifact = path . join ( this . webpackOutputPath , archiveName ) ;
79
+ const serverlessArtifact = path . join ( '.serverless' , archiveName ) ;
80
+
81
+ return { webpackArtifact, serverlessArtifact } ;
82
+ }
83
+
84
+ function copyArtifactByName ( artifactName ) {
85
+ const { webpackArtifact, serverlessArtifact } = getArtifactLocations . call ( this , artifactName ) ;
86
+
87
+ // Make sure the destination dir exists
88
+ this . serverless . utils . writeFileDir ( serverlessArtifact ) ;
89
+
90
+ fs . copyFileSync ( webpackArtifact , serverlessArtifact ) ;
91
+ }
92
+
93
+ function setServiceArtifactPath ( artifactPath ) {
94
+ _ . set ( this . serverless , 'service.package.artifact' , artifactPath ) ;
95
+ }
96
+
97
+ function isIndividialPackaging ( ) {
98
+ return _ . get ( this . serverless , 'service.package.individually' ) ;
99
+ }
100
+
101
+ function getArtifactName ( entryFunction ) {
102
+ return `${ entryFunction . funcName || this . serverless . service . getServiceObject ( ) . name } .zip` ;
103
+ }
104
+
72
105
module . exports = {
73
106
packageModules ( ) {
107
+ if ( this . skipCompile ) {
108
+ return BbPromise . resolve ( ) ;
109
+ }
110
+
74
111
const stats = this . compileStats ;
75
112
76
113
return BbPromise . mapSeries ( stats . stats , ( compileStats , index ) => {
77
114
const entryFunction = _ . get ( this . entryFunctions , index , { } ) ;
78
- const filename = ` ${ entryFunction . funcName || this . serverless . service . getServiceObject ( ) . name } .zip` ;
115
+ const filename = getArtifactName . call ( this , entryFunction ) ;
79
116
const modulePath = compileStats . compilation . compiler . outputPath ;
80
117
81
118
const startZip = _ . now ( ) ;
@@ -87,37 +124,52 @@ module.exports = {
87
124
this . serverless . cli . log (
88
125
`Zip ${ _ . isEmpty ( entryFunction ) ? 'service' : 'function' } : ${ modulePath } [${ _ . now ( ) - startZip } ms]`
89
126
)
90
- )
91
- . then ( artifactPath => {
92
- if ( _ . get ( this . serverless , 'service.package.individually' ) ) {
93
- setArtifactPath . call (
94
- this ,
95
- entryFunction . funcName ,
96
- entryFunction . func ,
97
- path . relative ( this . serverless . config . servicePath , artifactPath )
98
- ) ;
99
- }
100
- return artifactPath ;
101
- } ) ;
102
- } ) . then ( artifacts => {
103
- if ( ! _ . get ( this . serverless , 'service.package.individually' ) && ! _ . isEmpty ( artifacts ) ) {
104
- // Set the service artifact to all functions
105
- const allFunctionNames = this . serverless . service . getAllFunctions ( ) ;
106
- _ . forEach ( allFunctionNames , funcName => {
107
- const func = this . serverless . service . getFunction ( funcName ) ;
108
- setArtifactPath . call ( this , funcName , func , path . relative ( this . serverless . config . servicePath , artifacts [ 0 ] ) ) ;
109
- } ) ;
110
- // For Google set the service artifact path
111
- if ( _ . get ( this . serverless , 'service.provider.name' ) === 'google' ) {
112
- _ . set (
113
- this . serverless ,
114
- 'service.package.artifact' ,
115
- path . relative ( this . serverless . config . servicePath , artifacts [ 0 ] )
116
- ) ;
117
- }
118
- }
127
+ ) ;
128
+ } ) ;
129
+ } ,
130
+
131
+ copyExistingArtifacts ( ) {
132
+ this . serverless . cli . log ( 'Copying existing artifacts...' ) ;
133
+ const allFunctionNames = this . serverless . service . getAllFunctions ( ) ;
134
+
135
+ // Copy artifacts to package location
136
+ if ( isIndividialPackaging . call ( this ) ) {
137
+ _ . forEach ( allFunctionNames , funcName => copyArtifactByName . call ( this , funcName ) ) ;
138
+ } else {
139
+ // Copy service packaged artifact
140
+ const serviceName = this . serverless . service . getServiceObject ( ) . name ;
141
+ copyArtifactByName . call ( this , serviceName ) ;
142
+ }
143
+
144
+ _ . forEach ( allFunctionNames , funcName => {
145
+ const func = this . serverless . service . getFunction ( funcName ) ;
146
+
147
+ const archiveName = isIndividialPackaging . call ( this ) ? funcName : this . serverless . service . getServiceObject ( ) . name ;
119
148
120
- return null ;
149
+ const { serverlessArtifact } = getArtifactLocations . call ( this , archiveName ) ;
150
+ setArtifactPath . call ( this , funcName , func , serverlessArtifact ) ;
121
151
} ) ;
152
+
153
+ // Set artifact locations
154
+ if ( isIndividialPackaging . call ( this ) ) {
155
+ _ . forEach ( allFunctionNames , funcName => {
156
+ const func = this . serverless . service . getFunction ( funcName ) ;
157
+
158
+ const archiveName = funcName ;
159
+
160
+ const { serverlessArtifact } = getArtifactLocations . call ( this , archiveName ) ;
161
+ setArtifactPath . call ( this , funcName , func , serverlessArtifact ) ;
162
+ } ) ;
163
+ } else {
164
+ const archiveName = this . serverless . service . getServiceObject ( ) . name ;
165
+
166
+ const { serverlessArtifact } = getArtifactLocations . call ( this , archiveName ) ;
167
+
168
+ if ( _ . get ( this . serverless , 'service.provider.name' ) === 'google' ) {
169
+ setServiceArtifactPath . call ( this , serverlessArtifact ) ;
170
+ }
171
+ }
172
+
173
+ return BbPromise . resolve ( ) ;
122
174
}
123
175
} ;
0 commit comments