webpack.dev.js 5.8 KB

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