import {resolve} from 'path'

const aliases = {},
      extensions = ['.js'],
      globExtensions = ['js'],
      loaders = [],
      pkg = require(resolve('package')),
      {dependencies = {}} = pkg,
      {devDependencies = {}} = pkg,
      {optionalDependencies = {}} = pkg,
      {peerDependencies = {}} = pkg,
      keys1 = Object.keys(dependencies),
      keys2 = Object.keys(devDependencies),
      keys3 = Object.keys(optionalDependencies),
      keys4 = Object.keys(peerDependencies),
      modules = [...keys1, ...keys2, ...keys3, ...keys4]
let glob

// JSON
if(moduleExists('json-loader')) {
  extensions.push('.json')
  loaders.push({
    test: /\.json$/,
    loaders: ['json-loader'],
  })
}

// JS with Babel (auto self contain if Babel runtime is available)
if(moduleExists('babel-loader')) {
  extensions.push('.jsx')
  globExtensions.push('jsx')
  loaders.push({
    test: /^(?!.*(bower_components|node_modules))+.+\.js$/,
    loaders: ['babel-loader'],
  })
  if(moduleExists('babel-runtime', 'babel-runtime/package')) {
    loaders[loaders.length - 1].loaders[0] += '?optional[]=runtime'
  }
}

// CoffeeScript
if(moduleExists('coffee-loader')) {
  extensions.push('.coffee', '.coffee.md', '.litcoffee')
  globExtensions.push('coffee', 'coffee.md', 'litcoffee')
  loaders.push({
    test: /\.coffee$/,
    loaders: ['coffee-loader'],
  })
  loaders.push({
    test: /\.(coffee\.md|litcoffee)$/,
    loaders: ['coffee-loader?literate'],
  })
}

// HTML
if(moduleExists('html-loader')) {
  extensions.push('.html')
  loaders.push({
    test: /\.html$/,
    loaders: ['html-loader?-minimize'],
  })
}

// Build glob
glob = globExtensions.length > 1
  ? `*.{${globExtensions.join(',')}}`
  : `*.${globExtensions[0]}`

export {aliases, extensions, glob, loaders}

function moduleExists(mod, path = mod) {
  if(modules.includes(mod)) {
    try {
      require.resolve(path)
      return true
    }
    catch(e) {
      return false
    }
  }
  return false
}

@@ -809,76 +809,23 @@
 %7B%0A  
-extensions.push('.jsx')%0A  globExtensions.push('jsx')%0A  loaders.push(
+const loader = 
 %7B%0A  
@@ -878,16 +878,18 @@
 )+.+%5C.js
+x?
 $/,%0A    
@@ -914,24 +914,101 @@
 oader'%5D,%0A  %7D
+%0A  extensions.push('.jsx')%0A  globExtensions.push('jsx')%0A  loaders.push(loader
 )%0A  if(modul
@@ -1074,29 +1074,8 @@
 ader
-s%5Bloaders.length - 1%5D
 .loa

Refactor some code