webpack.dev.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-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. sourceMap: false
  84. }
  85. }
  86. ]
  87. },
  88. {
  89. test: /\.scss$/,
  90. use: [
  91. {
  92. loader: 'cache-loader',
  93. options: {
  94. cacheDirectory: cacheDir
  95. }
  96. },
  97. 'style-loader',
  98. 'css-loader',
  99. 'postcss-loader',
  100. {
  101. loader: 'sass-loader',
  102. options: {
  103. implementation: require('sass'),
  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. },
  175. plugins: [
  176. new VueLoaderPlugin(),
  177. new VuetifyLoaderPlugin(),
  178. new MomentTimezoneDataPlugin({
  179. startYear: 2017,
  180. endYear: (new Date().getFullYear()) + 5
  181. }),
  182. new CopyWebpackPlugin([
  183. { from: 'client/static' },
  184. { from: './node_modules/prismjs/components', to: 'js/prism' }
  185. ], {}),
  186. new HtmlWebpackPlugin({
  187. template: 'dev/templates/master.pug',
  188. filename: '../server/views/master.pug',
  189. hash: false,
  190. inject: false,
  191. excludeChunks: ['setup', 'legacy']
  192. }),
  193. new HtmlWebpackPlugin({
  194. template: 'dev/templates/legacy.pug',
  195. filename: '../server/views/legacy/master.pug',
  196. hash: false,
  197. inject: false,
  198. excludeChunks: ['setup', 'app']
  199. }),
  200. new HtmlWebpackPlugin({
  201. template: 'dev/templates/setup.pug',
  202. filename: '../server/views/setup.pug',
  203. hash: false,
  204. inject: false,
  205. excludeChunks: ['app', 'legacy']
  206. }),
  207. new HtmlWebpackPugPlugin(),
  208. new SriWebpackPlugin({
  209. hashFuncNames: ['sha256', 'sha512'],
  210. enabled: false
  211. }),
  212. new WebpackBarPlugin({
  213. name: 'Client Assets'
  214. }),
  215. new webpack.DefinePlugin({
  216. 'process.env.NODE_ENV': JSON.stringify('development'),
  217. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  218. }),
  219. new WriteFilePlugin(),
  220. new webpack.HotModuleReplacementPlugin(),
  221. new webpack.WatchIgnorePlugin([
  222. /node_modules/
  223. ])
  224. ],
  225. optimization: {
  226. namedModules: true,
  227. namedChunks: true,
  228. splitChunks: {
  229. cacheGroups: {
  230. default: {
  231. minChunks: 2,
  232. priority: -20,
  233. reuseExistingChunk: true
  234. },
  235. vendor: {
  236. test: /[\\/]node_modules[\\/]/,
  237. minChunks: 2,
  238. priority: -10
  239. }
  240. }
  241. },
  242. runtimeChunk: 'single'
  243. },
  244. resolve: {
  245. mainFields: ['browser', 'main', 'module'],
  246. symlinks: true,
  247. alias: {
  248. '@': path.join(process.cwd(), 'client'),
  249. 'vue$': 'vue/dist/vue.esm.js',
  250. 'gql': path.join(process.cwd(), 'client/graph'),
  251. // Duplicates fixes:
  252. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  253. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  254. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  255. },
  256. extensions: [
  257. '.js',
  258. '.json',
  259. '.vue'
  260. ],
  261. modules: [
  262. 'node_modules'
  263. ]
  264. },
  265. node: {
  266. fs: 'empty'
  267. },
  268. stats: {
  269. children: false,
  270. entrypoints: false
  271. },
  272. target: 'web',
  273. watch: true
  274. }