vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const path = require("path");
  2. const resolve = dir => path.join(__dirname, dir);
  3. //用于生产环境去除多余的css
  4. const PurgecssPlugin = require("purgecss-webpack-plugin");
  5. //全局文件路径
  6. const glob = require("glob-all");
  7. //压缩代码并去掉console
  8. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  9. //代码打包zip
  10. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  11. const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
  12. module.exports = {
  13. // 废弃baseUrl 一般运维会配置好的
  14. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  15. //打包的输出目录
  16. // outputDir: "dist/",
  17. //保存时是否校验
  18. lintOnSave: true,
  19. //如果文件等设置
  20. pages: {
  21. index: {
  22. entry: "src/main.js",
  23. template: "public/index.html",
  24. filename: "index.html"
  25. }
  26. },
  27. //静态资源打包路径
  28. assetsDir: "static",
  29. //默认false 可以加快打包
  30. productionSourceMap: false,
  31. //打包后的启动文件
  32. indexPath: "congfigtest.html",
  33. //打包文件是否使用hash
  34. filenameHashing: true,
  35. runtimeCompiler: false,
  36. transpileDependencies: [],
  37. //打包的css路径及命名
  38. css: {
  39. modules: false,
  40. //vue 文件中修改css 不生效 注释掉 extract:true
  41. extract: {
  42. filename: "style/[name].[hash:8].css",
  43. chunkFilename: "style/[name].[hash:8].css"
  44. },
  45. sourceMap: false,
  46. loaderOptions: {
  47. css: {},
  48. less: {
  49. // 向全局less样式传入共享的全局变量
  50. // data: `@import "~assets/less/variables.less";$src: "${process.env.VUE_APP_SRC}";`
  51. },
  52. // postcss 设置
  53. postcss: {
  54. plugins: [
  55. require("postcss-px-to-viewport")({
  56. viewportWidth: 750, // 视窗的宽度,对应的是我们设计稿的宽度,一般是750
  57. viewportHeight: 1334, // 视窗的高度,根据750设备的宽度来指定,一般指定1334,也可以不配置
  58. unitPrecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
  59. viewportUnit: "vw", // 指定需要转换成的视窗单位,建议使用vw
  60. selectorBlackList: [".ignore", ".hairlines"], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
  61. minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
  62. mediaQuery: false // 允许在媒体查询中转换`px`
  63. })
  64. ]
  65. }
  66. }
  67. },
  68. //webpack 链式配置 默认已经配置好了 node_moudles/@vue
  69. chainWebpack: config => {
  70. // 修复HMR
  71. config.resolve.symlinks(true);
  72. // 修复Lazy loading routes 按需加载的问题,如果没有配置按需加载不需要写,会报错
  73. // config.plugin("html").tap(args => {
  74. // args[0].chunksSortMode = "none";
  75. // return args;
  76. // });
  77. //添加别名
  78. config.resolve.alias
  79. .set("@", resolve("src"))
  80. .set("assets", resolve("src/assets"))
  81. .set("components", resolve("src/components"))
  82. .set("layout", resolve("src/layout"))
  83. .set("base", resolve("src/base"))
  84. .set("static", resolve("src/static"));
  85. // 压缩图片
  86. config.module
  87. .rule("images")
  88. .use("image-webpack-loader")
  89. .loader("image-webpack-loader")
  90. .options({
  91. mozjpeg: { progressive: true, quality: 65 },
  92. optipng: { enabled: false },
  93. pngquant: { quality: "65-90", speed: 4 },
  94. gifsicle: { interlaced: false },
  95. webp: { quality: 75 }
  96. });
  97. },
  98. //webpack 配置
  99. configureWebpack: config => {
  100. const plugins = [];
  101. //去掉不用的css 多余的css
  102. plugins.push(
  103. new PurgecssPlugin({
  104. paths: glob.sync([path.join(__dirname, "./**/*.vue")]),
  105. extractors: [
  106. {
  107. extractor: class Extractor {
  108. static extract(content) {
  109. const validSection = content.replace(
  110. /<style([\s\S]*?)<\/style>+/gim,
  111. ""
  112. );
  113. return validSection.match(/[A-Za-z0-9-_:/]+/g) || [];
  114. }
  115. },
  116. extensions: ["html", "vue"]
  117. }
  118. ],
  119. whitelist: ["html", "body"],
  120. whitelistPatterns: [/el-.*/],
  121. whitelistPatternsChildren: [/^token/, /^pre/, /^code/]
  122. })
  123. );
  124. //启用代码压缩
  125. plugins.push(
  126. new UglifyJsPlugin({
  127. uglifyOptions: {
  128. compress: {
  129. warnings: false,
  130. drop_console: true,
  131. drop_debugger: false,
  132. // pure_funcs: ["console.log"] //移除console
  133. }
  134. },
  135. sourceMap: false,
  136. parallel: true
  137. })
  138. ),
  139. //代码压缩打包
  140. plugins.push(
  141. new CompressionWebpackPlugin({
  142. filename: "[path].gz[query]",
  143. algorithm: "gzip",
  144. test: productionGzipExtensions,
  145. threshold: 10240,
  146. minRatio: 0.8
  147. })
  148. );
  149. config.plugins = [...config.plugins, ...plugins];
  150. },
  151. parallel: require("os").cpus().length > 1,
  152. pluginOptions: {},
  153. pwa: {},
  154. //设置代理
  155. // devServer: {
  156. // port: 8080,
  157. // host: "0.0.0.0",
  158. // https: false,
  159. // open: true,
  160. // openPage: "about",
  161. // hot: true,
  162. // disableHostCheck: true,
  163. // proxy: {
  164. // "/api": {
  165. // target: "https://cdn.awenliang.cn",
  166. // ws: true,
  167. // changeOrigin: true
  168. // },
  169. // "/foo": {
  170. // target: "https://cdn.awenliang.cn",
  171. // ws: true,
  172. // changeOrigin: true
  173. // }
  174. // }
  175. // }
  176. };