webpack.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const {CleanWebpackPlugin} = require("clean-webpack-plugin");
  4. const VueLoaderPlugin = require("vue-loader/lib/plugin");
  5. const config = {
  6. mode: 'development',
  7. entry: ['@babel/polyfill', path.resolve(__dirname, './src/index.js')],
  8. output: {
  9. filename: '[name].[hash:8].js',
  10. path: path.resolve(__dirname, './dist')
  11. },
  12. plugins: [
  13. // 自动把入口 js 插入到 index.html 中
  14. new HtmlWebpackPlugin({
  15. filename: "index.html",
  16. template: path.resolve(__dirname, './src/public/index.html')
  17. }),
  18. // 清除 dist 的插件
  19. new CleanWebpackPlugin(),
  20. new VueLoaderPlugin()
  21. ],
  22. module: {
  23. rules: [
  24. {
  25. test: /\.vue$/,
  26. use: ['vue-loader']
  27. },
  28. // 转为 es5 语法,兼容更多浏览器
  29. {
  30. test: /\.js$/,
  31. exclude: /node_modules/,
  32. use: [
  33. {
  34. loader: 'babel-loader',
  35. options: {
  36. presets: ['@babel/preset-env']
  37. }
  38. }
  39. ]
  40. },
  41. // css 打包
  42. {
  43. test: /\.css$/,
  44. use: ['vue-style-loader', 'css-loader']
  45. },
  46. // 设置 limit,如果超过限制,使用 file-loader 打包
  47. {
  48. test: /\.(jpg|png|gif)$/,
  49. use: [
  50. {
  51. loader: 'url-loader',
  52. options: {
  53. limit: 1024,
  54. fallback: {
  55. loader: 'file-loader',
  56. options: {
  57. name: 'img/[name].[hash:8].[ext]'
  58. }
  59. }
  60. }
  61. }
  62. ]
  63. },
  64. {
  65. test: /\.(mp4|webm|ogg|mp3|wav)(\?.*)?$/,
  66. use: [
  67. {
  68. loader: 'url-loader',
  69. options: {
  70. limit: 1024,
  71. fallback: {
  72. loader: 'file-loader',
  73. options: {
  74. name: 'media/[name].[hash:8].[ext]'
  75. }
  76. }
  77. }
  78. }
  79. ]
  80. },
  81. {
  82. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  83. use: [
  84. {
  85. loader: 'url-loader',
  86. options: {
  87. limit: 1024,
  88. fallback: {
  89. loader: 'file-loader',
  90. options: {
  91. name: 'fonts/[name].[hash:8].[ext]'
  92. }
  93. }
  94. }
  95. }
  96. ]
  97. }
  98. ]
  99. },
  100. devServer: {
  101. port: '3000',
  102. host: '127.0.0.1',
  103. hot: true,
  104. // 自动打开 url
  105. open: true
  106. },
  107. }
  108. module.exports = config;