webpack.prod.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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'))
  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: '/_assets/',
  33. filename: `js/[name].js?${now}`,
  34. chunkFilename: `js/[name].js?${now}`,
  35. globalObject: 'this',
  36. crossOriginLoading: 'use-credentials'
  37. },
  38. module: {
  39. rules: [
  40. {
  41. test: /\.js$/,
  42. exclude: (modulePath) => {
  43. return modulePath.includes('node_modules') && !modulePath.includes('vuetify')
  44. },
  45. use: [
  46. {
  47. loader: 'cache-loader',
  48. options: {
  49. cacheDirectory: cacheDir
  50. }
  51. },
  52. {
  53. loader: 'babel-loader',
  54. options: {
  55. ...babelConfig,
  56. cacheDirectory: babelDir
  57. }
  58. }
  59. ]
  60. },
  61. {
  62. test: /\.css$/,
  63. use: [
  64. 'style-loader',
  65. MiniCssExtractPlugin.loader,
  66. 'css-loader',
  67. 'postcss-loader'
  68. ]
  69. },
  70. {
  71. test: /\.sass$/,
  72. use: [
  73. {
  74. loader: 'cache-loader',
  75. options: {
  76. cacheDirectory: cacheDir
  77. }
  78. },
  79. 'style-loader',
  80. 'css-loader',
  81. 'postcss-loader',
  82. {
  83. loader: 'sass-loader',
  84. options: {
  85. implementation: require('sass'),
  86. sourceMap: false
  87. }
  88. }
  89. ]
  90. },
  91. {
  92. test: /\.scss$/,
  93. use: [
  94. {
  95. loader: 'cache-loader',
  96. options: {
  97. cacheDirectory: cacheDir
  98. }
  99. },
  100. 'style-loader',
  101. MiniCssExtractPlugin.loader,
  102. 'css-loader',
  103. 'postcss-loader',
  104. {
  105. loader: 'sass-loader',
  106. options: {
  107. implementation: require('sass'),
  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. loader: 'webpack-modernizr-loader',
  179. test: /\.modernizrrc\.js$/
  180. }
  181. ]
  182. },
  183. plugins: [
  184. new VueLoaderPlugin(),
  185. new VuetifyLoaderPlugin(),
  186. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  187. new MomentTimezoneDataPlugin({
  188. startYear: 2017,
  189. endYear: (new Date().getFullYear()) + 5
  190. }),
  191. new CopyWebpackPlugin({
  192. patterns: [
  193. { from: 'client/static' },
  194. { from: './node_modules/prismjs/components', to: 'js/prism' }
  195. ]
  196. }),
  197. new MiniCssExtractPlugin({
  198. filename: 'css/bundle.[hash].css',
  199. chunkFilename: 'css/[name].[chunkhash].css'
  200. }),
  201. new HtmlWebpackPlugin({
  202. template: 'dev/templates/master.pug',
  203. filename: '../server/views/master.pug',
  204. hash: false,
  205. inject: false,
  206. excludeChunks: ['setup', 'legacy']
  207. }),
  208. new HtmlWebpackPlugin({
  209. template: 'dev/templates/legacy.pug',
  210. filename: '../server/views/legacy/master.pug',
  211. hash: false,
  212. inject: false,
  213. excludeChunks: ['setup', 'app']
  214. }),
  215. new HtmlWebpackPlugin({
  216. template: 'dev/templates/setup.pug',
  217. filename: '../server/views/setup.pug',
  218. hash: false,
  219. inject: false,
  220. excludeChunks: ['app', 'legacy']
  221. }),
  222. new HtmlWebpackPugPlugin(),
  223. new ScriptExtHtmlWebpackPlugin({
  224. sync: 'runtime.js',
  225. defaultAttribute: 'async'
  226. }),
  227. new WebpackBarPlugin({
  228. name: 'Client Assets'
  229. }),
  230. new CleanWebpackPlugin(),
  231. new OptimizeCssAssetsPlugin({
  232. cssProcessorOptions: { discardComments: { removeAll: true } },
  233. canPrint: true
  234. }),
  235. new webpack.DefinePlugin({
  236. 'process.env.NODE_ENV': JSON.stringify('production'),
  237. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  238. }),
  239. new webpack.optimize.MinChunkSizePlugin({
  240. minChunkSize: 50000
  241. })
  242. ],
  243. optimization: {
  244. namedModules: true,
  245. namedChunks: true,
  246. splitChunks: {
  247. name: 'vendor',
  248. minChunks: 2
  249. },
  250. runtimeChunk: 'single'
  251. },
  252. resolve: {
  253. mainFields: ['browser', 'main', 'module'],
  254. symlinks: true,
  255. alias: {
  256. '@': path.join(process.cwd(), 'client'),
  257. 'vue$': 'vue/dist/vue.esm.js',
  258. 'gql': path.join(process.cwd(), 'client/graph'),
  259. // Duplicates fixes:
  260. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  261. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  262. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro'),
  263. 'modernizr$': path.resolve(process.cwd(), 'client/.modernizrrc.js')
  264. },
  265. extensions: [
  266. '.js',
  267. '.json',
  268. '.vue'
  269. ],
  270. modules: [
  271. 'node_modules'
  272. ]
  273. },
  274. node: {
  275. fs: 'empty'
  276. },
  277. stats: {
  278. children: false,
  279. entrypoints: false
  280. },
  281. target: 'web'
  282. }