const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const merge = require('webpack-merge');
const autoprefixer = require('autoprefixer');

const PATHS = {
  site: [
    path.join(__dirname, 'layouts'),
    path.join(__dirname, 'pages')
  ],
  style: path.join(process.cwd(), 'styles'),
  packages: path.join(__dirname, '..', 'packages')
};

const commonConfig = {
  resolve: {
    // Patch webpack module resolution so that the site works with `packages`
    modules: [
      PATHS.packages
    ]
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        include: PATHS.site
      },
      {
        test: /\.woff$/,
        use: 'url-loader?prefix=font/&limit=5000&mimetype=application/font-woff'
      },
      {
        test: /\.ttf$|\.eot$/,
        use: 'file-loader?prefix=font/'
      },
      {
        test: /\.jpg$/,
        use: 'file-loader'
      },
      {
        test: /\.png$/,
        use: 'file-loader'
      },
      {
        test: /\.svg$/,
        use: 'raw-loader'
      },
      {
        test: /\.html$/,
        use: 'raw-loader'
      },
      {
        test: /\.json$/,
        use: 'json-loader'
      }
    ]
  },
  plugins: [
    new CopyWebpackPlugin([
      {
        from: './CNAME',
        to: './'
      }
    ])
  ]
};

module.exports = function (env) {
  switch (env) {
    case 'build':
      return merge(
        commonConfig,
        buildConfig(PATHS.style)
      );
    default:
    case 'start':
      return merge(
        commonConfig,
        developmentConfig(PATHS.style)
      );
  }
};

function developmentConfig(stylePaths) {
  return {
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            'style-loader',
            'css-loader'
          ],
          include: stylePaths
        },
        {
          test: /\.scss$/,
          use: [
            'style-loader',
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                plugins: () => ([
                  autoprefixer({ browsers: ['last 2 versions'] })
                ])
              }
            },
            'sass-loader'
          ],
          include: stylePaths
        }
      ]
    }
  };
}

function buildConfig(stylePaths) {
  return {
    module: {
      rules: [
        {
          test: /\.css$/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: 'css-loader'
          }),
          include: stylePaths
        },
        {
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [
              'css-loader',
              {
                loader: 'postcss-loader',
                options: {
                  plugins: () => ([
                    autoprefixer({ browsers: ['last 2 versions'] })
                  ])
                }
              },
              'sass-loader'
            ]
          }),
          include: stylePaths
        }
      ]
    },
    plugins: [
      new ExtractTextPlugin({
        filename: '[name].css',
        allChunks: true
      }),
      new CleanWebpackPlugin(['build'])
    ]
  };
}

@@ -404,16 +404,63 @@
   style:
+ %5B%0A    path.join(process.cwd(), 'layouts'),%0A   
  path.jo
@@ -488,16 +488,21 @@
 yles'),%0A
+  %5D,%0A
   packag
@@ -2128,32 +2128,80 @@
      'css-loader
+?modules&localIdentName=%5Blocal%5D--%5Bhash:base64:5%5D
 ',%0A            %7B

add css-modules support