You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
7.4 KiB

  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(), 'client/svg'),
  141. path.join(process.cwd(), 'node_modules/grapesjs')
  142. ],
  143. use: [
  144. {
  145. loader: 'file-loader',
  146. options: {
  147. name: '[name].[ext]',
  148. outputPath: 'svg/'
  149. }
  150. }
  151. ]
  152. },
  153. {
  154. test: /\.svg$/,
  155. include: [
  156. path.join(process.cwd(), 'client/svg')
  157. ],
  158. use: [
  159. {
  160. loader: 'raw-loader'
  161. }
  162. ]
  163. },
  164. {
  165. test: /\.(graphql|gql)$/,
  166. exclude: /node_modules/,
  167. use: [
  168. { loader: 'graphql-persisted-document-loader' },
  169. { loader: 'graphql-tag/loader' }
  170. ]
  171. },
  172. {
  173. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  174. exclude: [
  175. path.join(process.cwd(), 'client')
  176. ],
  177. use: [{
  178. loader: 'file-loader',
  179. options: {
  180. name: '[name].[ext]',
  181. outputPath: 'fonts/'
  182. }
  183. }]
  184. },
  185. {
  186. test: /.jsx$/,
  187. loader: 'babel-loader',
  188. exclude: /node_modules/,
  189. options: {
  190. presets: ['es2015', 'react']
  191. }
  192. },
  193. {
  194. test: /\.flow$/,
  195. loader: 'ignore-loader'
  196. }
  197. ]
  198. },
  199. plugins: [
  200. new VueLoaderPlugin(),
  201. new VuetifyLoaderPlugin(),
  202. new CopyWebpackPlugin([
  203. { from: 'client/static' },
  204. { from: './node_modules/prismjs/components', to: 'js/prism' },
  205. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  206. ], {}),
  207. new HtmlWebpackPlugin({
  208. template: 'dev/templates/master.pug',
  209. filename: '../server/views/master.pug',
  210. hash: false,
  211. inject: false,
  212. excludeChunks: ['setup', 'legacy']
  213. }),
  214. new HtmlWebpackPlugin({
  215. template: 'dev/templates/legacy.pug',
  216. filename: '../server/views/legacy/master.pug',
  217. hash: false,
  218. inject: false,
  219. excludeChunks: ['setup', 'app']
  220. }),
  221. new HtmlWebpackPlugin({
  222. template: 'dev/templates/setup.pug',
  223. filename: '../server/views/setup.pug',
  224. hash: false,
  225. inject: false,
  226. excludeChunks: ['app', 'legacy']
  227. }),
  228. new HtmlWebpackPugPlugin(),
  229. new SriWebpackPlugin({
  230. hashFuncNames: ['sha256', 'sha512'],
  231. enabled: false
  232. }),
  233. new WebpackBarPlugin({
  234. name: 'Client Assets'
  235. }),
  236. new webpack.DefinePlugin({
  237. 'process.env.NODE_ENV': JSON.stringify('development'),
  238. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  239. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  240. }),
  241. new WriteFilePlugin(),
  242. new webpack.HotModuleReplacementPlugin(),
  243. new webpack.WatchIgnorePlugin([
  244. /node_modules/
  245. ])
  246. ],
  247. optimization: {
  248. namedModules: true,
  249. namedChunks: true,
  250. splitChunks: {
  251. cacheGroups: {
  252. default: {
  253. minChunks: 2,
  254. priority: -20,
  255. reuseExistingChunk: true
  256. },
  257. vendor: {
  258. test: /[\\/]node_modules[\\/]/,
  259. minChunks: 2,
  260. priority: -10
  261. }
  262. }
  263. },
  264. runtimeChunk: 'single'
  265. },
  266. resolve: {
  267. mainFields: ['browser', 'main', 'module'],
  268. symlinks: true,
  269. alias: {
  270. '@': path.join(process.cwd(), 'client'),
  271. 'vue$': 'vue/dist/vue.esm.js',
  272. 'gql': path.join(process.cwd(), 'client/graph'),
  273. // Duplicates fixes:
  274. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  275. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  276. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  277. },
  278. extensions: [
  279. '.js',
  280. '.json',
  281. '.jsx',
  282. '.vue'
  283. ],
  284. modules: [
  285. 'node_modules'
  286. ]
  287. },
  288. node: {
  289. fs: 'empty'
  290. },
  291. stats: {
  292. children: false,
  293. entrypoints: false
  294. },
  295. target: 'web',
  296. watch: true
  297. }