webpack.prod.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(), '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 webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  193. new CopyWebpackPlugin([
  194. { from: 'client/static' },
  195. { from: './node_modules/prismjs/components', to: 'js/prism' },
  196. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  197. ], {}),
  198. new MiniCssExtractPlugin({
  199. filename: 'css/bundle.[hash].css',
  200. chunkFilename: 'css/[name].[chunkhash].css'
  201. }),
  202. new HtmlWebpackPlugin({
  203. template: 'dev/templates/master.pug',
  204. filename: '../server/views/master.pug',
  205. hash: false,
  206. inject: false,
  207. excludeChunks: ['setup', 'legacy']
  208. }),
  209. new HtmlWebpackPlugin({
  210. template: 'dev/templates/legacy.pug',
  211. filename: '../server/views/legacy/master.pug',
  212. hash: false,
  213. inject: false,
  214. excludeChunks: ['setup', 'app']
  215. }),
  216. new HtmlWebpackPlugin({
  217. template: 'dev/templates/setup.pug',
  218. filename: '../server/views/setup.pug',
  219. hash: false,
  220. inject: false,
  221. excludeChunks: ['app', 'legacy']
  222. }),
  223. new HtmlWebpackPugPlugin(),
  224. new ScriptExtHtmlWebpackPlugin({
  225. sync: 'runtime.js',
  226. defaultAttribute: 'async'
  227. }),
  228. new SriWebpackPlugin({
  229. hashFuncNames: ['sha256', 'sha512'],
  230. enabled: true
  231. }),
  232. new SimpleProgressWebpackPlugin({
  233. format: 'expanded'
  234. }),
  235. new CleanWebpackPlugin(),
  236. new OptimizeCssAssetsPlugin({
  237. cssProcessorOptions: { discardComments: { removeAll: true } },
  238. canPrint: true
  239. }),
  240. new webpack.DefinePlugin({
  241. 'process.env.NODE_ENV': JSON.stringify('production'),
  242. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  243. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  244. })
  245. ],
  246. optimization: {
  247. namedModules: true,
  248. namedChunks: true,
  249. splitChunks: {
  250. name: 'vendor',
  251. minChunks: 2
  252. },
  253. runtimeChunk: 'single'
  254. },
  255. resolve: {
  256. mainFields: ['browser', 'main', 'module'],
  257. symlinks: true,
  258. alias: {
  259. '@': path.join(process.cwd(), 'client'),
  260. 'vue$': 'vue/dist/vue.esm.js',
  261. 'gql': path.join(process.cwd(), 'client/graph'),
  262. // Duplicates fixes:
  263. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  264. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  265. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  266. },
  267. extensions: [
  268. '.js',
  269. '.json',
  270. 'jsx',
  271. '.vue'
  272. ],
  273. modules: [
  274. 'node_modules'
  275. ]
  276. },
  277. node: {
  278. fs: 'empty'
  279. },
  280. stats: {
  281. children: false,
  282. entrypoints: false
  283. },
  284. target: 'web'
  285. }