健康同学微信公众号h5项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.1KB

  1. const path = require('path');
  2. const defaultSettings = require('./src/config/index.js');
  3. /* const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; */
  4. const resolve = dir => path.join(__dirname, dir);
  5. // page title
  6. const name = defaultSettings.title || 'health-student';
  7. // 生产环境,测试和正式
  8. const IS_PROD = ['production', 'test'].includes(process.env.NODE_ENV);
  9. const { defineConfig } = require('@vue/cli-service');
  10. module.exports = defineConfig({
  11. publicPath: './', // 署应用包时的基本 URL。 vue-router hash 模式使用
  12. // publicPath: '/app/', //署应用包时的基本 URL。 vue-router history模式使用
  13. outputDir: 'dist', // 生产环境构建文件的目录
  14. lintOnSave: !IS_PROD,
  15. productionSourceMap: false, // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  16. devServer: {
  17. port: 9093, // 端口
  18. open: false, // 启动后打开浏览器
  19. client: {
  20. overlay: {
  21. // 当出现编译器错误或警告时,在浏览器中显示全屏覆盖层
  22. warnings: false,
  23. errors: false
  24. }
  25. }
  26. // proxy: {
  27. // //配置跨域
  28. // '/api': {
  29. // target: "https://test.xxx.com",
  30. // // ws:true,
  31. // changOrigin:true,
  32. // pathRewrite:{
  33. // '^/api':'/'
  34. // }
  35. // }
  36. // }
  37. },
  38. css: {
  39. extract: true, // 是否将组件中的 CSS 提取至一个独立的 CSS 文件中 (而不是动态注入到 JavaScript 中的 inline 代码)。
  40. sourceMap: false,
  41. loaderOptions: {
  42. scss: {
  43. // 向全局sass样式传入共享的全局变量, $src可以配置图片cdn前缀
  44. // 详情: https://cli.vuejs.org/guide/css.html#passing-options-to-pre-processor-loaders
  45. additionalData: `
  46. @import "assets/css/mixin.scss";
  47. @import "assets/css/variables.scss";
  48. `
  49. }
  50. }
  51. },
  52. configureWebpack: config => {
  53. config.name = name;
  54. // 为生产环境修改配置...
  55. },
  56. chainWebpack: config => {
  57. // 别名 alias
  58. config.resolve.alias
  59. .set('@', resolve('src'))
  60. .set('assets', resolve('src/assets'))
  61. .set('api', resolve('src/api'))
  62. .set('views', resolve('src/views'))
  63. .set('components', resolve('src/components'));
  64. /**
  65. * 设置保留空格
  66. */
  67. config.module
  68. .rule('vue')
  69. .use('vue-loader')
  70. .loader('vue-loader')
  71. .tap(options => {
  72. options.compilerOptions.preserveWhitespace = true;
  73. return options;
  74. })
  75. .end();
  76. /**
  77. * 打包分析
  78. */
  79. if (IS_PROD) {
  80. /* config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
  81. {
  82. analyzerMode: 'static'
  83. }
  84. ]); */
  85. }
  86. config
  87. // https://webpack.js.org/configuration/devtool/#development
  88. .when(!IS_PROD, config => config.devtool('cheap-source-map'));
  89. config.when(IS_PROD, config => {
  90. config.optimization.splitChunks({
  91. chunks: 'all',
  92. cacheGroups: {
  93. // cacheGroups 下可以可以配置多个组,每个组根据test设置条件,符合test条件的模块
  94. commons: {
  95. name: 'chunk-commons',
  96. test: resolve('src/components'),
  97. minChunks: 3, // 被至少用三次以上打包分离
  98. priority: 5, // 优先级
  99. reuseExistingChunk: true // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
  100. },
  101. node_vendors: {
  102. name: 'chunk-libs',
  103. chunks: 'initial', // 只打包初始时依赖的第三方
  104. test: /[\\/]node_modules[\\/]/,
  105. priority: 10
  106. },
  107. vantUI: {
  108. name: 'chunk-vantUI', // 单独将 vantUI 拆包
  109. priority: 20, // 数字大权重到,满足多个 cacheGroups 的条件时候分到权重高的
  110. test: /[\\/]node_modules[\\/]_?vant(.*)/
  111. }
  112. }
  113. });
  114. config.optimization.runtimeChunk('single');
  115. });
  116. }
  117. });