webpack.client.config.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const webpack = require('webpack')
  2. const merge = require('webpack-merge')
  3. const base = require('./webpack.base.config')
  4. const SWPrecachePlugin = require('sw-precache-webpack-plugin')
  5. const VueSSRClientPlugin = require('vue-server-renderer/client-plugin')
  6. const config = merge(base, {
  7. entry: {
  8. app: './src/entry-client.js'
  9. },
  10. resolve: {
  11. alias: {
  12. 'create-api': './create-api-client.js'
  13. }
  14. },
  15. plugins: [
  16. // strip dev-only code in Vue source
  17. new webpack.DefinePlugin({
  18. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
  19. 'process.env.VUE_ENV': '"client"'
  20. }),
  21. // extract vendor chunks for better caching
  22. new webpack.optimize.CommonsChunkPlugin({
  23. name: 'vendor',
  24. minChunks: function (module) {
  25. // a module is extracted into the vendor chunk if...
  26. return (
  27. // it's inside node_modules
  28. /node_modules/.test(module.context) &&
  29. // and not a CSS file (due to extract-text-webpack-plugin limitation)
  30. !/\.css$/.test(module.request)
  31. )
  32. }
  33. }),
  34. // extract webpack runtime & manifest to avoid vendor chunk hash changing
  35. // on every build.
  36. new webpack.optimize.CommonsChunkPlugin({
  37. name: 'manifest'
  38. }),
  39. new VueSSRClientPlugin()
  40. ]
  41. })
  42. if (process.env.NODE_ENV === 'production') {
  43. config.plugins.push(
  44. // auto generate service worker
  45. new SWPrecachePlugin({
  46. cacheId: 'vue-hn',
  47. filename: 'service-worker.js',
  48. minify: true,
  49. dontCacheBustUrlsMatching: /./,
  50. staticFileGlobsIgnorePatterns: [/\.map$/, /\.json$/],
  51. runtimeCaching: [{
  52. urlPattern: '/',
  53. handler: 'networkFirst'
  54. },
  55. {
  56. urlPattern: /\/(top|new|show|ask|jobs)/,
  57. handler: 'networkFirst'
  58. },
  59. {
  60. urlPattern: '/item/:id',
  61. handler: 'networkFirst'
  62. },
  63. {
  64. urlPattern: '/user/:id',
  65. handler: 'networkFirst'
  66. }
  67. ]
  68. })
  69. )
  70. }
  71. module.exports = config