vue.config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. const path = require("path");
  3. const defaultSettings = require("./src/settings.js");
  4. function resolve(dir) {
  5. return path.join(__dirname, dir);
  6. }
  7. const name = defaultSettings.title || "vue Element Admin"; //页面标题
  8. //如果端口设置为80,
  9. //使用管理员权限执行命令行。
  10. //例如,Mac:sudo npm run
  11. //您可以通过以下方法更改端口:
  12. //端口=9527 npm运行开发或npm运行开发--端口=9527
  13. const port = process.env.port || process.env.npm_config_port || 9528; // 开发端口
  14. // dev portal配置项说明可在中找到https://cli.vuejs.org/config/
  15. module.exports = {
  16. assetsDir: 'static',
  17. lintOnSave: false,
  18. productionSourceMap: false,
  19. //开启运行时编译模式
  20. runtimeCompiler: true,
  21. devServer: {
  22. port: port,
  23. open: true,
  24. overlay: {
  25. warnings: false,
  26. errors: true
  27. },
  28. },
  29. configureWebpack: {
  30. name: name,
  31. resolve: {
  32. alias: {
  33. "@": resolve("src")
  34. }
  35. }
  36. },
  37. chainWebpack(config) {
  38. config.plugin("preload").tap(() => [{
  39. rel: "preload",
  40. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  41. include: "initial"
  42. }]);
  43. config.plugins.delete("prefetch");
  44. // 设置svg精灵加载程序
  45. config.module
  46. .rule("svg")
  47. .exclude.add(resolve("src/icons"))
  48. .end();
  49. config.module
  50. .rule("icons")
  51. .test(/\.svg$/)
  52. .include.add(resolve("src/icons"))
  53. .end()
  54. .use("svg-sprite-loader")
  55. .loader("svg-sprite-loader")
  56. .options({
  57. symbolId: "icon-[name]"
  58. })
  59. .end();
  60. config.when(process.env.NODE_ENV !== "development", config => {
  61. config
  62. .plugin("ScriptExtHtmlWebpackPlugin")
  63. .after("html")
  64. .use("script-ext-html-webpack-plugin", [{
  65. // `runtime`必须与runtimeChunk名称相同。默认值为“运行时”`
  66. inline: /runtime\..*\.js$/
  67. }])
  68. .end();
  69. config.optimization.splitChunks({
  70. chunks: "all",
  71. cacheGroups: {
  72. libs: {
  73. name: "chunk-libs",
  74. test: /[\\/]node_modules[\\/]/,
  75. priority: 10,
  76. chunks: "initial" //仅限于最初依赖的第三方
  77. },
  78. elementUI: {
  79. name: "chunk-elementUI", //将elementUI拆分为单个包
  80. priority: 20, // 重量需要大于libs和app,否则将打包到libs或app中
  81. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ //为了适应cnpm
  82. },
  83. commons: {
  84. name: "chunk-commons",
  85. test: resolve("src/components"), // 您可以自定义您的规则
  86. minChunks: 3, // 最小公共数
  87. priority: 5,
  88. reuseExistingChunk: true
  89. }
  90. }
  91. });
  92. config.optimization.runtimeChunk("single");
  93. });
  94. },
  95. css: {
  96. loaderOptions: {
  97. sass: {
  98. prependData: '\n @import "@/styles/variables.scss";\n '
  99. }
  100. }
  101. },
  102. publicPath: './'
  103. };