webpack.config.base.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const { VueLoaderPlugin } = require('vue-loader');
  6. const CopyPlugin = require('copy-webpack-plugin');
  7. module.exports = {
  8. entry: {
  9. index: path.join(__dirname, '../src/main.js'),
  10. },
  11. output: {
  12. filename: isDev ? '[name].bundle.[chunkhash].js' : '[name].js',
  13. path: path.resolve(__dirname, '../dist'),
  14. clean: true, // 每一次打包前清空dist文件夹
  15. },
  16. module: {
  17. rules: [
  18. {
  19. test: /\.(png|svg|jpeg|gif)$/i,
  20. include: path.join(__dirname, '../src'),
  21. type: 'asset/resource',
  22. },
  23. {
  24. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  25. include: [path.join(__dirname, '../src')],
  26. type: 'asset/resource',
  27. },
  28. {
  29. test: /\.(le|c)ss$/i,
  30. include: [path.join(__dirname, '../src')],
  31. use: [
  32. {
  33. loader: MiniCssExtractPlugin.loader,
  34. options: {
  35. publicPath: '../',
  36. },
  37. },
  38. {
  39. loader: 'css-loader',
  40. options: {
  41. esModule: true,
  42. modules: {
  43. namedExport: true,
  44. localIdentName: '[local]',
  45. },
  46. },
  47. },
  48. {
  49. loader: 'postcss-loader',
  50. options: {
  51. postcssOptions: {
  52. plugins: [['postcss-preset-env']],
  53. },
  54. },
  55. },
  56. 'less-loader',
  57. ],
  58. },
  59. {
  60. test: /\.vue$/,
  61. loader: 'vue-loader',
  62. },
  63. ],
  64. },
  65. resolve: {
  66. extensions: ['.js', '.jsx', '.ts', '.tsx'],
  67. alias: {
  68. '@': path.join(__dirname, '../src'),
  69. },
  70. symlinks: false,
  71. },
  72. plugins: [
  73. new HtmlWebpackPlugin({
  74. title: 'Vue App',
  75. favicon: path.join(__dirname, '../public/favicon.ico'),
  76. template: path.join(__dirname, '../public/index.html'),
  77. }),
  78. new MiniCssExtractPlugin({
  79. filename: isDev ? '[name].[contenthash].css' : '[name].css',
  80. chunkFilename: isDev ? '[id].[contenthash].css' : '[id].css',
  81. }),
  82. // 请确保引入这个插件!
  83. new VueLoaderPlugin(),
  84. new CopyPlugin({
  85. patterns: [
  86. {
  87. from: path.join(__dirname, '../public'),
  88. to: path.join(__dirname, '../dist', 'public'),
  89. },
  90. ],
  91. }),
  92. ],
  93. };