lib/config/buildPlugins.js
function loadPluginsForType(type, options, version, compress) {
  // Start with default if available
  let plugins = type in allPlugins && allPlugins[type].default ? allPlugins[type].default.slice() : [];

  // Add plugins based on version presets
  plugins = version.reduce((plugins, preset) => {
    let presetPlugins;

    if ('string' == typeof preset) {
      preset = preset.toLowerCase();
      // Ignore generic
      if (RE_GENERIC_VERSION.test(preset)) return plugins;
      // Skip if babel version and type is postcss
      if (type == 'postcss' && preset in allPlugins.babel) return plugins;

      presetPlugins = allPlugins[type][preset];
      // Try and parse with browserslist if no match
      if (!presetPlugins) preset = { browsers: [preset] };
    }

    // Handle object ({ chrome: 5 }, { browsers: ['last 3'] })
    if (!presetPlugins && 'string' != typeof preset) {
      const key = Object.keys(preset)[0];
      const value = preset[key];

      if (key == 'browsers' && Array.isArray(value)) {
        if (type == 'postcss') {
          // Add Autoprefixer and pass-through
          presetPlugins = [['autoprefixer', preset]];
        } else if (type == 'babel') {
          try {
            // Add Babel plugins based on Autoprefixer-style config
            presetPlugins = resolvePluginsForBrowsers(
              browserslist(value).map(browser => {
                const [name, version] = browser.split(' ');

                return { [name]: version };
              })
            );
          } catch (err) {
            /* invalid browserslist value */
          }
        }
      } else {
        // See if concated key+value exists already
        presetPlugins =
          allPlugins[type][(key + value).toLowerCase()] || (type == 'babel' && resolvePluginsForBrowsers(preset));
      }
    }

    if (!presetPlugins) warn(`unable to resolve plugins for ${strong(preset)} version`, 1);

    return unique(plugins.concat(presetPlugins || []));
  }, plugins);

  // Add compression plugins
  if (compress && type in allPlugins && allPlugins[type].compress) {
    plugins.push(...allPlugins[type].compress);
  }

  // Add plugins defined in build (add before current plugins, but preserve order)
  for (let i = options.plugins.length - 1; i >= 0; i--) {
    const plugin = options.plugins[i];
    const pluginName = Array.isArray(plugin) ? plugin[0] : plugin;
    let exists = false;

    plugins.some((existingPlugin, idx) => {
      const existingPluginName = Array.isArray(existingPlugin) ? existingPlugin[0] : existingPlugin;

      // Overwrite if exists
      if (pluginName == existingPluginName) {
        plugins[idx] = plugin;
        exists = true;
      }

      return exists;
    });

    if (!exists) plugins.unshift(plugin);
  }
  options.plugins = plugins;
  // Store hash of plugin names
  options.fingerprint = md5(JSON.stringify(options));
  // Install missing dependencies
  dependencies.install(extractDependencyIds(options));
  resolveDependencyPaths(options);
}
function loadPluginsForType(type, options, version, compress) {
  // Start with default if available
  let plugins = type in allPlugins && allPlugins[type].default ? allPlugins[type].default.slice() : [];

  // Add plugins based on version presets
  plugins = version.reduce((plugins, preset) => {
    let presetPlugins;

    if ('string' == typeof preset) {
      preset = preset.toLowerCase();
      // Ignore generic
      if (RE_GENERIC_VERSION.test(preset)) return plugins;
      // Skip if babel version and type is postcss
      if (type == 'postcss' && preset in allPlugins.babel) return plugins;

      presetPlugins = allPlugins[type][preset];
      // Try and parse with browserslist if no match
      if (!presetPlugins) preset = { browsers: [preset] };
    }

    // Handle object ({ chrome: 5 }, { browsers: ['last 3'] })
    if (!presetPlugins && 'string' != typeof preset) {
      const key = Object.keys(preset)[0];
      const value = preset[key];

      if (key == 'browsers' && Array.isArray(value)) {
        if (type == 'postcss') {
          // Add Autoprefixer and pass-through
          presetPlugins = [['autoprefixer', preset]];
        } else if (type == 'babel') {
          try {
            // Add Babel plugins based on Autoprefixer-style config
            presetPlugins = resolvePluginsForBrowsers(
              browserslist(value).map(browser => {
                const [name, version] = browser.split(' ');

                return { [name]: version };
              })
            );
          } catch (err) {
            /* invalid browserslist value */
          }
        }
      } else {
        // See if concated key+value exists already
        presetPlugins =
          allPlugins[type][(key + value).toLowerCase()] || (type == 'babel' && resolvePluginsForBrowsers(preset));
      }
    }

    if (!presetPlugins) warn(`unable to resolve plugins for ${strong(preset)} version`, 1);

    return unique(plugins.concat(presetPlugins || []));
  }, plugins);

  // Add compression plugins
  if (compress && type in allPlugins && allPlugins[type].compress) {
    plugins.push(...allPlugins[type].compress);
  }

  // Add plugins defined in build (add before current plugins, but preserve order)
  for (let i = options.plugins.length - 1; i >= 0; i--) {
    const plugin = options.plugins[i];
    const pluginName = Array.isArray(plugin) ? plugin[0] : plugin;
    let exists = false;

    plugins.some((existingPlugin, idx) => {
      const existingPluginName = Array.isArray(existingPlugin) ? existingPlugin[0] : existingPlugin;

      // Overwrite if exists
      if (pluginName == existingPluginName) {
        plugins[idx] = plugin;
        exists = true;
      }

      return exists;
    });

    if (!exists) plugins.unshift(plugin);
  }
  options.plugins = plugins;
  // Store hash of plugin names
  options.fingerprint = md5(JSON.stringify(options));
  // Install missing dependencies
  dependencies.install(extractDependencyIds(options));
  resolveDependencyPaths(options);
}
function loadPluginsForType ( type , options , version , compress ) { // Start with default if available let plugins = type in allPlugins && allPlugins [ type ] . default ? allPlugins [ type ] . default . slice ( ) : [ ] ; // Add plugins based on version presets plugins = version . reduce ( ( plugins , preset ) => { let presetPlugins ; if ( 'string' == typeof preset ) { preset = preset . toLowerCase ( ) ; // Ignore generic if ( RE_GENERIC_VERSION . test ( preset ) ) return plugins ; // Skip if babel version and type is postcss if ( type == 'postcss' && preset in allPlugins . babel ) return plugins ; presetPlugins = allPlugins [ type ] [ preset ] ; // Try and parse with browserslist if no match if ( ! presetPlugins ) preset = { browsers : [ preset ] } ; } // Handle object ({ chrome: 5 }, { browsers: ['last 3'] }) if ( ! presetPlugins && 'string' != typeof preset ) { const key = Object . keys ( preset ) [ 0 ] ; const value = preset [ key ] ; if ( key == 'browsers' && Array . isArray ( value ) ) { if ( type == 'postcss' ) { // Add Autoprefixer and pass-through presetPlugins = [ [ 'autoprefixer' , preset ] ] ; } else if ( type == 'babel' ) { try { // Add Babel plugins based on Autoprefixer-style config presetPlugins = resolvePluginsForBrowsers ( browserslist ( value ) . map ( browser => { const [ name , version ] = browser . split ( ' ' ) ; return { [ name ] : version } ; } ) ) ; } catch ( err ) { /* invalid browserslist value */ } } } else { // See if concated key+value exists already presetPlugins = allPlugins [ type ] [ ( key + value ) . toLowerCase ( ) ] || ( type == 'babel' && resolvePluginsForBrowsers ( preset ) ) ; } } if ( ! presetPlugins ) warn ( ` ${ strong ( preset ) } ` , 1 ) ; return unique ( plugins . concat ( presetPlugins || [ ] ) ) ; } , plugins ) ; // Add compression plugins if ( compress && type in allPlugins && allPlugins [ type ] . compress ) { plugins . push ( ... allPlugins [ type ] . compress ) ; } // Add plugins defined in build (add before current plugins, but preserve order) for ( let i = options . plugins . length - 1 ; i >= 0 ; i -- ) { const plugin = options . plugins [ i ] ; const pluginName = Array . isArray ( plugin ) ? plugin [ 0 ] : plugin ; let exists = false ; plugins . some ( ( existingPlugin , idx ) => { const existingPluginName = Array . isArray ( existingPlugin ) ? existingPlugin [ 0 ] : existingPlugin ; // Overwrite if exists if ( pluginName == existingPluginName ) { plugins [ idx ] = plugin ; exists = true ; } return exists ; } ) ; if ( ! exists ) plugins . unshift ( plugin ) ; } options . plugins = plugins ; // Store hash of plugin names options . fingerprint = md5 ( JSON . stringify ( options ) ) ; // Install missing dependencies dependencies . install ( extractDependencyIds ( options ) ) ; resolveDependencyPaths ( options ) ; }
Load plugins for 'type'
@param {String} type
@param {Object} options
@param {Array} version
@param {Boolean} compress
Load plugins for type