webpack.prod.js 7.4 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 { CleanWebpackPlugin } = require('clean-webpack-plugin')
  9. const CopyWebpackPlugin = require('copy-webpack-plugin')
  10. const HtmlWebpackPlugin = require('html-webpack-plugin')
  11. const HtmlWebpackPugPlugin = require('html-webpack-pug-plugin')
  12. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  13. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  14. const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
  15. const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin')
  16. const WebpackBarPlugin = require('webpackbar')
  17. const SriWebpackPlugin = require('webpack-subresource-integrity')
  18. const babelConfig = fs.readJsonSync(path.join(process.cwd(), '.babelrc'))
  19. const cacheDir = '.webpack-cache/cache'
  20. const babelDir = path.join(process.cwd(), '.webpack-cache/babel')
  21. process.noDeprecation = true
  22. fs.emptyDirSync(path.join(process.cwd(), 'assets'))
  23. module.exports = {
  24. mode: 'production',
  25. entry: {
  26. app: './client/index-app.js',
  27. legacy: './client/index-legacy.js',
  28. setup: './client/index-setup.js'
  29. },
  30. output: {
  31. path: path.join(process.cwd(), 'assets'),
  32. publicPath: '/',
  33. filename: 'js/[name].[hash].js',
  34. chunkFilename: 'js/[name].[chunkhash].js',
  35. globalObject: 'this',
  36. crossOriginLoading: 'use-credentials'
  37. },
  38. module: {
  39. rules: [
  40. {
  41. test: /\.js$/,
  42. exclude: /node_modules/,
  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. MiniCssExtractPlugin.loader,
  64. 'css-loader',
  65. 'postcss-loader'
  66. ]
  67. },
  68. {
  69. test: /\.sass$/,
  70. use: [
  71. {
  72. loader: 'cache-loader',
  73. options: {
  74. cacheDirectory: cacheDir
  75. }
  76. },
  77. 'style-loader',
  78. 'css-loader',
  79. 'postcss-loader',
  80. {
  81. loader: 'sass-loader',
  82. options: {
  83. implementation: require('sass'),
  84. fiber: Fiber,
  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. fiber: Fiber,
  108. sourceMap: false
  109. }
  110. },
  111. {
  112. loader: 'sass-resources-loader',
  113. options: {
  114. resources: path.join(process.cwd(), '/client/scss/global.scss')
  115. }
  116. }
  117. ]
  118. },
  119. {
  120. test: /\.vue$/,
  121. loader: 'vue-loader'
  122. },
  123. {
  124. test: /\.pug$/,
  125. exclude: [
  126. path.join(process.cwd(), 'dev')
  127. ],
  128. loader: 'pug-plain-loader'
  129. },
  130. {
  131. test: /\.(png|jpg|gif)$/,
  132. use: [
  133. {
  134. loader: 'url-loader',
  135. options: {
  136. limit: 8192
  137. }
  138. }
  139. ]
  140. },
  141. {
  142. test: /\.svg$/,
  143. exclude: [
  144. path.join(process.cwd(), 'node_modules/grapesjs')
  145. ],
  146. use: [
  147. {
  148. loader: 'file-loader',
  149. options: {
  150. name: '[name].[ext]',
  151. outputPath: 'svg/'
  152. }
  153. }
  154. ]
  155. },
  156. {
  157. test: /\.(graphql|gql)$/,
  158. exclude: /node_modules/,
  159. use: [
  160. { loader: 'graphql-persisted-document-loader' },
  161. { loader: 'graphql-tag/loader' }
  162. ]
  163. },
  164. {
  165. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  166. exclude: [
  167. path.join(process.cwd(), 'client')
  168. ],
  169. use: [{
  170. loader: 'file-loader',
  171. options: {
  172. name: '[name].[ext]',
  173. outputPath: 'fonts/'
  174. }
  175. }]
  176. },
  177. {
  178. test: /.jsx$/,
  179. loader: 'babel-loader',
  180. exclude: /node_modules/,
  181. options: {
  182. presets: ['es2015', 'react']
  183. }
  184. },
  185. {
  186. test: /\.flow$/,
  187. loader: 'ignore-loader'
  188. }
  189. ]
  190. },
  191. plugins: [
  192. new VueLoaderPlugin(),
  193. new VuetifyLoaderPlugin(),
  194. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  195. new CopyWebpackPlugin([
  196. { from: 'client/static' },
  197. { from: './node_modules/prismjs/components', to: 'js/prism' },
  198. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  199. ], {}),
  200. new MiniCssExtractPlugin({
  201. filename: 'css/bundle.[hash].css',
  202. chunkFilename: 'css/[name].[chunkhash].css'
  203. }),
  204. new HtmlWebpackPlugin({
  205. template: 'dev/templates/master.pug',
  206. filename: '../server/views/master.pug',
  207. hash: false,
  208. inject: false,
  209. excludeChunks: ['setup', 'legacy']
  210. }),
  211. new HtmlWebpackPlugin({
  212. template: 'dev/templates/legacy.pug',
  213. filename: '../server/views/legacy/master.pug',
  214. hash: false,
  215. inject: false,
  216. excludeChunks: ['setup', 'app']
  217. }),
  218. new HtmlWebpackPlugin({
  219. template: 'dev/templates/setup.pug',
  220. filename: '../server/views/setup.pug',
  221. hash: false,
  222. inject: false,
  223. excludeChunks: ['app', 'legacy']
  224. }),
  225. new HtmlWebpackPugPlugin(),
  226. new ScriptExtHtmlWebpackPlugin({
  227. sync: 'runtime.js',
  228. defaultAttribute: 'async'
  229. }),
  230. new SriWebpackPlugin({
  231. hashFuncNames: ['sha256', 'sha512'],
  232. enabled: true
  233. }),
  234. new WebpackBarPlugin({
  235. name: 'Client Assets'
  236. }),
  237. new CleanWebpackPlugin(),
  238. new OptimizeCssAssetsPlugin({
  239. cssProcessorOptions: { discardComments: { removeAll: true } },
  240. canPrint: true
  241. }),
  242. new webpack.DefinePlugin({
  243. 'process.env.NODE_ENV': JSON.stringify('production'),
  244. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  245. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  246. })
  247. ],
  248. optimization: {
  249. namedModules: true,
  250. namedChunks: true,
  251. splitChunks: {
  252. name: 'vendor',
  253. minChunks: 2
  254. },
  255. runtimeChunk: 'single'
  256. },
  257. resolve: {
  258. mainFields: ['browser', 'main', 'module'],
  259. symlinks: true,
  260. alias: {
  261. '@': path.join(process.cwd(), 'client'),
  262. 'vue$': 'vue/dist/vue.esm.js',
  263. 'gql': path.join(process.cwd(), 'client/graph'),
  264. // Duplicates fixes:
  265. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  266. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  267. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  268. },
  269. extensions: [
  270. '.js',
  271. '.json',
  272. 'jsx',
  273. '.vue'
  274. ],
  275. modules: [
  276. 'node_modules'
  277. ]
  278. },
  279. node: {
  280. fs: 'empty'
  281. },
  282. stats: {
  283. children: false,
  284. entrypoints: false
  285. },
  286. target: 'web'
  287. }