vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const path = require('path');
  2. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  3. module.exports = {
  4. // 基本路径
  5. /* 部署生产环境和开发环境下的URL:可对当前环境进行区分,baseUrl 从 Vue CLI 3.3 起已弃用,要使用publicPath */
  6. /* baseUrl: process.env.NODE_ENV === 'production' ? './' : '/' */
  7. publicPath: process.env.NODE_ENV === 'production' ? './' : './',
  8. // 输出文件目录
  9. outputDir: 'dist',
  10. // eslint-loader 是否在保存的时候检查
  11. lintOnSave: false,
  12. // use the full build with in-browser compiler?
  13. // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
  14. // compiler: false,
  15. runtimeCompiler: true, //关键点在这
  16. // 调整内部的 webpack 配置。
  17. // 查阅 https://github.com/vuejs/vue-doc-zh-cn/vue-cli/webpack.md
  18. // webpack配置
  19. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  20. // //修改或新增html-webpack-plugin的值,在index.html里面能读取htmlWebpackPlugin.options.title
  21. // chainWebpack: config =>{
  22. // config.plugin('html')
  23. // .tap(args => {
  24. // console.log('htmlhtml', args)
  25. // args[0].title = "平台";
  26. // return args;
  27. // })
  28. // },
  29. configureWebpack: (config) => {
  30. if (process.env.NODE_ENV === 'production') {
  31. // 为生产环境修改配置...
  32. config.mode = 'production';
  33. // 将每个依赖包打包成单独的js文件
  34. var optimization = {
  35. runtimeChunk: 'single',
  36. splitChunks: {
  37. chunks: 'all',
  38. maxInitialRequests: Infinity,
  39. minSize: 20000, // 依赖包超过20000bit将被单独打包
  40. cacheGroups: {
  41. vendor: {
  42. test: /[\\/]node_modules[\\/]/,
  43. name(module) {
  44. // get the name. E.g. node_modules/packageName/not/this/part.js
  45. // or node_modules/packageName
  46. const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  47. // npm package names are URL-safe, but some servers don't like @ symbols
  48. return `npm.${packageName.replace('@', '')}`;
  49. }
  50. }
  51. }
  52. },
  53. minimizer: [
  54. new UglifyJsPlugin({
  55. uglifyOptions: {
  56. // 删除注释
  57. output: {
  58. comments: false
  59. },
  60. // 删除console debugger 删除警告
  61. compress: {
  62. warnings: false,
  63. drop_console: true, //console
  64. drop_debugger: false,
  65. pure_funcs: [ 'console.log' ] //移除console
  66. }
  67. }
  68. })
  69. ]
  70. };
  71. Object.assign(config, {
  72. optimization
  73. });
  74. } else {
  75. // 为开发环境修改配置...
  76. config.mode = 'development';
  77. var optimization2 = {
  78. runtimeChunk: 'single',
  79. splitChunks: {
  80. chunks: 'all',
  81. maxInitialRequests: Infinity,
  82. minSize: 20000, // 依赖包超过20000bit将被单独打包
  83. cacheGroups: {
  84. vendor: {
  85. test: /[\\/]node_modules[\\/]/,
  86. name(module) {
  87. // get the name. E.g. node_modules/packageName/not/this/part.js
  88. // or node_modules/packageName
  89. const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  90. // npm package names are URL-safe, but some servers don't like @ symbols
  91. return `npm.${packageName.replace('@', '')}`;
  92. }
  93. }
  94. }
  95. }
  96. };
  97. }
  98. Object.assign(config, {
  99. // 开发生产共同配置
  100. // externals: {
  101. // 'vue': 'Vue',
  102. // 'element-ui': 'ELEMENT',
  103. // 'vue-router': 'VueRouter',
  104. // 'vuex': 'Vuex'
  105. // } // 防止将某些 import 的包(package)打包到 bundle 中,而是在运行时(runtime)再去从外部获取这些扩展依赖(用于csdn引入)
  106. resolve: {
  107. extensions: [ '.js', '.vue', '.json', '.ts' ], //文件优先解析后缀名顺序
  108. alias: {
  109. '@': path.resolve('./src')
  110. }, // 别名配置
  111. plugins: []
  112. },
  113. optimization: optimization2
  114. });
  115. },
  116. // vue-loader 配置项
  117. // https://vue-loader.vuejs.org/en/options.html
  118. // vueLoader: {},
  119. // 生产环境是否生成 sourceMap 文件
  120. productionSourceMap: false,
  121. // css相关配置
  122. css: {
  123. // 是否使用css分离插件 ExtractTextPlugin
  124. // extract: true, //注释css热更新生效
  125. // 开启 CSS source maps?
  126. sourceMap: false,
  127. // css预设器配置项
  128. loaderOptions: {
  129. less: {
  130. // less 全局变量
  131. lessOptions: {
  132. globalVars: {
  133. hack: `true; @import '~@/assets/styles/variables.less';`
  134. },
  135. javascriptEnabled: true
  136. }
  137. }
  138. }
  139. // 启用 CSS modules for all css / pre-processor files.
  140. // modules: false,
  141. },
  142. // use thread-loader for babel & TS in production build
  143. // enabled by default if the machine has more than 1 cores
  144. parallel: require('os').cpus().length > 1,
  145. // 是否启用dll
  146. // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#dll-mode
  147. // dll: false,
  148. // PWA 插件相关配置
  149. // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
  150. pwa: {},
  151. // webpack-dev-server 相关配置
  152. devServer: {
  153. /* 自动打开浏览器 */
  154. open: true,
  155. // host: "192.168.0.137",
  156. host: '0.0.0.0', //局域网和本地访问
  157. //host: "192.168.1.137",
  158. hot: true,
  159. port: 8888,
  160. https: false,
  161. hotOnly: false,
  162. before: () => {}
  163. },
  164. // 第三方插件配置
  165. pluginOptions: {}
  166. };