// IMPORTANT to make weboack working you need to install it globaly on your machine: npm install webpack -g
//for testing time on compilation run this: webpack --profile --display-modules --display-reasons
// require.ensure(['name.js'], function(require){require(name.js).xyz();})

import webpack from 'webpack';
import rimraf from 'rimraf';// remove files
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const extractSass = new ExtractTextPlugin({
  filename: "../css/style.css",
  //disable: process.env.NODE_ENV !== "development"
});

module.exports = {
  context: __dirname + '/frontend/js', //base directory for enrty files
  entry: {
    'main': 'main',
    'css'                 : './../scss/styles.scss' // Styles entry point
  },

  output: {
    path: __dirname + '/public/js',
    publicPath: '/', // internet path for our files, in the end of path must be a slash '/'
    filename: '[name].js',
    libraryTarget: "var",
    library: '[name]'
  },

  resolve: {
    modules: ['frontend/js', 'node_modules'], // define url where to look for modules
    alias: {}
  },
  resolveLoader: {
    modules: ['node_modules'] // define url where to look for loader modules
  },
  plugins: [
    new webpack.NoEmitOnErrorsPlugin(), //if we have error during compilation then webpack will not compile files
    new webpack.optimize.CommonsChunkPlugin({ // if you use one module in more then 2 files this modules will go in common.js file
     name: 'main',
     minChunks: 2
     }),
    {
      apply: (compiler) => {
        rimraf.sync(compiler.options.output.path); // will remove everything what is in /public/js
      }
    },
    extractSass
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [/node_modules/],
        use: 'babel-loader?presets[]=es2015'
      },
      {
        test: /\.scss$/,
        use: extractSass.extract({
          use: [{
            loader: "css-loader",
            options: {
              sourceMap: true
            }
          }, {
            loader: "sass-loader"
          }],
          // use style-loader in development
          fallback: "style-loader"
        })
      }
    ]
  }
};
@@ -306,16 +306,41 @@
 bpack';%0A
+import path from 'path';%0A
 import r
@@ -627,19 +627,16 @@
 frontend
-/js
 ', //bas
@@ -687,16 +687,21 @@
 main': '
+./js/
 main',%0A 
@@ -713,30 +713,11 @@
 ss' 
-                : './.
+: '
 ./sc
@@ -1092,16 +1092,22 @@
 alias: %7B
+%0A%0A    
 %7D%0A  %7D,%0A 
@@ -1947,76 +1947,8 @@
 der%22
-,%0A            options: %7B%0A              sourceMap: true%0A            %7D
 %0A   
@@ -2098,16 +2098,175 @@
      %7D)%0A
+      %7D,%0A      %7B%0A        test: /%5C.(woff2%7Cwoff%7Cttf%7Ceot%7Csvg%7Cotf)?(%5C?v=%5B0-9%5D%5C.%5B0-9%5D%5C.%5B0-9%5D)?$/,%0A        loaders: %5B%22url-loader?limit=100&name=fonts/%5Bname%5D.%5Bext%5D%22%5D%0A
       %7D%0A

update webpack config