* Zip up .serverless/requirements or .serverless/[MODULE]/requirements. * @return {Promise} the promise to pack requirements.
()
| 192 | * @return {Promise} the promise to pack requirements. |
| 193 | */ |
| 194 | async function packRequirements() { |
| 195 | if (this.options.zip) { |
| 196 | const servicePath = |
| 197 | this.servicePath || |
| 198 | this.serverless?.config?.servicePath || |
| 199 | this.serverless?.serviceDir || |
| 200 | process.cwd() |
| 201 | // Step 1: Get all Python functions and set default module |
| 202 | const pythonFuncs = this.targetFuncs |
| 203 | .filter((func) => { |
| 204 | const runtime = func.runtime || this.serverless.service.provider.runtime |
| 205 | return runtime && runtime.match(/^python.*/) |
| 206 | }) |
| 207 | .map((func) => { |
| 208 | // Default module to '.' if not specified |
| 209 | if (!func.module) { |
| 210 | func.module = '.' |
| 211 | } |
| 212 | return func |
| 213 | }) |
| 214 | |
| 215 | // Step 2: Separate functions by packaging mode |
| 216 | // Check BOTH function-level AND service-level package.individually |
| 217 | const individuallyPackagedFuncs = [] |
| 218 | const sharedPackagedFuncs = [] |
| 219 | |
| 220 | for (const func of pythonFuncs) { |
| 221 | // A function is individually packaged if EITHER: |
| 222 | // 1. It has package.individually: true at function level, OR |
| 223 | // 2. The service has package.individually: true at service level |
| 224 | const isFunctionIndividual = func.package?.individually === true |
| 225 | const isServiceIndividual = |
| 226 | this.serverless.service.package?.individually === true |
| 227 | |
| 228 | if (isFunctionIndividual || isServiceIndividual) { |
| 229 | individuallyPackagedFuncs.push(func) |
| 230 | } else { |
| 231 | sharedPackagedFuncs.push(func) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Step 3: Pack requirements for individually packaged functions |
| 236 | // Process each unique module once (functions can share modules) |
| 237 | if (individuallyPackagedFuncs.length > 0) { |
| 238 | const uniqueFunctions = uniqBy( |
| 239 | individuallyPackagedFuncs, |
| 240 | (func) => func.module, |
| 241 | ) |
| 242 | for (const func of uniqueFunctions) { |
| 243 | let packProgress |
| 244 | if (this.progress && this.log) { |
| 245 | packProgress = this.progress.get( |
| 246 | `python-pack-requirements-${func.module}`, |
| 247 | ) |
| 248 | packProgress.update( |
| 249 | `Zipping required Python packages for ${func.module}`, |
| 250 | ) |
| 251 | this.log.info(`Zipping required Python packages for ${func.module}`) |