vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. const path = require("path");
  3. const defaultSettings = require("./src/settings.js");
  4. // const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  5. // .BundleAnalyzerPlugin;
  6. function resolve(dir) {
  7. return path.join(__dirname, dir);
  8. }
  9. const name = defaultSettings.title; // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following methods:
  14. // port = 9528 npm run dev OR npm run dev --port = 9528
  15. const port = process.env.port || process.env.npm_config_port || 9528; // dev port
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to htt6ps://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: "./",
  26. outputDir: "dist",
  27. assetsDir: "static",
  28. lintOnSave: false,
  29. productionSourceMap: false,
  30. runtimeCompiler: true,
  31. devServer: {
  32. port: port,
  33. open: false,
  34. overlay: {
  35. warnings: false,
  36. errors: true
  37. },
  38. proxy: {
  39. "/commonOcr": {
  40. target: "http://61.191.20.167:5000",
  41. changeOrigin: true,
  42. pathRewrite: {
  43. // pathRewrite表示路径重写,key表示一个正则,value表示别名
  44. "^/commonOcr": "/commonOcr" // 即用 '/api'表示'http://localhost:3000/api'
  45. }
  46. // onProxyReq: function(proxyReq) {
  47. // proxyReq.removeHeader("origin");
  48. // }
  49. }
  50. }
  51. // before: require("./mock/mock-server.js")
  52. },
  53. configureWebpack: {
  54. // provide the app's title in webpack's name field, so that
  55. // it can be accessed in index.html to inject the correct title.
  56. name: name,
  57. resolve: {
  58. alias: {
  59. "@": resolve("src")
  60. }
  61. }
  62. },
  63. chainWebpack(config) {
  64. config.plugins.delete("preload"); // TODO: need test
  65. config.plugins.delete("prefetch"); // TODO: need test
  66. // config.when(process.env.use_analyzer === true, config => {
  67. // config
  68. // .plugin("webpack-bundle-analyzer")
  69. // .use(BundleAnalyzerPlugin)
  70. // .init(Plugin => new Plugin());
  71. // });
  72. config.module
  73. .rule("echarts")
  74. .test(/\.js$/)
  75. .include.add(resolve("./node_modules/resize-detector"))
  76. .end()
  77. .use("babel")
  78. .loader("babel-loader")
  79. .end();
  80. // set svg-sprite-loader
  81. config.module
  82. .rule("svg")
  83. .exclude.add(resolve("src/icons"))
  84. .end();
  85. config.module
  86. .rule("icons")
  87. .test(/\.svg$/)
  88. .include.add(resolve("src/icons"))
  89. .end()
  90. .use("svg-sprite-loader")
  91. .loader("svg-sprite-loader")
  92. .options({
  93. symbolId: "icon-[name]"
  94. })
  95. .end();
  96. // set preserveWhitespace
  97. config.module
  98. .rule("vue")
  99. .use("vue-loader")
  100. .loader("vue-loader")
  101. .tap(options => {
  102. options.compilerOptions.preserveWhitespace = true;
  103. return options;
  104. })
  105. .end();
  106. config
  107. // https://webpack.js.org/configuration/devtool/#development
  108. .when(process.env.NODE_ENV === "development", config =>
  109. config.devtool("cheap-source-map")
  110. );
  111. config.when(process.env.NODE_ENV !== "development", config => {
  112. config
  113. .plugin("ScriptExtHtmlWebpackPlugin")
  114. .after("html")
  115. .use("script-ext-html-webpack-plugin", [
  116. {
  117. // `runtime` must same as runtimeChunk name. default is `runtime`
  118. inline: /runtime\..*\.js$/
  119. }
  120. ])
  121. .end();
  122. config.optimization.splitChunks({
  123. chunks: "all",
  124. cacheGroups: {
  125. libs: {
  126. name: "chunk-libs",
  127. test: /[\\/]node_modules[\\/]/,
  128. priority: 10,
  129. chunks: "initial" // only package third parties that are initially dependent
  130. },
  131. elementUI: {
  132. name: "chunk-elementUI", // split elementUI into a single package
  133. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  134. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  135. },
  136. dataV: {
  137. name: "chunk-dataV",
  138. priority: 15,
  139. test: /[\\/]node_modules[\\/]_?@jiaminghi(.*)/
  140. },
  141. commons: {
  142. name: "chunk-commons",
  143. test: resolve("src/components"), // can customize your rules
  144. minChunks: 3, // minimum common number
  145. priority: 5,
  146. reuseExistingChunk: true
  147. }
  148. }
  149. });
  150. config.optimization.runtimeChunk("single");
  151. });
  152. }
  153. };