webpack.dev.js 7.0 KB

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