webpack.prod.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: /\.(graphql|gql)$/,
  170. exclude: /node_modules/,
  171. loader: 'graphql-tag/loader'
  172. },
  173. {
  174. test: /.jsx$/,
  175. loader: 'babel-loader',
  176. exclude: /node_modules/,
  177. options: {
  178. presets: ['es2015', 'react']
  179. }
  180. },
  181. {
  182. test: /\.flow$/,
  183. loader: 'ignore-loader'
  184. }
  185. ]
  186. },
  187. plugins: [
  188. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  189. new CopyWebpackPlugin([
  190. { from: 'client/static' },
  191. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  192. ], {}),
  193. new MiniCssExtractPlugin({
  194. filename: 'css/bundle.css',
  195. chunkFilename: 'css/[name].css'
  196. }),
  197. new HtmlWebpackPlugin({
  198. template: 'dev/templates/master.pug',
  199. filename: '../server/views/master.pug',
  200. hash: true,
  201. inject: 'head'
  202. }),
  203. new HtmlWebpackPugPlugin(),
  204. new ScriptExtHtmlWebpackPlugin({
  205. sync: 'runtime.js',
  206. defaultAttribute: 'async'
  207. }),
  208. new SimpleProgressWebpackPlugin({
  209. format: 'expanded'
  210. }),
  211. new CleanWebpackPlugin([
  212. 'assets/js/*.*',
  213. 'assets/css/*.*',
  214. 'assets/*.js',
  215. 'assets/*.json'
  216. ], {
  217. root: process.cwd(),
  218. verbose: false
  219. }),
  220. new OptimizeCssAssetsPlugin({
  221. cssProcessorOptions: { discardComments: { removeAll: true } },
  222. canPrint: true
  223. }),
  224. new OfflinePlugin({
  225. ServiceWorker: {
  226. minify: false
  227. },
  228. publicPath: '/',
  229. externals: ['/'],
  230. caches: {
  231. main: [
  232. 'js/client.js'
  233. ],
  234. additional: [
  235. ':externals:'
  236. ],
  237. optional: [
  238. 'js/*.chunk.js'
  239. ]
  240. },
  241. safeToUseOptionalCaches: true
  242. })
  243. ],
  244. optimization: {
  245. namedModules: true,
  246. namedChunks: true,
  247. splitChunks: {
  248. name: 'vendor',
  249. minChunks: 2
  250. },
  251. runtimeChunk: 'single'
  252. },
  253. resolve: {
  254. mainFields: ['browser', 'main', 'module'],
  255. symlinks: true,
  256. alias: {
  257. '@': path.join(process.cwd(), 'client'),
  258. 'vue$': 'vue/dist/vue.esm.js',
  259. 'gql': path.join(process.cwd(), 'client/graph'),
  260. 'mdi': path.resolve(process.cwd(), 'node_modules/vue-material-design-icons'),
  261. // Duplicates fixes:
  262. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  263. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  264. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  265. },
  266. extensions: [
  267. '.js',
  268. '.json',
  269. 'jsx',
  270. '.vue'
  271. ],
  272. modules: [
  273. 'node_modules'
  274. ]
  275. },
  276. node: {
  277. fs: 'empty'
  278. },
  279. stats: {
  280. children: false,
  281. entrypoints: false
  282. },
  283. target: 'web'
  284. }