webpack.dev.js 7.2 KB

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