webpack.dev.js 6.9 KB

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