webpack.dev.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. path.join(process.cwd(), 'node_modules/grapesjs')
  121. ],
  122. use: [
  123. {
  124. loader: 'file-loader',
  125. options: {
  126. name: '[name].[ext]',
  127. outputPath: 'svg/'
  128. }
  129. }
  130. ]
  131. },
  132. {
  133. test: /\.svg$/,
  134. include: [
  135. path.join(process.cwd(), 'client/svg')
  136. ],
  137. use: [
  138. {
  139. loader: 'raw-loader'
  140. }
  141. ]
  142. },
  143. {
  144. test: /\.(graphql|gql)$/,
  145. exclude: /node_modules/,
  146. use: [
  147. { loader: 'graphql-persisted-document-loader' },
  148. { loader: 'graphql-tag/loader' }
  149. ]
  150. },
  151. {
  152. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  153. exclude: [
  154. path.join(process.cwd(), 'client')
  155. ],
  156. use: [{
  157. loader: 'file-loader',
  158. options: {
  159. name: '[name].[ext]',
  160. outputPath: 'fonts/'
  161. }
  162. }]
  163. },
  164. {
  165. test: /.jsx$/,
  166. loader: 'babel-loader',
  167. exclude: /node_modules/,
  168. options: {
  169. presets: ['es2015', 'react']
  170. }
  171. },
  172. {
  173. test: /\.flow$/,
  174. loader: 'ignore-loader'
  175. }
  176. ]
  177. },
  178. plugins: [
  179. new VueLoaderPlugin(),
  180. new CopyWebpackPlugin([
  181. { from: 'client/static' },
  182. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  183. ], {}),
  184. new HtmlWebpackPlugin({
  185. template: 'dev/templates/master.pug',
  186. filename: '../server/views/master.pug',
  187. hash: false,
  188. inject: 'head'
  189. }),
  190. new HtmlWebpackPugPlugin(),
  191. new SimpleProgressWebpackPlugin({
  192. format: 'compact'
  193. }),
  194. new webpack.DefinePlugin({
  195. 'process.env.NODE_ENV': JSON.stringify('development'),
  196. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  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. }