webpack.prod.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable no-undef */
  2. const webpack = require('webpack');
  3. const { merge } = require("webpack-merge");
  4. const path = require("path");
  5. const common = require("./webpack.common.js");
  6. const CompressionPlugin = require("compression-webpack-plugin");
  7. const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); // js压缩插件
  8. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  9. const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  10. // const DllReferencePlugin = require('webpack/lib/DllReferencePlugin');
  11. const fs = require('fs');
  12. const lessToJs = require('less-vars-to-js');
  13. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  14. const CopyWebpackPlugin = require('copy-webpack-plugin')
  15. //转换 less 变量,用于主题
  16. const baseLess = lessToJs(fs.readFileSync(path.join(__dirname, '../src/themes/themes.less'), 'utf8'));
  17. let themers = {};
  18. Object.keys(baseLess).map((k, i) => {
  19. let key = String(k).replace(/@/g, "");
  20. themers[key] = String(baseLess[k]);
  21. });
  22. const lessVars = Object.assign(themers, { "@CDN_BASE": "" });
  23. const lessExclude = /(node_modules)|(App)/;
  24. module.exports = merge(common, {
  25. mode: "production",
  26. module: {
  27. rules: [
  28. {
  29. test: /\.less$/,
  30. exclude: lessExclude,
  31. use: [
  32. MiniCssExtractPlugin.loader,
  33. {
  34. loader: "css-loader",
  35. options: {
  36. importLoaders: 1,
  37. modules: {
  38. localIdentName: "[local]___[hash:base64:5]"
  39. }
  40. },
  41. },
  42. {
  43. loader: "less-loader",
  44. options: {
  45. modifyVars: lessVars,
  46. javascriptEnabled: true
  47. }
  48. }
  49. ],
  50. },
  51. {
  52. test: /\.less$/,
  53. include: lessExclude,
  54. use: [
  55. MiniCssExtractPlugin.loader,
  56. "css-loader",
  57. {
  58. loader: "less-loader",
  59. options: {
  60. modifyVars: lessVars,
  61. javascriptEnabled: true
  62. }
  63. }
  64. ],
  65. },
  66. {
  67. test: /\.css$/,
  68. use: [
  69. MiniCssExtractPlugin.loader,
  70. 'css-loader',
  71. ],
  72. exclude: /(node_modules)/
  73. },
  74. // style
  75. {
  76. test: /\.css$/,
  77. use: [
  78. MiniCssExtractPlugin.loader,
  79. 'css-loader'],
  80. exclude: /(src)/
  81. },
  82. {
  83. test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  84. loader: "file-loader"
  85. },
  86. {
  87. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  88. use: ['url-loader?limit=10000&mimetype=images/svg+xml']
  89. },
  90. {
  91. test: /\.(less|css)$/,
  92. use: ['style-loader', 'css-loader'],
  93. include: [
  94. path.resolve(__dirname, './node_modules/monaco-editor/'),
  95. path.resolve(__dirname, './node_modules/bravo-editor/'),
  96. ]
  97. },
  98. ]
  99. },
  100. optimization: {
  101. minimizer: [
  102. new UglifyJsPlugin({
  103. cache: true,
  104. parallel: true,
  105. sourceMap: true // set to true if you want JS source maps
  106. }),
  107. new OptimizeCSSAssetsPlugin({})
  108. ]
  109. },
  110. plugins: [
  111. new MiniCssExtractPlugin({
  112. filename: "static/css/[name].[contenthash].css",
  113. chunkFilename: "static/css/[id].[contenthash].css",
  114. ignoreOrder: true
  115. }),
  116. // gzip压缩插件
  117. new CompressionPlugin({
  118. filename: '[path].gz[query]', //目标资源名称。[file] 会被替换成原资源。[path] 会被替换成原资源路径,[query] 替换成原查询字符串
  119. algorithm: 'gzip',//算法
  120. test: new RegExp(
  121. '\\.(js|css)$' //压缩 js 与 css
  122. ),
  123. threshold: 10240,//只处理比这个值大的资源。按字节计算
  124. minRatio: 0.8//只有压缩率比这个值小的资源才会被处理
  125. }),
  126. new webpack.DllReferencePlugin({
  127. manifest: require(path.resolve(__dirname, '../dist/manifest.json')),
  128. }),
  129. new webpack.DefinePlugin({
  130. 'process.env.APP_ENV': JSON.stringify(process.env.APP_ENV),
  131. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  132. }),
  133. new CopyWebpackPlugin([
  134. {
  135. from: path.resolve(__dirname, '../src/assets/js/privateConfig.js'),
  136. to:path.resolve(__dirname, '../dist/static/privateConfig.js'),
  137. // to: '../dist/static/privateConfig.js'
  138. },
  139. {
  140. from: path.resolve(__dirname, '../src/assets/js/crypto-js.js'),
  141. to:path.resolve(__dirname, '../dist/static/crypto-js.js'),
  142. // to: '../dist/static/crypto-js.js'
  143. },
  144. ]),
  145. new MonacoWebpackPlugin()
  146. ],
  147. });