webpack.config.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const {resolve} = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. module.exports = {
  4. entry : ['./src/js/index.js','./src/index.html'],
  5. output:{
  6. filename: 'js/built.js',
  7. path: resolve(__dirname,'build')
  8. },
  9. module:{
  10. rules:[
  11. //less
  12. {
  13. test:/\.less$/,
  14. use:['style-loader','css-loader','less-loader']
  15. },
  16. //css
  17. {
  18. test:/\.css$/,
  19. use:['style-loader','css-loader']
  20. },
  21. //图片
  22. {
  23. test: /\.(jpg|jpeg|png|gif)$/,
  24. loader: 'url-loader',
  25. options:{
  26. limit:8*1024,
  27. name:'[hash:10].[ext]',
  28. esModule:false,
  29. outputPath: 'imgs'
  30. },
  31. type:'javascript/auto'
  32. },
  33. //html
  34. {
  35. test:/\.html$/,
  36. loader: 'html-loader'
  37. },
  38. //其他
  39. {
  40. exclude:/\.(html|js|css|less|jpg|jpeg|png|gif)$/,
  41. loader:'file-loader',
  42. options:{
  43. name:'[hash:10].[ext]',
  44. esModule:false,
  45. outputPath: 'media'
  46. },
  47. type:'javascript/auto'
  48. }
  49. ]
  50. },
  51. plugins:[
  52. new HtmlWebpackPlugin({
  53. template:'./src/index.html'
  54. })
  55. ],
  56. mode: 'development',
  57. devServer:{
  58. contentBase:resolve(__dirname,'build'),
  59. compress:true,
  60. port:3000,
  61. open:true,
  62. hot:true
  63. },
  64. target: "web", //可以解决browserslist 导致 webpack-dev-server 的自动刷新失效
  65. /**
  66. * source-map: 外部
  67. * 提示错误代码,提示源代码位置。
  68. * inline-source-map: 内联
  69. * 提示错误代码,提示源代码位置。
  70. * hidden-source-map: 外部
  71. * 提示到错误代码,错误原因,不能追踪到源代码错误,只是构建后。
  72. * eval-source-map: 内联
  73. * 提示错误代码,提示源代码位置。
  74. * nosources-source-map: 外部
  75. * 提示错误代码,没有任何源代码信息。
  76. * cheap-source-map: 外部
  77. * 提示一整行错误带,提示源代码位置。
  78. * cheap-module-source-map: 外部
  79. * 提示错误代码,提示源代码位置。module会将loader的source map加入。
  80. *
  81. * 开发环境:速度快,调试更友好--eval-source-map
  82. * eval inline cheap
  83. * 生产环境:隐藏源代码
  84. * 内联:会让代码体积变大,不考虑。
  85. * mosources 、 source-map 、hidden
  86. */
  87. devtool:'hidden-source-map'
  88. }