webpack.prod.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 { CleanWebpackPlugin } = require('clean-webpack-plugin')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const HtmlWebpackPugPlugin = require('html-webpack-pug-plugin')
  11. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  12. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  13. const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
  14. const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin')
  15. const WebpackBarPlugin = require('webpackbar')
  16. const SriWebpackPlugin = require('webpack-subresource-integrity')
  17. const babelConfig = fs.readJsonSync(path.join(process.cwd(), '.babelrc'))
  18. const cacheDir = '.webpack-cache/cache'
  19. const babelDir = path.join(process.cwd(), '.webpack-cache/babel')
  20. process.noDeprecation = true
  21. fs.emptyDirSync(path.join(process.cwd(), 'assets'))
  22. module.exports = {
  23. mode: 'production',
  24. entry: {
  25. app: './client/index-app.js',
  26. legacy: './client/index-legacy.js',
  27. setup: './client/index-setup.js'
  28. },
  29. output: {
  30. path: path.join(process.cwd(), 'assets'),
  31. publicPath: '/',
  32. filename: 'js/[name].[hash].js',
  33. chunkFilename: 'js/[name].[chunkhash].js',
  34. globalObject: 'this',
  35. crossOriginLoading: 'use-credentials'
  36. },
  37. module: {
  38. rules: [
  39. {
  40. test: /\.js$/,
  41. exclude: (modulePath) => {
  42. return modulePath.includes('node_modules') && !modulePath.includes('vuetify')
  43. },
  44. use: [
  45. {
  46. loader: 'cache-loader',
  47. options: {
  48. cacheDirectory: cacheDir
  49. }
  50. },
  51. {
  52. loader: 'babel-loader',
  53. options: {
  54. ...babelConfig,
  55. cacheDirectory: babelDir
  56. }
  57. }
  58. ]
  59. },
  60. {
  61. test: /\.css$/,
  62. use: [
  63. 'style-loader',
  64. MiniCssExtractPlugin.loader,
  65. 'css-loader',
  66. 'postcss-loader'
  67. ]
  68. },
  69. {
  70. test: /\.sass$/,
  71. use: [
  72. {
  73. loader: 'cache-loader',
  74. options: {
  75. cacheDirectory: cacheDir
  76. }
  77. },
  78. 'style-loader',
  79. 'css-loader',
  80. 'postcss-loader',
  81. {
  82. loader: 'sass-loader',
  83. options: {
  84. implementation: require('sass'),
  85. sourceMap: false
  86. }
  87. }
  88. ]
  89. },
  90. {
  91. test: /\.scss$/,
  92. use: [
  93. {
  94. loader: 'cache-loader',
  95. options: {
  96. cacheDirectory: cacheDir
  97. }
  98. },
  99. 'style-loader',
  100. MiniCssExtractPlugin.loader,
  101. 'css-loader',
  102. 'postcss-loader',
  103. {
  104. loader: 'sass-loader',
  105. options: {
  106. implementation: require('sass'),
  107. sourceMap: false
  108. }
  109. },
  110. {
  111. loader: 'sass-resources-loader',
  112. options: {
  113. resources: path.join(process.cwd(), '/client/scss/global.scss')
  114. }
  115. }
  116. ]
  117. },
  118. {
  119. test: /\.vue$/,
  120. loader: 'vue-loader'
  121. },
  122. {
  123. test: /\.pug$/,
  124. exclude: [
  125. path.join(process.cwd(), 'dev')
  126. ],
  127. loader: 'pug-plain-loader'
  128. },
  129. {
  130. test: /\.(png|jpg|gif)$/,
  131. use: [
  132. {
  133. loader: 'url-loader',
  134. options: {
  135. limit: 8192
  136. }
  137. }
  138. ]
  139. },
  140. {
  141. test: /\.svg$/,
  142. exclude: [
  143. path.join(process.cwd(), 'node_modules/grapesjs')
  144. ],
  145. use: [
  146. {
  147. loader: 'file-loader',
  148. options: {
  149. name: '[name].[ext]',
  150. outputPath: 'svg/'
  151. }
  152. }
  153. ]
  154. },
  155. {
  156. test: /\.(graphql|gql)$/,
  157. exclude: /node_modules/,
  158. use: [
  159. { loader: 'graphql-persisted-document-loader' },
  160. { loader: 'graphql-tag/loader' }
  161. ]
  162. },
  163. {
  164. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  165. exclude: [
  166. path.join(process.cwd(), 'client')
  167. ],
  168. use: [{
  169. loader: 'file-loader',
  170. options: {
  171. name: '[name].[ext]',
  172. outputPath: 'fonts/'
  173. }
  174. }]
  175. },
  176. {
  177. test: /.jsx$/,
  178. loader: 'babel-loader',
  179. exclude: /node_modules/,
  180. options: {
  181. presets: ['es2015', 'react']
  182. }
  183. },
  184. {
  185. test: /\.flow$/,
  186. loader: 'ignore-loader'
  187. }
  188. ]
  189. },
  190. plugins: [
  191. new VueLoaderPlugin(),
  192. new VuetifyLoaderPlugin(),
  193. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  194. new CopyWebpackPlugin([
  195. { from: 'client/static' },
  196. { from: './node_modules/prismjs/components', to: 'js/prism' },
  197. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  198. ], {}),
  199. new MiniCssExtractPlugin({
  200. filename: 'css/bundle.[hash].css',
  201. chunkFilename: 'css/[name].[chunkhash].css'
  202. }),
  203. new HtmlWebpackPlugin({
  204. template: 'dev/templates/master.pug',
  205. filename: '../server/views/master.pug',
  206. hash: false,
  207. inject: false,
  208. excludeChunks: ['setup', 'legacy']
  209. }),
  210. new HtmlWebpackPlugin({
  211. template: 'dev/templates/legacy.pug',
  212. filename: '../server/views/legacy/master.pug',
  213. hash: false,
  214. inject: false,
  215. excludeChunks: ['setup', 'app']
  216. }),
  217. new HtmlWebpackPlugin({
  218. template: 'dev/templates/setup.pug',
  219. filename: '../server/views/setup.pug',
  220. hash: false,
  221. inject: false,
  222. excludeChunks: ['app', 'legacy']
  223. }),
  224. new HtmlWebpackPugPlugin(),
  225. new ScriptExtHtmlWebpackPlugin({
  226. sync: 'runtime.js',
  227. defaultAttribute: 'async'
  228. }),
  229. new SriWebpackPlugin({
  230. hashFuncNames: ['sha256', 'sha512'],
  231. enabled: true
  232. }),
  233. new WebpackBarPlugin({
  234. name: 'Client Assets'
  235. }),
  236. new CleanWebpackPlugin(),
  237. new OptimizeCssAssetsPlugin({
  238. cssProcessorOptions: { discardComments: { removeAll: true } },
  239. canPrint: true
  240. }),
  241. new webpack.DefinePlugin({
  242. 'process.env.NODE_ENV': JSON.stringify('production'),
  243. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  244. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  245. })
  246. ],
  247. optimization: {
  248. namedModules: true,
  249. namedChunks: true,
  250. splitChunks: {
  251. name: 'vendor',
  252. minChunks: 2
  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. }