webpack.dev.js 6.0 KB

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