webpack.dev.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. const webpack = require('webpack')
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const { VueLoaderPlugin } = require('vue-loader')
  5. const CopyWebpackPlugin = require('copy-webpack-plugin')
  6. const HtmlWebpackPlugin = require('html-webpack-plugin')
  7. const HtmlWebpackPugPlugin = require('html-webpack-pug-plugin')
  8. const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin')
  9. const WriteFilePlugin = require('write-file-webpack-plugin')
  10. const babelConfig = fs.readJsonSync(path.join(process.cwd(), '.babelrc'))
  11. const cacheDir = '.webpack-cache/cache'
  12. const babelDir = path.join(process.cwd(), '.webpack-cache/babel')
  13. process.noDeprecation = true
  14. fs.emptyDirSync(path.join(process.cwd(), 'assets'))
  15. module.exports = {
  16. mode: 'development',
  17. entry: {
  18. client: ['./client/index.js', 'webpack-hot-middleware/client']
  19. },
  20. output: {
  21. path: path.join(process.cwd(), 'assets'),
  22. publicPath: '/',
  23. filename: 'js/[name].js',
  24. chunkFilename: 'js/[name].js',
  25. globalObject: 'this',
  26. pathinfo: true
  27. },
  28. module: {
  29. rules: [
  30. {
  31. test: /\.js$/,
  32. exclude: /node_modules/,
  33. use: [
  34. {
  35. loader: 'cache-loader',
  36. options: {
  37. cacheDirectory: cacheDir
  38. }
  39. },
  40. {
  41. loader: 'babel-loader',
  42. options: {
  43. ...babelConfig,
  44. cacheDirectory: babelDir
  45. }
  46. }
  47. ]
  48. },
  49. {
  50. test: /\.css$/,
  51. use: [
  52. 'style-loader',
  53. 'css-loader',
  54. 'postcss-loader'
  55. ]
  56. },
  57. {
  58. test: /\.scss$/,
  59. use: [
  60. {
  61. loader: 'cache-loader',
  62. options: {
  63. cacheDirectory: cacheDir
  64. }
  65. },
  66. 'style-loader',
  67. 'css-loader',
  68. 'postcss-loader',
  69. {
  70. loader: 'sass-loader',
  71. options: {
  72. sourceMap: false
  73. }
  74. },
  75. {
  76. loader: 'sass-resources-loader',
  77. options: {
  78. resources: path.join(process.cwd(), '/client/scss/global.scss')
  79. }
  80. }
  81. ]
  82. },
  83. {
  84. test: /\.styl$/,
  85. use: [
  86. 'style-loader',
  87. 'css-loader',
  88. 'postcss-loader',
  89. 'stylus-loader'
  90. ]
  91. },
  92. {
  93. test: /\.vue$/,
  94. loader: 'vue-loader'
  95. },
  96. {
  97. test: /\.pug$/,
  98. exclude: [
  99. path.join(process.cwd(), 'dev')
  100. ],
  101. loader: 'pug-plain-loader'
  102. },
  103. {
  104. test: /\.(png|jpg|gif)$/,
  105. use: [
  106. {
  107. loader: 'url-loader',
  108. options: {
  109. limit: 8192
  110. }
  111. }
  112. ]
  113. },
  114. {
  115. test: /\.svg$/,
  116. exclude: [
  117. path.join(process.cwd(), 'client/svg')
  118. ],
  119. use: [
  120. {
  121. loader: 'file-loader',
  122. options: {
  123. name: '[name].[ext]',
  124. outputPath: 'svg/'
  125. }
  126. }
  127. ]
  128. },
  129. {
  130. test: /\.svg$/,
  131. include: [
  132. path.join(process.cwd(), 'client/svg')
  133. ],
  134. use: [
  135. {
  136. loader: 'raw-loader'
  137. }
  138. ]
  139. },
  140. {
  141. test: /\.(graphql|gql)$/,
  142. exclude: /node_modules/,
  143. use: [
  144. { loader: 'graphql-persisted-document-loader' },
  145. { loader: 'graphql-tag/loader' }
  146. ]
  147. },
  148. {
  149. test: /.jsx$/,
  150. loader: 'babel-loader',
  151. exclude: /node_modules/,
  152. options: {
  153. presets: ['es2015', 'react']
  154. }
  155. },
  156. {
  157. test: /\.flow$/,
  158. loader: 'ignore-loader'
  159. }
  160. ]
  161. },
  162. plugins: [
  163. new VueLoaderPlugin(),
  164. new CopyWebpackPlugin([
  165. { from: 'client/static' },
  166. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  167. ], {}),
  168. new HtmlWebpackPlugin({
  169. template: 'dev/templates/master.pug',
  170. filename: '../server/views/master.pug',
  171. hash: false,
  172. inject: 'head'
  173. }),
  174. new HtmlWebpackPugPlugin(),
  175. new SimpleProgressWebpackPlugin({
  176. format: 'compact'
  177. }),
  178. new webpack.DefinePlugin({
  179. 'process.env': { NODE_ENV: '"development"' },
  180. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  181. }),
  182. new WriteFilePlugin(),
  183. new webpack.HotModuleReplacementPlugin(),
  184. new webpack.WatchIgnorePlugin([
  185. /node_modules/
  186. ])
  187. ],
  188. optimization: {
  189. namedModules: true,
  190. namedChunks: true,
  191. splitChunks: {
  192. cacheGroups: {
  193. default: {
  194. minChunks: 2,
  195. priority: -20,
  196. reuseExistingChunk: true
  197. },
  198. vendor: {
  199. test: /[\\/]node_modules[\\/]/,
  200. minChunks: 2,
  201. priority: -10
  202. }
  203. }
  204. },
  205. runtimeChunk: 'single'
  206. },
  207. resolve: {
  208. mainFields: ['browser', 'main', 'module'],
  209. symlinks: true,
  210. alias: {
  211. '@': path.join(process.cwd(), 'client'),
  212. 'vue$': 'vue/dist/vue.esm.js',
  213. 'gql': path.join(process.cwd(), 'client/graph'),
  214. 'mdi': path.join(process.cwd(), 'node_modules/vue-material-design-icons'),
  215. // Duplicates fixes:
  216. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  217. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  218. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  219. },
  220. extensions: [
  221. '.js',
  222. '.json',
  223. '.jsx',
  224. '.vue'
  225. ],
  226. modules: [
  227. 'node_modules'
  228. ]
  229. },
  230. node: {
  231. fs: 'empty'
  232. },
  233. stats: {
  234. children: false,
  235. entrypoints: false
  236. },
  237. target: 'web',
  238. watch: true
  239. }