webpack.dev.js 7.1 KB

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