webpack.dev.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. path.join(process.cwd(), 'node_modules/grapesjs/src/styles/fonts/main-fonts.svg')
  137. ],
  138. use: [
  139. {
  140. loader: 'raw-loader'
  141. }
  142. ]
  143. },
  144. {
  145. test: /\.(graphql|gql)$/,
  146. exclude: /node_modules/,
  147. use: [
  148. { loader: 'graphql-persisted-document-loader' },
  149. { loader: 'graphql-tag/loader' }
  150. ]
  151. },
  152. {
  153. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  154. use: [{
  155. loader: 'file-loader',
  156. options: {
  157. name: '[name].[ext]',
  158. outputPath: 'fonts/'
  159. }
  160. }]
  161. },
  162. {
  163. test: /.jsx$/,
  164. loader: 'babel-loader',
  165. exclude: /node_modules/,
  166. options: {
  167. presets: ['es2015', 'react']
  168. }
  169. },
  170. {
  171. test: /\.flow$/,
  172. loader: 'ignore-loader'
  173. }
  174. ]
  175. },
  176. plugins: [
  177. new VueLoaderPlugin(),
  178. new CopyWebpackPlugin([
  179. { from: 'client/static' },
  180. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  181. ], {}),
  182. new HtmlWebpackPlugin({
  183. template: 'dev/templates/master.pug',
  184. filename: '../server/views/master.pug',
  185. hash: false,
  186. inject: 'head'
  187. }),
  188. new HtmlWebpackPugPlugin(),
  189. new SimpleProgressWebpackPlugin({
  190. format: 'compact'
  191. }),
  192. new webpack.DefinePlugin({
  193. 'process.env.NODE_ENV': JSON.stringify('development'),
  194. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  195. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  196. }),
  197. new WriteFilePlugin(),
  198. new webpack.HotModuleReplacementPlugin(),
  199. new webpack.WatchIgnorePlugin([
  200. /node_modules/
  201. ])
  202. ],
  203. optimization: {
  204. namedModules: true,
  205. namedChunks: true,
  206. splitChunks: {
  207. cacheGroups: {
  208. default: {
  209. minChunks: 2,
  210. priority: -20,
  211. reuseExistingChunk: true
  212. },
  213. vendor: {
  214. test: /[\\/]node_modules[\\/]/,
  215. minChunks: 2,
  216. priority: -10
  217. }
  218. }
  219. },
  220. runtimeChunk: 'single'
  221. },
  222. resolve: {
  223. mainFields: ['browser', 'main', 'module'],
  224. symlinks: true,
  225. alias: {
  226. '@': path.join(process.cwd(), 'client'),
  227. 'vue$': 'vue/dist/vue.esm.js',
  228. 'gql': path.join(process.cwd(), 'client/graph'),
  229. 'mdi': path.join(process.cwd(), 'node_modules/vue-material-design-icons'),
  230. // Duplicates fixes:
  231. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  232. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  233. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  234. },
  235. extensions: [
  236. '.js',
  237. '.json',
  238. '.jsx',
  239. '.vue'
  240. ],
  241. modules: [
  242. 'node_modules'
  243. ]
  244. },
  245. node: {
  246. fs: 'empty'
  247. },
  248. stats: {
  249. children: false,
  250. entrypoints: false
  251. },
  252. target: 'web',
  253. watch: true
  254. }