webpack.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  6. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  7. const webpack = require('webpack');
  8. const proxyHost = "http://192.168.2.236:5050";
  9. module.exports = {
  10. entry: {
  11. index: path.resolve(__dirname, 'src/js', 'index.js'),
  12. knowledgeTree: path.resolve(__dirname, 'src/js', 'knowledgeTree.js'),
  13. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  14. },
  15. output: {
  16. publicPath: '/', //这里要放的是静态资源CDN的地址
  17. path: path.resolve(__dirname, 'dist'),
  18. filename: 'js/[name].js'
  19. },
  20. resolve: {
  21. extensions: [".js", ".css", ".json"],
  22. alias: {} //配置别名可以加快webpack查找模块的速度
  23. },
  24. plugins: [// 多入口的html文件用chunks这个参数来区分
  25. new HtmlWebpackPlugin({
  26. title: 'knowledgeGraph',
  27. template: path.resolve(__dirname, 'src/html', 'knowledgeGraph.html'),
  28. filename: 'knowledgeGraph.html',
  29. chunks: ['index', 'vendor', 'common'],
  30. inject: true,
  31. hash: true, //防止缓存
  32. minify: {
  33. removeAttributeQuotes: true, //压缩 去掉引号
  34. removeComments: true, //移除HTML中的注释
  35. collapseWhitespace: true //删除空白符与换行符
  36. }
  37. }),
  38. new HtmlWebpackPlugin({
  39. title: 'knowledgeTree',
  40. template: path.resolve(__dirname, 'src/html', 'knowledgeTree.html'),
  41. filename: 'knowledgeTree.html',
  42. chunks: ['knowledgeTree', 'vendor', 'common'],
  43. inject: true,
  44. hash: true, //防止缓存
  45. minify: {
  46. removeAttributeQuotes: true, //压缩 去掉引号
  47. removeComments: true, //移除HTML中的注释
  48. collapseWhitespace: true //删除空白符与换行符
  49. }
  50. }),
  51. new MiniCssExtractPlugin({
  52. filename: 'css/[name].css',
  53. chunkFilename: '[id].css'
  54. }),
  55. new webpack.ProvidePlugin({
  56. $: 'jquery',
  57. jQuery: 'jquery',
  58. 'window.jQuery': 'jquery'
  59. }),
  60. new webpack.HotModuleReplacementPlugin(),
  61. new CleanWebpackPlugin(),
  62. new ExtractTextPlugin("stylesheets/[name].css"),
  63. ],
  64. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  65. minimizer: [
  66. new UglifyJsPlugin({
  67. uglifyOptions: {
  68. ie8: true,
  69. compress: {
  70. properties: false,
  71. warnings: false
  72. },
  73. mangle: {
  74. screw_ie8: false,
  75. except: ['e']
  76. },
  77. output: {
  78. beautify: true
  79. },
  80. sourceMap: false
  81. }
  82. })
  83. ],
  84. splitChunks: {
  85. cacheGroups: {
  86. commons: {
  87. chunks: "initial",
  88. name: "common",
  89. minChunks: 2,
  90. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  91. minSize: 0, // This is example is too small to create commons chunks
  92. reuseExistingChunk: true // 可设置是否重用该chunk
  93. }
  94. }
  95. }
  96. },
  97. module: {
  98. rules: [
  99. {
  100. test: /.js$/,
  101. enforce: 'post',
  102. loader: 'es3ify-loader'
  103. },
  104. {
  105. test: /\.m?js$/,
  106. exclude: /(node_modules|bower_components)/,
  107. use: {
  108. loader: 'babel-loader',
  109. options: {
  110. presets:['@babel/preset-env']
  111. }
  112. }
  113. },
  114. {
  115. test: /\.css$/,
  116. use:ExtractTextPlugin.extract({
  117. use: [
  118. // {
  119. // loader: MiniCssExtractPlugin.loader
  120. // },
  121. 'css-loader'
  122. ]
  123. })
  124. },
  125. {
  126. test: /\.less$/,
  127. use: ExtractTextPlugin.extract({
  128. fallback: 'style-loader',
  129. use: [
  130. // {
  131. // loader: 'style-loader', // creates style nodes from JS strings
  132. // },
  133. {
  134. loader: 'css-loader', // translates CSS into CommonJS
  135. },
  136. {
  137. loader: 'less-loader', // compiles Less to CSS
  138. },
  139. ],
  140. })
  141. },
  142. {
  143. test: /\.(png|jpg|jpeg|gif|svg)$/,
  144. use: {
  145. loader: 'file-loader',
  146. options: {
  147. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  148. limit: 10000,
  149. name: '[name].[ext]'
  150. }
  151. }
  152. }
  153. ]
  154. },
  155. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  156. mode: 'development',
  157. devServer: {
  158. contentBase: "./dist", //静态文件根目录
  159. proxy: {
  160. '/api': proxyHost
  161. },
  162. hot: true,
  163. openPage:'knowledgeGraph.html'
  164. }
  165. }