webpack.prod.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-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 now = Math.round(Date.now() / 1000)
  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-legacy'))
  23. module.exports = {
  24. mode: 'production',
  25. entry: {
  26. app: './client/index-app.js'
  27. },
  28. output: {
  29. path: path.join(process.cwd(), 'assets-legacy'),
  30. publicPath: '/_assets-legacy/',
  31. filename: `js/[name].js?${now}`,
  32. chunkFilename: `js/[name].js?${now}`,
  33. globalObject: 'this',
  34. crossOriginLoading: 'use-credentials'
  35. },
  36. module: {
  37. rules: [
  38. {
  39. test: /\.js$/,
  40. exclude: (modulePath) => {
  41. return modulePath.includes('node_modules') && !modulePath.includes('vuetify')
  42. },
  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. sourceMap: false,
  85. sassOptions: {
  86. fiber: false
  87. }
  88. }
  89. }
  90. ]
  91. },
  92. {
  93. test: /\.scss$/,
  94. use: [
  95. {
  96. loader: 'cache-loader',
  97. options: {
  98. cacheDirectory: cacheDir
  99. }
  100. },
  101. 'style-loader',
  102. MiniCssExtractPlugin.loader,
  103. 'css-loader',
  104. 'postcss-loader',
  105. {
  106. loader: 'sass-loader',
  107. options: {
  108. implementation: require('sass'),
  109. sourceMap: false,
  110. sassOptions: {
  111. fiber: false
  112. }
  113. }
  114. },
  115. {
  116. loader: 'sass-resources-loader',
  117. options: {
  118. resources: path.join(process.cwd(), '/client/scss/global.scss')
  119. }
  120. }
  121. ]
  122. },
  123. {
  124. test: /\.vue$/,
  125. loader: 'vue-loader'
  126. },
  127. {
  128. test: /\.pug$/,
  129. exclude: [
  130. path.join(process.cwd(), 'dev')
  131. ],
  132. loader: 'pug-plain-loader'
  133. },
  134. {
  135. test: /\.(png|jpg|gif)$/,
  136. use: [
  137. {
  138. loader: 'url-loader',
  139. options: {
  140. limit: 8192
  141. }
  142. }
  143. ]
  144. },
  145. {
  146. test: /\.svg$/,
  147. exclude: [
  148. path.join(process.cwd(), 'node_modules/grapesjs')
  149. ],
  150. use: [
  151. {
  152. loader: 'file-loader',
  153. options: {
  154. name: '[name].[ext]',
  155. outputPath: 'svg/'
  156. }
  157. }
  158. ]
  159. },
  160. {
  161. test: /\.(graphql|gql)$/,
  162. exclude: /node_modules/,
  163. use: [
  164. { loader: 'graphql-persisted-document-loader' },
  165. { loader: 'graphql-tag/loader' }
  166. ]
  167. },
  168. {
  169. test: /\.(woff2|woff|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  170. use: [{
  171. loader: 'file-loader',
  172. options: {
  173. name: '[name].[ext]',
  174. outputPath: 'fonts/'
  175. }
  176. }]
  177. },
  178. {
  179. loader: 'webpack-modernizr-loader',
  180. test: /\.modernizrrc\.js$/
  181. }
  182. ]
  183. },
  184. plugins: [
  185. new VueLoaderPlugin(),
  186. new VuetifyLoaderPlugin(),
  187. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  188. new MomentTimezoneDataPlugin({
  189. startYear: 2017,
  190. endYear: (new Date().getFullYear()) + 5
  191. }),
  192. new CopyWebpackPlugin({
  193. patterns: [
  194. { from: 'client/static' },
  195. { from: './node_modules/prismjs/components', to: 'js/prism' }
  196. ]
  197. }),
  198. new MiniCssExtractPlugin({
  199. filename: 'css/bundle.[hash].css',
  200. chunkFilename: 'css/[name].[chunkhash].css'
  201. }),
  202. new HtmlWebpackPlugin({
  203. template: 'dev/templates/base.pug',
  204. filename: '../server/views/base.pug',
  205. hash: false,
  206. inject: false
  207. }),
  208. new HtmlWebpackPugPlugin(),
  209. new ScriptExtHtmlWebpackPlugin({
  210. sync: 'runtime.js',
  211. defaultAttribute: 'async'
  212. }),
  213. new WebpackBarPlugin({
  214. name: 'Client Assets'
  215. }),
  216. new CleanWebpackPlugin(),
  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. }),
  225. new webpack.optimize.MinChunkSizePlugin({
  226. minChunkSize: 50000
  227. })
  228. ],
  229. optimization: {
  230. namedModules: true,
  231. namedChunks: true,
  232. splitChunks: {
  233. name: 'vendor',
  234. minChunks: 2
  235. },
  236. runtimeChunk: 'single'
  237. },
  238. resolve: {
  239. mainFields: ['browser', 'main', 'module'],
  240. symlinks: true,
  241. alias: {
  242. '@': path.join(process.cwd(), 'client'),
  243. 'vue$': 'vue/dist/vue.esm.js',
  244. 'gql': path.join(process.cwd(), 'client/graph'),
  245. // Duplicates fixes:
  246. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  247. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  248. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro'),
  249. 'modernizr$': path.resolve(process.cwd(), 'client/.modernizrrc.js')
  250. },
  251. extensions: [
  252. '.js',
  253. '.json',
  254. '.vue'
  255. ],
  256. modules: [
  257. 'node_modules'
  258. ]
  259. },
  260. node: {
  261. fs: 'empty'
  262. },
  263. stats: {
  264. children: false,
  265. entrypoints: false
  266. },
  267. target: 'web'
  268. }