vue.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const path = require('path');
  2. const resolve = (dir) => path.join(__dirname, dir);
  3. const CopyWebpackPlugin = require('copy-webpack-plugin');
  4. const webpack = require('webpack');
  5. const cesiumSource = './node_modules/cesium/Source'
  6. const cesiumWorkers = '../Source/Workers'
  7. function extendDefaultPlugins(arr) {
  8. let plug = [
  9. 'removeDoctype',
  10. 'removeXMLProcInst',
  11. 'removeComments',
  12. 'removeMetadata',
  13. 'removeEditorsNSData',
  14. 'cleanupAttrs',
  15. 'mergeStyles',
  16. 'inlineStyles',
  17. 'minifyStyles',
  18. 'cleanupIDs',
  19. 'removeUselessDefs',
  20. 'cleanupNumericValues',
  21. 'convertColors',
  22. 'removeUnknownsAndDefaults',
  23. 'removeNonInheritableGroupAttrs',
  24. 'removeUselessStrokeAndFill',
  25. 'removeViewBox',
  26. 'cleanupEnableBackground',
  27. 'removeHiddenElems',
  28. 'removeEmptyText',
  29. 'convertShapeToPath',
  30. 'convertEllipseToCircle',
  31. 'moveElemsAttrsToGroup',
  32. 'moveGroupAttrsToElems',
  33. 'collapseGroups',
  34. 'convertPathData',
  35. 'convertTransform',
  36. 'removeEmptyAttrs',
  37. 'removeEmptyContainers',
  38. 'mergePaths',
  39. 'removeUnusedNS',
  40. 'sortDefsChildren',
  41. 'removeTitle',
  42. 'removeDesc'
  43. ];
  44. return plug.concat(arr);
  45. }
  46. // 添加less继承
  47. function addStyleResource(rule) {
  48. rule.use('style-resource')
  49. .loader('style-resources-loader')
  50. .options({
  51. patterns: [
  52. resolve('src/assets/styles/common/common.less')
  53. ]
  54. })
  55. }
  56. module.exports = {
  57. publicPath: "./", // 默认'/',部署应用包时的基本 URL
  58. indexPath: 'index.html', // 相对于打包路径index.html的路径
  59. outputDir: 'dist', // 'dist', 生产环境构建文件的目录
  60. assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
  61. lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
  62. runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
  63. configureWebpack: {
  64. plugins: [
  65. new CopyWebpackPlugin([{ from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' }]),
  66. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Assets'), to: 'Assets' }]),
  67. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' }]),
  68. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers' }]),
  69. new webpack.DefinePlugin({
  70. CESIUM_BASE_URL: JSON.stringify('./')
  71. }),
  72. ]
  73. },
  74. chainWebpack: (config) => {
  75. config.resolve.symlinks(true); // 修复热更新失效
  76. config.plugin('html')
  77. .tap(args => {
  78. args[0].title = "智能综合分析系统";
  79. return args;
  80. })
  81. // 路径别名
  82. config.resolve.alias
  83. .set("@", resolve("src"))
  84. .set("@node", resolve("node_modules"))
  85. .set("@com", resolve("src/components"))
  86. .set("@assets", resolve("src/assets"))
  87. .set("@api", resolve("src/api"))
  88. .set("@store", resolve("src/store"))
  89. .set("@modeConfig", resolve("public/static/config"))
  90. .set("@tools", resolve("src/tools"))
  91. .set("cesium", resolve("node_modules/cesium/Source"));
  92. // less 继承
  93. const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
  94. types.forEach(type => addStyleResource(config.module.rule('less').oneOf(type)));
  95. const metaLoaderRule = config.module.rule('meta-loader');
  96. metaLoaderRule.test(/\.js$/)
  97. .use('@open-wc/webpack-import-meta-loader')
  98. .loader('@open-wc/webpack-import-meta-loader');
  99. // svg 雪碧图
  100. config.module // 排除其他svg-loader
  101. .rule('svg')
  102. .exclude.add(resolve('src/assets/icon/svg'))
  103. .end()
  104. .exclude.add(resolve('src/assets/icon/svg_fill'))
  105. .end();
  106. // svg雪碧图
  107. const svgRule = config.module.rule('icons');
  108. svgRule.test(/\.svg$/)
  109. .include.add(resolve('src/assets/icon/svg'))
  110. .end()
  111. .use('svg-sprite-loader')
  112. .loader('svg-sprite-loader')
  113. .options({
  114. symbolId: 'svg-[name]',
  115. // extract: true,
  116. // publicPath: "img/",
  117. // spriteFilename: "svg-sprite-[hash:8].svg",
  118. });
  119. // config
  120. // .plugin("svgsprite")
  121. // .use(SVGSpriteLoaderPlugin, [{
  122. // plainSprite: true
  123. // }]);
  124. // svgo 去除svg中无用元素
  125. svgRule.use('svgo-loader').loader('svgo-loader').options({
  126. plugins: extendDefaultPlugins([{
  127. name: "removeAttrs",
  128. params: {
  129. attrs: 'fill',
  130. }
  131. }])
  132. });
  133. // svg雪碧图 不去除fill属性
  134. // const svgRuleFill = config.module.rule('icons_fill');
  135. // svgRuleFill.test(/\.svg$/)
  136. // .include.add(resolve('src/assets/icon/svg_fill'))
  137. // .end()
  138. // .use('svg-sprite-loader')
  139. // .loader('svg-sprite-loader')
  140. // .options({
  141. // symbolId: 'fill-[name]',
  142. // // extract: true,
  143. // // publicPath: "img/",
  144. // // spriteFilename: "svg-sprite-[hash:8].svg",
  145. // });
  146. // // config
  147. // // .plugin("svgsprite")
  148. // // .use(SVGSpriteLoaderPlugin, [{
  149. // // plainSprite: true
  150. // // }]);
  151. // // svgo 去除svg中无用元素
  152. // svgRuleFill.use('svgo-loader').loader('svgo-loader').options({
  153. // plugins: extendDefaultPlugins([])
  154. // });
  155. },
  156. devServer: {
  157. //代理配置
  158. proxy: {
  159. '/cbk': {
  160. target: 'https://h5.caibeike.com.cn/', // 请求地址
  161. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  162. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  163. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  164. pathRewrite: { // 路径重写,eg:把api接口替换为''
  165. '^/cbk': ''
  166. }
  167. }
  168. },
  169. open: false, // 是否打开浏览器
  170. }
  171. }