lib/packExternalModules.js
function getProdModules(externalModules, packagePath, dependencyGraph, forceExcludes) {
  const packageJsonPath = path.join(process.cwd(), packagePath);
  const packageJson = require(packageJsonPath);
  const prodModules = [];

  // only process the module stated in dependencies section
  if (!packageJson.dependencies) {
    return [];
  }

  // Get versions of all transient modules
  _.forEach(externalModules, module => {
    let moduleVersion = packageJson.dependencies[module.external];

    if (moduleVersion) {
      prodModules.push(`${module.external}@${moduleVersion}`);

      // Check if the module has any peer dependencies and include them too
      try {
        const modulePackagePath = path.join(
          path.dirname(path.join(process.cwd(), packagePath)),
          'node_modules',
          module.external,
          'package.json'
        );
        const peerDependencies = require(modulePackagePath).peerDependencies;
        if (!_.isEmpty(peerDependencies)) {
          this.options.verbose && this.serverless.cli.log(`Adding explicit peers for dependency ${module.external}`);
          const peerModules = getProdModules.call(this, _.map(peerDependencies, (value, key) => ({ external: key })), packagePath, dependencyGraph, forceExcludes);
          Array.prototype.push.apply(prodModules, peerModules);
        }
      } catch (e) {
        this.serverless.cli.log(`WARNING: Could not check for peer dependencies of ${module.external}`);
      }
    } else {
      if (!packageJson.devDependencies || !packageJson.devDependencies[module.external]) {
        // Add transient dependencies if they appear not in the service's dev dependencies
        const originInfo = _.get(dependencyGraph, 'dependencies', {})[module.origin] || {};
        moduleVersion = _.get(_.get(originInfo, 'dependencies', {})[module.external], 'version');
        if (!moduleVersion) {
          this.serverless.cli.log(`WARNING: Could not determine version of module ${module.external}`);
        }
        prodModules.push(moduleVersion ? `${module.external}@${moduleVersion}` : module.external);
      } else if (packageJson.devDependencies && packageJson.devDependencies[module.external] && !_.includes(forceExcludes, module.external)) {
        // To minimize the chance of breaking setups we whitelist packages available on AWS here. These are due to the previously missing check
        // most likely set in devDependencies and should not lead to an error now.
        const ignoredDevDependencies = ['aws-sdk'];

        if (!_.includes(ignoredDevDependencies, module.external)) {
          // Runtime dependency found in devDependencies but not forcefully excluded
          this.serverless.cli.log(`ERROR: Runtime dependency '${module.external}' found in devDependencies. Move it to dependencies or use forceExclude to explicitly exclude it.`);
          throw new this.serverless.classes.Error(`Serverless-webpack dependency error: ${module.external}.`);
        }

        this.options.verbose && this.serverless.cli.log(`INFO: Runtime dependency '${module.external}' found in devDependencies. It has been excluded automatically.`);
      }
    }
  });

  return prodModules;
}
function getProdModules(externalModules, packagePath, dependencyGraph, forceExcludes) {
  const packageJsonPath = path.join(process.cwd(), packagePath);
  const packageJson = require(packageJsonPath);
  const prodModules = [];

  // only process the module stated in dependencies section
  if (!packageJson.dependencies) {
    return [];
  }

  // Get versions of all transient modules
  _.forEach(externalModules, module => {
    let moduleVersion = packageJson.dependencies[module.external];

    if (moduleVersion) {
      prodModules.push(`${module.external}@${moduleVersion}`);

      // Check if the module has any peer dependencies and include them too
      try {
        const modulePackagePath = path.join(
          path.dirname(path.join(process.cwd(), packagePath)),
          'node_modules',
          module.external,
          'package.json'
        );
        const peerDependencies = require(modulePackagePath).peerDependencies;
        if (!_.isEmpty(peerDependencies)) {
          this.options.verbose && this.serverless.cli.log(`Adding explicit peers for dependency ${module.external}`);
          const peerModules = getProdModules.call(this, _.map(peerDependencies, (value, key) => ({ external: key })), packagePath, dependencyGraph, forceExcludes);
          Array.prototype.push.apply(prodModules, peerModules);
        }
      } catch (e) {
        this.serverless.cli.log(`WARNING: Could not check for peer dependencies of ${module.external}`);
      }
    } else {
      if (!packageJson.devDependencies || !packageJson.devDependencies[module.external]) {
        // Add transient dependencies if they appear not in the service's dev dependencies
        const originInfo = _.get(dependencyGraph, 'dependencies', {})[module.origin] || {};
        moduleVersion = _.get(_.get(originInfo, 'dependencies', {})[module.external], 'version');
        if (!moduleVersion) {
          this.serverless.cli.log(`WARNING: Could not determine version of module ${module.external}`);
        }
        prodModules.push(moduleVersion ? `${module.external}@${moduleVersion}` : module.external);
      } else if (packageJson.devDependencies && packageJson.devDependencies[module.external] && !_.includes(forceExcludes, module.external)) {
        // To minimize the chance of breaking setups we whitelist packages available on AWS here. These are due to the previously missing check
        // most likely set in devDependencies and should not lead to an error now.
        const ignoredDevDependencies = ['aws-sdk'];

        if (!_.includes(ignoredDevDependencies, module.external)) {
          // Runtime dependency found in devDependencies but not forcefully excluded
          this.serverless.cli.log(`ERROR: Runtime dependency '${module.external}' found in devDependencies. Move it to dependencies or use forceExclude to explicitly exclude it.`);
          throw new this.serverless.classes.Error(`Serverless-webpack dependency error: ${module.external}.`);
        }

        this.options.verbose && this.serverless.cli.log(`INFO: Runtime dependency '${module.external}' found in devDependencies. It has been excluded automatically.`);
      }
    }
  });

  return prodModules;
}
function getProdModules ( externalModules , packagePath , dependencyGraph , forceExcludes ) { const packageJsonPath = path . join ( process . cwd ( ) , packagePath ) ; const packageJson = require ( packageJsonPath ) ; const prodModules = [ ] ; // only process the module stated in dependencies section if ( ! packageJson . dependencies ) { return [ ] ; } // Get versions of all transient modules _ . forEach ( externalModules , module => { let moduleVersion = packageJson . dependencies [ module . external ] ; if ( moduleVersion ) { prodModules . push ( ` ${ module . external } ${ moduleVersion } ` ) ; // Check if the module has any peer dependencies and include them too try { const modulePackagePath = path . join ( path . dirname ( path . join ( process . cwd ( ) , packagePath ) ) , 'node_modules' , module . external , 'package.json' ) ; const peerDependencies = require ( modulePackagePath ) . peerDependencies ; if ( ! _ . isEmpty ( peerDependencies ) ) { this . options . verbose && this . serverless . cli . log ( ` ${ module . external } ` ) ; const peerModules = getProdModules . call ( this , _ . map ( peerDependencies , ( value , key ) => ( { external : key } ) ) , packagePath , dependencyGraph , forceExcludes ) ; Array . prototype . push . apply ( prodModules , peerModules ) ; } } catch ( e ) { this . serverless . cli . log ( ` ${ module . external } ` ) ; } } else { if ( ! packageJson . devDependencies || ! packageJson . devDependencies [ module . external ] ) { // Add transient dependencies if they appear not in the service's dev dependencies const originInfo = _ . get ( dependencyGraph , 'dependencies' , { } ) [ module . origin ] || { } ; moduleVersion = _ . get ( _ . get ( originInfo , 'dependencies' , { } ) [ module . external ] , 'version' ) ; if ( ! moduleVersion ) { this . serverless . cli . log ( ` ${ module . external } ` ) ; } prodModules . push ( moduleVersion ? ` ${ module . external } ${ moduleVersion } ` : module . external ) ; } else if ( packageJson . devDependencies && packageJson . devDependencies [ module . external ] && ! _ . includes ( forceExcludes , module . external ) ) { // To minimize the chance of breaking setups we whitelist packages available on AWS here. These are due to the previously missing check // most likely set in devDependencies and should not lead to an error now. const ignoredDevDependencies = [ 'aws-sdk' ] ; if ( ! _ . includes ( ignoredDevDependencies , module . external ) ) { // Runtime dependency found in devDependencies but not forcefully excluded this . serverless . cli . log ( ` ${ module . external } ` ) ; throw new this . serverless . classes . Error ( ` ${ module . external } ` ) ; } this . options . verbose && this . serverless . cli . log ( ` ${ module . external } ` ) ; } } } ) ; return prodModules ; }
Resolve the needed versions of production dependencies for external modules.
@this - The active plugin instance
Resolve the needed versions of production dependencies for external modules .