1. 'use strict'
    2. const path = require('path')
    3. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
    4. function resolve(dir) {
    5. return path.join(__dirname, dir)
    6. }
    7. const webpack = require('webpack')
    8. const px2rem = require('postcss-px2rem')
    9. const postcss = px2rem({
    10. // 基准大小 baseSize,需要和rem.js中相同
    11. remUnit: 16
    12. })
    13. const isProduction = process.env.NODE_ENV === 'production';
    14. // // cdn预加载使用,如果可以访问公网就可以放开
    15. // const externals = {
    16. // vue: 'Vue',
    17. // 'vue-router': 'VueRouter',
    18. // vuex: 'Vuex',
    19. // axios: 'axios',
    20. // "element-ui": "ELEMENT",
    21. // 'echarts':'echarts',
    22. // }
    23. // If your port is set to 80,
    24. // use administrator privileges to execute the command line.
    25. // For example, Mac: sudo npm run
    26. // You can change the port by the following method:
    27. // port = 9527 npm run dev OR npm run dev --port = 9527
    28. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
    29. // All configuration item explanations can be find in https://cli.vuejs.org/config/
    30. module.exports = {
    31. /**
    32. * You will need to set publicPath if you plan to deploy your site under a sub path,
    33. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
    34. * then publicPath should be set to "/bar/".
    35. * In most cases please use '/' !!!
    36. * Detail: https://cli.vuejs.org/config/#publicpath
    37. */
    38. publicPath: '/',
    39. outputDir: 'dist',
    40. assetsDir: 'static',
    41. lintOnSave: false,
    42. // lintOnSave: process.env.NODE_ENV === 'development',
    43. productionSourceMap: false,
    44. css: {
    45. loaderOptions: {
    46. postcss: {
    47. plugins: [
    48. postcss
    49. ]
    50. },
    51. scss:{
    52. prependData:`@import "~@/styles/mixin.scss";`
    53. }
    54. }
    55. },
    56. devServer: {
    57. port: port,
    58. open: true,
    59. overlay: {
    60. warnings: false,
    61. errors: true
    62. },
    63. proxy: {
    64. [process.env.VUE_APP_BASE_API]: {
    65. // target: `http://localhost:8081/ipamPortal`, //本机IP
    66. target: `http://192.168.60.227:8080/zoneportal/`, //内网IP
    67. // target: `http://192.168.60.149:8080/zoneportal/`, //内网IP
    68. // target: `http://172.168.70.176:8080/ipamPortal/`,
    69. // target: `http://172.168.70.128:8080/ipamPortal/`, //内网IP
    70. // target: `http://192.168.60.225:8080/zoneportal/`, //测试环境IP
    71. // target: `http://120.236.178.177:22780/zoneportal`, //外网
    72. // target: `window.config.app_api_url`, //外网
    73. changeOrigin: true,
    74. pathRewrite: {
    75. ["^" + process.env.VUE_APP_BASE_API]: ""
    76. }
    77. }
    78. }
    79. // before: require('./mock/mock-server.js')
    80. },
    81. configureWebpack: config => {
    82. const plugins = [];
    83. if (isProduction) {
    84. plugins.push(
    85. new UglifyJsPlugin({
    86. uglifyOptions: {
    87. output: {
    88. comments: false, // 去掉注释
    89. },
    90. warnings: false,
    91. compress: {
    92. drop_console: true,
    93. drop_debugger: false,
    94. pure_funcs: ['console.log']//移除console
    95. }
    96. }
    97. })
    98. )
    99. // 开启分离js
    100. config.optimization = {
    101. runtimeChunk: 'single',
    102. splitChunks: {
    103. chunks: 'all',
    104. maxInitialRequests: Infinity,
    105. minSize: 1000 * 60,
    106. cacheGroups: {
    107. vendor: {
    108. test: /[\\/]node_modules[\\/]/,
    109. name(module) {
    110. // 排除node_modules 然后吧 @ 替换为空 ,考虑到服务器的兼容
    111. const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
    112. return `npm.${packageName.replace('@', '')}`
    113. }
    114. }
    115. }
    116. }
    117. };
    118. // 取消webpack警告的性能提示
    119. config.performance = {
    120. hints: 'warning',
    121. //入口起点的最大体积
    122. maxEntrypointSize: 1000 * 500,
    123. //生成文件的最大体积
    124. maxAssetSize: 1000 * 1000,
    125. //只给出 js 文件的性能提示
    126. assetFilter: function (assetFilename) {
    127. return assetFilename.endsWith('.js');
    128. }
    129. }
    130. // 打包时npm包转CDN
    131. // config.externals = externals;
    132. }
    133. return { plugins }
    134. },
    135. chainWebpack(config) {
    136. // CDN外链,会插入到index.html中
    137. // const cdn = {
    138. // // 开发环境
    139. // dev: {
    140. // css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css'],
    141. // js: []
    142. // },
    143. // // 生产环境
    144. // build: {
    145. // css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css','https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css'],
    146. // js: [
    147. // 'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
    148. // 'https://cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
    149. // 'https://cdn.jsdelivr.net/npm/axios@0.18.1/dist/axios.min.js',
    150. // 'https://cdn.jsdelivr.net/npm/vuex@3.1.0/dist/vuex.min.js',
    151. // 'https://unpkg.com/element-ui/lib/index.js',
    152. // "https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"
    153. // ]
    154. // }
    155. // }
    156. // webpack 会默认给commonChunk打进chunk-vendors,所以需要对webpack的配置进行delete
    157. config.optimization.delete('splitChunks')
    158. // it can improve the speed of the first screen, it is recommended to turn on preload
    159. // it can improve the speed of the first screen, it is recommended to turn on preload
    160. config.plugin('preload').tap(() => [
    161. {
    162. rel: 'preload',
    163. // to ignore runtime.js
    164. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
    165. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
    166. include: 'initial'
    167. }
    168. ])
    169. // when there are many pages, it will cause too many meaningless requests
    170. config.plugins.delete('prefetch')
    171. // set svg-sprite-loader
    172. config.module
    173. .rule('svg')
    174. .exclude.add(resolve('src/icons'))
    175. .end()
    176. config.module
    177. .rule('icons')
    178. .test(/\.svg$/)
    179. .include.add(resolve('src/icons'))
    180. .end()
    181. .use('svg-sprite-loader')
    182. .loader('svg-sprite-loader')
    183. .options({
    184. symbolId: 'icon-[name]'
    185. })
    186. .end()
    187. config.plugin('provide').use(webpack.ProvidePlugin, [{
    188. $: 'jquery',
    189. jquery: 'jquery',
    190. jQuery: 'jquery',
    191. 'window.jQuery': 'jquery',
    192. }])
    193. // config.plugin('html').tap(args => {
    194. // if (process.env.NODE_ENV === 'production') {
    195. // args[0].cdn = cdn.build
    196. // }
    197. // if (process.env.NODE_ENV === 'development') {
    198. // args[0].cdn = cdn.dev
    199. // }
    200. // return args
    201. // })
    202. config
    203. .when(process.env.NODE_ENV !== 'development',
    204. config => {
    205. config
    206. .plugin('ScriptExtHtmlWebpackPlugin')
    207. .after('html')
    208. .use('script-ext-html-webpack-plugin', [{
    209. // `runtime` must same as runtimeChunk name. default is `runtime`
    210. inline: /runtime\..*\.js$/
    211. }])
    212. .end()
    213. /* config
    214. .optimization.splitChunks({
    215. chunks: 'all',
    216. cacheGroups: {
    217. libs: {
    218. name: 'chunk-libs',
    219. test: /[\\/]node_modules[\\/]/,
    220. priority: 10,
    221. chunks: 'initial' // only package third parties that are initially dependent
    222. },
    223. elementUI: {
    224. name: 'chunk-elementUI', // split elementUI into a single package
    225. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
    226. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
    227. },
    228. commons: {
    229. name: 'chunk-commons',
    230. test: resolve('src/components'), // can customize your rules
    231. minChunks: 3, // minimum common number
    232. priority: 5,
    233. reuseExistingChunk: true
    234. }
    235. }
    236. })*/
    237. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
    238. config.optimization.runtimeChunk('single')
    239. }
    240. )
    241. }
    242. }