webpack.base.js 2.34 KB
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackBar = require('webpackbar');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

const process = require('process');
const path = require('path');

const cwd = process.cwd();

module.exports = {
  output: {
    filename: './js/[name]-[contenthash].js',
    libraryTarget: 'umd',
    clean: true,
  },
  entry: path.resolve(cwd, './src/index.tsx'),
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        use: [
          'thread-loader',
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env',
                '@babel/preset-react',
                '@babel/preset-typescript',
              ],
            },
          },
        ],
      },
      {
        test: /\.(png|jpeg|jpg)(\?[a-z0-9=&.]+)?$/,
        use: {
          loader: 'file-loader',
          options: {
            name: './public/imgs/[name].[ext]',
          },
        },
      },
      {
        test: /\.(ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
        use: {
          loader: 'file-loader',
          options: {
            name: './public/fonts/[name].[ext]',
          },
        },
      },
      {
        test: /\.less$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'less-loader',
            options: {
              additionalData: `@import '@/styles/variable.less';`,
            },
          },
        ],
      },

      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.svg$/,
        use: ['@svgr/webpack', 'url-loader'],
      },
    ],
  },
  resolve: {
    extensions: ['.json', '.jsx', '.js', '.ts', '.tsx'],
    alias: {
      '@': path.resolve(cwd, './src/'),
      '@/src': path.resolve(cwd, './src/'),
    },
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(cwd, './src/public/index.html'),
    }),
    new CopyPlugin({
      patterns: [
        {
          from: path.resolve(cwd, './static'),
          to: 'static',
        },
      ],
    }),
    new WebpackBar(),
    new NodePolyfillPlugin()
  ],
};