aboutsummaryrefslogtreecommitdiff
path: root/vnext/webpack.config.js
blob: 16e5a135f1eb9158da20f949a8e4efd76a6ddb35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable @typescript-eslint/no-var-requires */

const ESLintPlugin = require('eslint-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = (env, argv) => {
  const node_env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
  const dev = node_env !== 'production';
  const config = {
    mode: node_env,
    devtool: dev ? 'source-map' : false,
    entry: {
      'index': [
        __dirname + '/src/index.js',
        __dirname + '/src/index.css'
      ]
    },
    output: {
      path: __dirname + '/../public',
      filename: '[name].js'
    },
    module: {
      rules: [{
        test: /\.js$/,
        exclude: [
          /node_modules/
        ],
        loader: 'babel-loader'
      }, {
        test: /\.(css)$/,
        use: [
          {
            loader: dev ? 'style-loader' : MiniCssExtractPlugin.loader,
          },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
            },
          },
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    'postcss-preset-env',
                    {
                      'stage': 0
                    },
                    'stylelint',
                    'autoprefixer',
                    {
                      'grid': true
                    }
                  ],
                ],
              },
            },
          },
        ],
      }, {
        test: /\.(png|jpe?g|gif|svg)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      }]
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: __dirname + '/src/index.html'
      }),
    ],
    resolve: {
      symlinks: false,
      extensions: ['.js']
    }
  };
  if (dev) {
    config.plugins.push(
      new ESLintPlugin({
        files: __dirname + '/src',
        lintDirtyModulesOnly: true,
        failOnWarning: false,
        failOnError: true,
        fix: false
      }));
    config.devServer = {
      hot: true,
      historyApiFallback: true,
      client: {
        overlay: true
      }
    };
  }
  config.optimization = {
    minimize: !dev,
    minimizer: [
      new TerserPlugin({
        minify: TerserPlugin.swcMinify,
        // `terserOptions` options will be passed to `swc` (`@swc/core`)
        // Link to options - https://swc.rs/docs/config-js-minify
        terserOptions: {},
      }),
    ]
  };
  return config;
};