const path = require('path');
const nodeExternals = require('webpack-node-externals');

const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserJSPlugin = require('terser-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')


module.exports = {
  mode: 'production',
  entry: './src',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, '../dist'),
    libraryTarget: 'commonjs2',
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../src'),
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      // 抽离 less
      {
        test: /\.less$/,
        exclude: /\.module\.less$/,
        loader: [
          'style-loader',  // 注意，这里不再用 style-loader
          'css-loader', 
          'less-loader'
        ]
      },
      {
        test: /\.module\.less$/,
        loader: [
          'style-loader', 
          {
            loader: 'css-loader',
            options: {
              modules: {
                localIdentName: '[local]_[hash:base64:5]',
              }
            }
          },
          // 'css-loader?modules',
          'less-loader'
        ]
      }
    ],
  },
  // devtool: 'nosources-source-map',
  // devtool: 'source-map',
  devtool: 'cheap-module-source-map',
  externals: [nodeExternals()],
  optimization: {
    minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
  }
};
