webpack.prod.js 6.6 KB

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