vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const webpack = require('webpack')
  2. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  3. const productionGzipExtensions = ['js', 'css']
  4. // const isProduction = process.env.NODE_ENV === 'production'
  5. // 预渲染
  6. const PrerenderSPAPlugin = require('prerender-spa-plugin');
  7. const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
  8. const path = require('path');
  9. module.exports = {
  10. // 基本路径 baseURL已经过时
  11. publicPath: './',
  12. // 输出文件目录
  13. outputDir: 'dist',
  14. // eslint-loader 是否在保存的时候检查
  15. lintOnSave: false,
  16. // use the full build with in-browser compiler?
  17. // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
  18. // compiler: false,
  19. // webpack配置
  20. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  21. chainWebpack: () => {},
  22. configureWebpack: config => {
  23. config.entry = ['babel-polyfill', './src/main.js']
  24. if (process.env.NODE_ENV === 'production') {
  25. config.plugins.push(
  26. new PrerenderSPAPlugin({
  27. // 生成文件的路径,也可以与webpakc打包的一致。
  28. // 下面这句话非常重要!!!
  29. // 这个目录只能有一级,如果目录层次大于一级,在生成的时候不会有任何错误提示,在预渲染的时候只会卡着不动。
  30. staticDir: path.join(__dirname, './dist'),
  31. indexPath: path.join(__dirname, './dist', 'index.html'),
  32. // 对应自己的路由文件,比如a有参数,就需要写成 /a/param1。
  33. routes: ['/home', '/fittingRoom', '/save', '/share', '/giftBox', '/giftBoxFive'],
  34. // renderer: new PrerenderSPAPlugin.PuppeteerRenderer({//这样写renderAfterTime生效了
  35. // renderAfterTime: 5000
  36. // }),
  37. // renderAfterDocumentEvent: 'render-event'
  38. // 这个很重要,如果没有配置这段,也不会进行预编译
  39. renderer: new Renderer({
  40. inject: {
  41. foo: 'bar'
  42. },
  43. headless: false,
  44. // 在 main.js 中 document.dispatchEvent(new Event('render-event')),两者的事件名称要对应上。
  45. renderAfterDocumentEvent: 'render-event'
  46. })
  47. })
  48. )
  49. };
  50. plugins: [
  51. // 配置compression-webpack-plugin压缩
  52. new CompressionWebpackPlugin({
  53. algorithm: 'gzip',
  54. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  55. threshold: 1024,
  56. minRatio: 0.8
  57. }),
  58. new webpack.optimize.LimitChunkCountPlugin({
  59. maxChunks: 5,
  60. minChunkSize: 100
  61. })
  62. ]
  63. },
  64. // vue-loader 配置项
  65. // https://vue-loader.vuejs.org/en/options.html
  66. // vueLoader: {},
  67. // 生产环境是否生成 sourceMap 文件
  68. productionSourceMap: false,
  69. // css相关配置
  70. css: {
  71. // 是否使用css分离插件 ExtractTextPlugin
  72. extract: true,
  73. // 开启 CSS source maps?
  74. sourceMap: false,
  75. // css预设器配置项
  76. loaderOptions: {
  77. postcss: {
  78. plugins: [
  79. require('postcss-px2rem-exclude')({ "remUnit": 75, "exclude": /node_modules/i }), // 换算的基数
  80. ]
  81. }
  82. },
  83. // 启用 CSS modules for all css / pre-processor files.
  84. modules: false
  85. },
  86. // use thread-loader for babel & TS in production build
  87. // enabled by default if the machine has more than 1 cores
  88. parallel: require('os').cpus().length > 1,
  89. // 是否启用dll
  90. // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#dll-mode
  91. // dll: false,
  92. // PWA 插件相关配置
  93. // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
  94. pwa: {},
  95. // webpack-dev-server 相关配置
  96. devServer: {
  97. hot:true,//自动保存
  98. overlay: { // 让浏览器 overlay 同时显示警告和错误
  99. warnings: true,
  100. errors: true
  101. },
  102. // host: "localhost",
  103. host: "0.0.0.0",
  104. port: 8080, // 端口号
  105. https: false, // https:{type:Boolean}
  106. open: false, //配置后自动启动浏览器
  107. hotOnly: true, // 热更新
  108. // proxy: {
  109. // '/api': {
  110. // target: 'http://ttr.yergoo.com',//服务器协议、ip和端口号
  111. // secure: false, // 如果是https接口,需要配置这个参数
  112. // ws: false,//是否代理websockets
  113. // changeOrigin: true,
  114. // pathRewrite:{
  115. // '^/api':''
  116. // }
  117. // },
  118. // },
  119. },
  120. // 第三方插件配置
  121. pluginOptions: {
  122. // ...
  123. }
  124. }