webpack.base.conf.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 存放dev和prod配置
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  6. // 第三方库单独打包
  7. const AutoDllPlugin = require('autodll-webpack-plugin');
  8. // 分离css,打包到单独文件
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  10. module.exports = {
  11. entry: {
  12. bundle: path.resolve(__dirname, '../src/main.js')
  13. },
  14. output: {
  15. path: path.resolve(__dirname, '../dist'),
  16. filename: '[name].[hash].js'
  17. },
  18. module: {
  19. rules: [
  20. {
  21. test: /\.vue$/,
  22. loader: 'vue-loader'
  23. },
  24. {
  25. test: /\.js$/,
  26. use: ['babel-loader'],
  27. include: path.resolve(__dirname + '/src/'),
  28. exclude: /node_modules/
  29. },
  30. {
  31. test: /\.(png|svg|jpg|gif)$/,
  32. use: [
  33. 'file-loader'
  34. ]
  35. },
  36. {
  37. test: /\.(woff|woff2|eot|ttf|otf)$/,
  38. use: [
  39. 'file-loader'
  40. ]
  41. },
  42. {
  43. test: /\.less$/,
  44. use: [
  45. {
  46. loader: MiniCssExtractPlugin.loader,
  47. options: {
  48. publicPath: '../'
  49. }
  50. },
  51. 'css-loader',
  52. 'postcss-loader',
  53. 'less-loader',
  54. ],
  55. },
  56. ]
  57. },
  58. plugins: [
  59. new webpack.HashedModuleIdsPlugin(), // 解决vender后面的hash每次都改变
  60. new HtmlWebpackPlugin({
  61. template: path.resolve(__dirname, '../index.html')
  62. }),
  63. new AutoDllPlugin({
  64. inject: true, // will inject the DLL bundle to index.html
  65. debug: true,
  66. filename: '[name]_[hash].js',
  67. path: './dll',
  68. entry: {
  69. vendor: ['vue', 'vue-router', 'vuex']
  70. }
  71. }), //单独打包第三方库
  72. new VueLoaderPlugin(), // 它的职责是将你定义过的其它规则复制并应用到 .vue 文件里相应语言的块
  73. new webpack.optimize.SplitChunksPlugin() // 提取公共代码
  74. ],
  75. resolve: {
  76. extensions: ['.js', '.scss', '.css', '.vue'],// 省去后缀
  77. alias: {
  78. 'vue$': 'vue/dist/vue.esm.js', //配置别名 确保webpack可以找到.vue文件
  79. "@": path.resolve(__dirname, '../src'),
  80. "components": path.resolve(__dirname, '../src/components'),
  81. "utils": path.resolve(__dirname + '../src/utils')
  82. },
  83. modules: ['node_modules']
  84. }
  85. }