const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const themePath = path.resolve(process.cwd(), 'theme.js');
const theme = fs.existsSync(themePath) ? require(themePath)() : {};
const env = require('node-env-file');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

env(path.resolve(process.cwd(), '.env'));
require.extensions['.less'] = require.extensions['.css'] = () => undefined;

module.exports = {
  reactHotLoader: true,
  debug: false,
  serverURL: `http://localhost:${process.env.PORT}`,
  clientURL: `http://localhost:${process.env.PORT + 1}`,
  additionalServerPaths: [
    path.resolve(__dirname, '..', 'graphql'),
    path.resolve(__dirname, 'server'),
    path.resolve(__dirname, '..', 'src', 'ekhn', 'schema'),
    path.resolve(process.cwd(), 'server')
  ],
  modifyWebpackConfig: (baseConfig, { type }) => {
    if (!baseConfig.resolve.alias) baseConfig.resolve.alias = {};
    baseConfig.resolve.modules.push(path.resolve(__dirname, '..', 'node_modules'));
    baseConfig.resolveLoader.modules.push(path.resolve(__dirname, '..', 'node_modules'));
    baseConfig.entry.main[baseConfig.entry.main.length - 1] = path.resolve(__dirname, type, 'index.js');
    baseConfig.resolve.alias.react = path.resolve(process.cwd(), 'node_modules', 'react');
    baseConfig.resolve.alias['react-dom'] = path.resolve(process.cwd(), 'node_modules', 'react-dom');
    baseConfig.resolve.alias['react-router'] = path.resolve(process.cwd(), 'node_modules', 'react-router-v4-decode-uri');
    baseConfig.resolve.alias.moment = path.resolve(process.cwd(), 'node_modules', 'moment');
    baseConfig.resolve.alias.lodash = path.resolve(process.cwd(), 'node_modules', 'lodash');
    baseConfig.resolve.alias.olymp = path.resolve(__dirname, '..');
    baseConfig.resolve.alias['@root'] = path.resolve(process.cwd());
    baseConfig.resolve.alias['@app'] = path.resolve(process.cwd(), 'app');
    if (type === 'server') {
      baseConfig.externals = [
        path.resolve(__dirname, '..', 'node_modules'),
        path.resolve(process.cwd(), 'node_modules')
      ].map(modulesDir => nodeExternals({
        modulesDir,
        whitelist: [
          'source-map-support/register',
          v => v === 'olymp' || v.indexOf('olymp/') === 0 || v.indexOf('olymp-') === 0,
          /\.(eot|woff|woff2|ttf|otf)$/,
          /\.(svg|png|jpg|jpeg|gif|ico)$/,
          /\.(mp4|mp3|ogg|swf|webp)$/,
          /\.(css|scss|sass|sss|less)$/
        ]
      }));
    }
    // baseConfig.plugins[0].definitions.KYT.PUBLIC_DIR = JSON.stringify(path.resolve(process.cwd(), 'public'));
    baseConfig.module.rules = baseConfig.module.rules.map(x => {
      if (x.loader === 'babel-loader') {
        delete x.exclude;
        x.include = [
          path.resolve(process.cwd(), 'app'),
          path.resolve(__dirname),
          path.resolve(__dirname, '..', 'src'),
        ];
        x.options.plugins.push(require.resolve('babel-plugin-transform-object-rest-spread'));
        x.options.plugins.push(require.resolve('babel-plugin-transform-es2015-destructuring'));
        x.options.plugins.push(require.resolve('babel-plugin-transform-decorators-legacy'));
        x.options.plugins.push(require.resolve('babel-plugin-transform-class-properties'));
        if (type === 'client') x.options.plugins.push([require.resolve('babel-plugin-import'), { libraryName: 'antd', style: true }]);
      } return x;
    });

    // TODO production client
    baseConfig.module.rules.forEach(x => {
      if (String(x.test) === String(/\.css$/)) {
        if (Array.isArray(x.use)) {
          x.use.filter(u => typeof u === 'object').forEach(u => {
            if (u.options && u.options.modules) u.options.modules = false;
          });
        }
      }
    });
    baseConfig.module.rules.push({
      test: /\.less$/,
      use: type === 'client' ? [
        'style-loader',
        { loader: 'css-loader', options: { importLoaders: 1 } },
        `less-loader?{"modifyVars":${JSON.stringify(theme)}}`
      ] : [require.resolve('empty-loader')],
    });
    return baseConfig;
  },
};

@@ -3802,24 +3802,90 @@
   %7D%0A    %7D);%0A
+    if (type === 'server') %7B // Empty loader for less on server%0A  
     baseConf
@@ -3907,16 +3907,18 @@
 .push(%7B%0A
+  
       te
@@ -3942,144 +3942,596 @@
     
+  
 use: 
-type === 'client' ? %5B%0A        'style-loader',%0A        %7B loader: 'css-loader', options: %7B importLoaders: 1 %7D %7D,%0A        %60less-loader
+'empty-loader',%0A      %7D);%0A    %7D else if (process.env.NODE_ENV === 'production') %7B // Extract text for less on prod%0A      baseConfig.module.rules.push(%7B%0A        test: /%5C.less$/,%0A        loader: ExtractTextPlugin.extract(%7B%0A          fallbackLoader: 'style',%0A          loader: 'css?sourceMap!postcss!less',%0A        %7D),%0A      %7D);%0A    %7D else %7B%0A      baseConfig.module.rules.push(%7B%0A        test: /%5C.less$/,%0A        use: %5B%0A          'style',%0A          %7B%0A            loader: 'css',%0A            options: %7B modules: false, sourceMap: true %7D,%0A          %7D,%0A          'postcss',%0A          %60less
 ?%7B%22m
@@ -4571,62 +4571,31 @@
 )%7D%7D%60
+,
 %0A      
-%5D : %5Brequire.resolve('empty-loader')
+  
 %5D,%0A
+  
     %7D);%0A
     
@@ -4586,24 +4586,30 @@
 ,%0A      %7D);%0A
+    %7D%0A
     return b

fix css