'use strict';
const path = require('path');
const utils = require('./utils');
const { VueLoaderPlugin } = require('vue-loader');
const ESLintPlugin = require('eslint-webpack-plugin');
const config = require('./config');
const HtmlWebpackPlugin = require('html-webpack-plugin');

function resolve(dir) {
  return path.join(__dirname, '..', dir);
}

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: ['./src/main.js'],
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      vue$: 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },
  output: {
    path: config.ouputDir,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[name].[chunkhash].js'),
    clean: true,
    publicPath: config.publicPath
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        type: 'asset',
        generator: {
          filename: 'img/[name].[hash:7].[ext]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 10000,
          }
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        type: 'asset',
        generator: {
          filename: 'media/[name].[hash:7].[ext]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 10000,
          }
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        type: 'asset',
        generator: {
          filename: 'fonts/[name].[hash:7].[ext]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 10000,
          }
        }
      },
      ...utils.styleLoaders(),
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    // new ESLintPlugin({
    //   extensions: ['js', 'vue'],
    //   files: 'src',
    //   fix: true
    // }),
    new HtmlWebpackPlugin({
      template: 'index.html',
    })
  ]
};
