webpack.config.base.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const isDev = process.env.NODE_ENV !== 'production';
  5. module.exports = {
  6. entry: {
  7. index: path.join(__dirname, '../src/main.js'),
  8. },
  9. output: {
  10. filename: isDev ? '[name].bundle.[chunkhash].js' : '[name].js',
  11. path: path.resolve(__dirname, '../dist'),
  12. clean: true, // 每一次打包前清空dist文件夹
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.(png|svg|jpeg|gif)$/i,
  18. include: path.join(__dirname, '../src'),
  19. type: 'asset/resource',
  20. },
  21. {
  22. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  23. include: [path.join(__dirname, '../src')],
  24. type: 'asset/resource',
  25. },
  26. {
  27. test: /\.(le|c)ss$/i,
  28. include: [path.join(__dirname, '../src')],
  29. use: [
  30. {
  31. loader: MiniCssExtractPlugin.loader,
  32. options: {
  33. publicPath: '../',
  34. },
  35. },
  36. {
  37. loader: 'css-loader',
  38. options: {
  39. esModule: true,
  40. modules: {
  41. namedExport: true,
  42. localIdentName: '[local]',
  43. },
  44. },
  45. },
  46. {
  47. loader: 'postcss-loader',
  48. options: {
  49. postcssOptions: {
  50. plugins: [['postcss-preset-env']],
  51. },
  52. },
  53. },
  54. 'less-loader',
  55. ],
  56. },
  57. ],
  58. },
  59. plugins: [
  60. new HtmlWebpackPlugin({
  61. title: 'Vue App',
  62. favicon: path.join(__dirname, '../public/favicon.ico'),
  63. template: path.join(__dirname, '../public/index.html'),
  64. }),
  65. new MiniCssExtractPlugin({
  66. filename: isDev ? '[name].[contenthash].css' : '[name].css',
  67. chunkFilename: isDev ? '[id].[contenthash].css' : '[id].css',
  68. }),
  69. ],
  70. };