webpack.prod.js 6.7 KB

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