vue.config.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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
  49. .use("style-resource")
  50. .loader("style-resources-loader")
  51. .options({
  52. patterns: [resolve("src/assets/styles/common/common.less")],
  53. });
  54. }
  55. module.exports = {
  56. publicPath: "./", // 默认'/',部署应用包时的基本 URL
  57. indexPath: "index.html", // 相对于打包路径index.html的路径
  58. outputDir: "dist", // 'dist', 生产环境构建文件的目录
  59. assetsDir: "static", // 相对于outputDir的静态资源(js、css、img、fonts)目录
  60. lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 li
  61. runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
  62. parallel: false,
  63. configureWebpack: {
  64. plugins: [
  65. new CopyWebpackPlugin([
  66. { from: path.join(cesiumSource, cesiumWorkers), to: "Workers" },
  67. ]),
  68. new CopyWebpackPlugin([
  69. { from: path.join(cesiumSource, "Assets"), to: "Assets" },
  70. ]),
  71. new CopyWebpackPlugin([
  72. { from: path.join(cesiumSource, "Widgets"), to: "Widgets" },
  73. ]),
  74. new CopyWebpackPlugin([
  75. {
  76. from: path.join(cesiumSource, "ThirdParty/Workers"),
  77. to: "ThirdParty/Workers",
  78. },
  79. ]),
  80. new webpack.DefinePlugin({
  81. CESIUM_BASE_URL: JSON.stringify("./"),
  82. }),
  83. ],
  84. },
  85. chainWebpack: (config) => {
  86. config.resolve.symlinks(true); // 修复热更新失效
  87. config.plugin("html").tap((args) => {
  88. args[0].title = "发电场站生产实时运营管理平台";
  89. return args;
  90. });
  91. // 路径别名
  92. config.resolve.alias
  93. .set("@", resolve("src"))
  94. .set("@node", resolve("node_modules"))
  95. .set("@com", resolve("src/components"))
  96. .set("@assets", resolve("src/assets"))
  97. .set("@api", resolve("src/api"))
  98. .set("@store", resolve("src/store"))
  99. .set("@modeConfig", resolve("public/static/config"))
  100. .set("@tools", resolve("src/tools"))
  101. .set("cesium", resolve("node_modules/cesium/Source"));
  102. // less 继承
  103. const types = ["vue-modules", "vue", "normal-modules", "normal"];
  104. types.forEach((type) =>
  105. addStyleResource(config.module.rule("less").oneOf(type))
  106. );
  107. const metaLoaderRule = config.module.rule("meta-loader");
  108. metaLoaderRule
  109. .test(/\.js$/)
  110. .use("@open-wc/webpack-import-meta-loader")
  111. .loader("@open-wc/webpack-import-meta-loader");
  112. // svg 雪碧图
  113. config.module // 排除其他svg-loader
  114. .rule("svg")
  115. .exclude.add(resolve("src/assets/icon/svg"))
  116. .end()
  117. .exclude.add(resolve("src/assets/icon/svg_fill"))
  118. .end();
  119. // svg雪碧图
  120. const svgRule = config.module.rule("icons");
  121. svgRule
  122. .test(/\.svg$/)
  123. .include.add(resolve("src/assets/icon/svg"))
  124. .end()
  125. .use("svg-sprite-loader")
  126. .loader("svg-sprite-loader")
  127. .options({
  128. symbolId: "svg-[name]",
  129. // extract: true,
  130. // publicPath: "img/",
  131. // spriteFilename: "svg-sprite-[hash:8].svg",
  132. });
  133. // config
  134. // .plugin("svgsprite")
  135. // .use(SVGSpriteLoaderPlugin, [{
  136. // plainSprite: true
  137. // }]);
  138. // svgo 去除svg中无用元素
  139. svgRule
  140. .use("svgo-loader")
  141. .loader("svgo-loader")
  142. .options({
  143. plugins: extendDefaultPlugins([
  144. {
  145. name: "removeAttrs",
  146. params: {
  147. attrs: "fill",
  148. },
  149. },
  150. ]),
  151. });
  152. // svg雪碧图 不去除fill属性
  153. // const svgRuleFill = config.module.rule('icons_fill');
  154. // svgRuleFill.test(/\.svg$/)
  155. // .include.add(resolve('src/assets/icon/svg_fill'))
  156. // .end()
  157. // .use('svg-sprite-loader')
  158. // .loader('svg-sprite-loader')
  159. // .options({
  160. // symbolId: 'fill-[name]',
  161. // // extract: true,
  162. // // publicPath: "img/",
  163. // // spriteFilename: "svg-sprite-[hash:8].svg",
  164. // });
  165. // // config
  166. // // .plugin("svgsprite")
  167. // // .use(SVGSpriteLoaderPlugin, [{
  168. // // plainSprite: true
  169. // // }]);
  170. // // svgo 去除svg中无用元素
  171. // svgRuleFill.use('svgo-loader').loader('svgo-loader').options({
  172. // plugins: extendDefaultPlugins([])
  173. // });
  174. },
  175. devServer: {
  176. //代理配置
  177. proxy: {
  178. "/cbk": {
  179. target: "https://h5.caibeike.com.cn/", // 请求地址
  180. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  181. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  182. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  183. pathRewrite: {
  184. // 路径重写,eg:把api接口替换为''
  185. "^/cbk": "",
  186. },
  187. },
  188. },
  189. open: false, // 是否打开浏览器
  190. },
  191. };