webpack.prod.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 { 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 SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin')
  15. const SriWebpackPlugin = require('webpack-subresource-integrity')
  16. const { VueLoaderPlugin } = require('vue-loader')
  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: /node_modules/,
  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. MiniCssExtractPlugin.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. fiber: Fiber,
  84. sourceMap: false
  85. }
  86. }
  87. ]
  88. },
  89. {
  90. test: /\.scss$/,
  91. use: [
  92. {
  93. loader: 'cache-loader',
  94. options: {
  95. cacheDirectory: cacheDir
  96. }
  97. },
  98. 'style-loader',
  99. MiniCssExtractPlugin.loader,
  100. 'css-loader',
  101. 'postcss-loader',
  102. {
  103. loader: 'sass-loader',
  104. options: {
  105. implementation: require('sass'),
  106. fiber: Fiber,
  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(), 'client/svg'),
  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: /\.svg$/,
  158. include: [
  159. path.join(process.cwd(), 'client/svg')
  160. ],
  161. use: [
  162. {
  163. loader: 'raw-loader'
  164. }
  165. ]
  166. },
  167. {
  168. test: /\.(graphql|gql)$/,
  169. exclude: /node_modules/,
  170. use: [
  171. { loader: 'graphql-persisted-document-loader' },
  172. { loader: 'graphql-tag/loader' }
  173. ]
  174. },
  175. {
  176. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  177. exclude: [
  178. path.join(process.cwd(), 'client')
  179. ],
  180. use: [{
  181. loader: 'file-loader',
  182. options: {
  183. name: '[name].[ext]',
  184. outputPath: 'fonts/'
  185. }
  186. }]
  187. },
  188. {
  189. test: /.jsx$/,
  190. loader: 'babel-loader',
  191. exclude: /node_modules/,
  192. options: {
  193. presets: ['es2015', 'react']
  194. }
  195. },
  196. {
  197. test: /\.flow$/,
  198. loader: 'ignore-loader'
  199. }
  200. ]
  201. },
  202. plugins: [
  203. new VueLoaderPlugin(),
  204. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  205. new CopyWebpackPlugin([
  206. { from: 'client/static' },
  207. { from: './node_modules/prismjs/components', to: 'js/prism' },
  208. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  209. ], {}),
  210. new MiniCssExtractPlugin({
  211. filename: 'css/bundle.[hash].css',
  212. chunkFilename: 'css/[name].[chunkhash].css'
  213. }),
  214. new HtmlWebpackPlugin({
  215. template: 'dev/templates/master.pug',
  216. filename: '../server/views/master.pug',
  217. hash: false,
  218. inject: false,
  219. excludeChunks: ['setup', 'legacy']
  220. }),
  221. new HtmlWebpackPlugin({
  222. template: 'dev/templates/legacy.pug',
  223. filename: '../server/views/legacy/master.pug',
  224. hash: false,
  225. inject: false,
  226. excludeChunks: ['setup', 'app']
  227. }),
  228. new HtmlWebpackPlugin({
  229. template: 'dev/templates/setup.pug',
  230. filename: '../server/views/setup.pug',
  231. hash: false,
  232. inject: false,
  233. excludeChunks: ['app', 'legacy']
  234. }),
  235. new HtmlWebpackPugPlugin(),
  236. new ScriptExtHtmlWebpackPlugin({
  237. sync: 'runtime.js',
  238. defaultAttribute: 'async'
  239. }),
  240. new SriWebpackPlugin({
  241. hashFuncNames: ['sha256', 'sha512'],
  242. enabled: true
  243. }),
  244. new SimpleProgressWebpackPlugin({
  245. format: 'expanded'
  246. }),
  247. new CleanWebpackPlugin(),
  248. new OptimizeCssAssetsPlugin({
  249. cssProcessorOptions: { discardComments: { removeAll: true } },
  250. canPrint: true
  251. }),
  252. new webpack.DefinePlugin({
  253. 'process.env.NODE_ENV': JSON.stringify('production'),
  254. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  255. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  256. })
  257. ],
  258. optimization: {
  259. namedModules: true,
  260. namedChunks: true,
  261. splitChunks: {
  262. name: 'vendor',
  263. minChunks: 2
  264. },
  265. runtimeChunk: 'single'
  266. },
  267. resolve: {
  268. mainFields: ['browser', 'main', 'module'],
  269. symlinks: true,
  270. alias: {
  271. '@': path.join(process.cwd(), 'client'),
  272. 'vue$': 'vue/dist/vue.esm.js',
  273. 'gql': path.join(process.cwd(), 'client/graph'),
  274. // Duplicates fixes:
  275. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  276. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  277. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  278. },
  279. extensions: [
  280. '.js',
  281. '.json',
  282. 'jsx',
  283. '.vue'
  284. ],
  285. modules: [
  286. 'node_modules'
  287. ]
  288. },
  289. node: {
  290. fs: 'empty'
  291. },
  292. stats: {
  293. children: false,
  294. entrypoints: false
  295. },
  296. target: 'web'
  297. }