Oxios

webpack config

Sep 30th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  4. const TerserPlugin = require('terser-webpack-plugin');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const { DefinePlugin } = require('webpack');
  7. const dotEnv = require('dotenv').config();
  8.  
  9. const isEnvProduction = process.env.NODE_ENV === 'production';
  10.  
  11. module.exports = {
  12.   mode: process.env.NODE_ENV,
  13.   entry: {
  14.     main: [ 'whatwg-fetch', '@babel/polyfill', './src/index.js' ],
  15.   },
  16.   output: {
  17.     path: path.resolve(__dirname, 'build'),
  18.     filename: isEnvProduction ? '[name].[hash:10].js' : '[name].js',
  19.     chunkFilename: isEnvProduction ? '[name].[hash:10].chunk.js' : '[name].chunk.js',
  20.     publicPath: '/',
  21.   },
  22.   resolve: {
  23.     modules: [ path.resolve(__dirname, 'node_modules') ],
  24.     ...require('./webpack.config.alias').resolve,
  25.   },
  26.   module: {
  27.     rules: [
  28.       {
  29.         test: /\.(js|jsx)$/,
  30.         exclude: [
  31.           /node_modules\/(?!ansi-regex).*/,
  32.         ],
  33.         use: {
  34.           loader: 'babel-loader',
  35.           options: {
  36.             presets: [
  37.               [ '@babel/preset-env', { modules: false } ],
  38.               '@babel/preset-react',
  39.             ],
  40.             plugins: [
  41.               [
  42.                 'babel-plugin-styled-components',
  43.                 {
  44.                   ssr: false,
  45.                   displayName: !isEnvProduction,
  46.                   fileName: false,
  47.                   minify: isEnvProduction,
  48.                   transpileTemplateLiterals: isEnvProduction,
  49.                   pure: false,
  50.                 },
  51.               ],
  52.               '@babel/plugin-transform-regenerator',
  53.               '@babel/plugin-proposal-class-properties',
  54.             ],
  55.           },
  56.         },
  57.       },
  58.       {
  59.         test: /\.font\.js/,
  60.         use: [
  61.           MiniCssExtractPlugin.loader,
  62.           'css-loader',
  63.           {
  64.             loader: 'webfonts-loader',
  65.             options: {
  66.               fileName: '[name].[hash:8].[ext]',
  67.             },
  68.           },
  69.         ],
  70.       },
  71.       {
  72.         test: /\.(jpg|jpeg|png|gif|svg)$/,
  73.         loader: 'file-loader',
  74.         options: {
  75.           name: '[name].[hash:8].[ext]',
  76.         },
  77.       },
  78.       {
  79.         test: /\.(ttf|eot|woff2?)$/,
  80.         loader: 'file-loader',
  81.         options: {
  82.           name: '[name].[ext]',
  83.         },
  84.       },
  85.       {
  86.         test: /\.css$/i,
  87.         use: [ 'style-loader', 'css-loader' ],
  88.       },
  89.     ],
  90.   },
  91.   devServer: {
  92.     open: true,
  93.     overlay: true,
  94.     port: process.env.PORT,
  95.     quiet: true,
  96.     disableHostCheck: true,
  97.     writeToDisk: true,
  98.     historyApiFallback: true,
  99.     proxy: {
  100.       '/api': {
  101.         target: process.env.PROXY_URL,
  102.       },
  103.       '/uploads': {
  104.         target: process.env.PROXY_URL,
  105.       },
  106.     },
  107.   },
  108.   optimization: {
  109.     minimize: isEnvProduction,
  110.     minimizer: [
  111.       new TerserPlugin({
  112.         terserOptions: {
  113.           parse: {
  114.             ecma: 8,
  115.           },
  116.           warnings: false,
  117.           compress: {
  118.             ecma: 5,
  119.             warnings: false,
  120.             comparisons: false,
  121.             inline: 2,
  122.           },
  123.           mangle: true,
  124.           output: {
  125.             ecma: 5,
  126.             comments: false,
  127.             ascii_only: true,
  128.           },
  129.         },
  130.         parallel: true,
  131.         cache: true,
  132.         sourceMap: true,
  133.       }),
  134.     ],
  135.     splitChunks: {
  136.       chunks: 'all',
  137.     },
  138.   },
  139.   devtool: isEnvProduction ? 'source-map' : 'eval-cheap-module-source-map',
  140.   performance: false,
  141.   plugins: [
  142.     new DefinePlugin({
  143.       'process.env': JSON.stringify(dotEnv.parsed),
  144.     }),
  145.     new HtmlWebpackPlugin({
  146.       template: './src/index.html',
  147.       filename: './index.html',
  148.       favicon: './src/assets/images/favicon.ico',
  149.       showErrors: true,
  150.       inject: true,
  151.     }),
  152.     new CleanWebpackPlugin(),
  153.     new MiniCssExtractPlugin({
  154.       filename: '[name].[hash:10].css',
  155.     }),
  156.   ],
  157. };
  158.  
Advertisement
Add Comment
Please, Sign In to add comment