You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.3 KiB

  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. ],
  126. use: [
  127. {
  128. loader: 'file-loader',
  129. options: {
  130. name: '[name].[ext]',
  131. outputPath: 'svg/'
  132. }
  133. }
  134. ]
  135. },
  136. {
  137. test: /\.svg$/,
  138. include: [
  139. path.join(process.cwd(), 'client/svg')
  140. ],
  141. use: [
  142. {
  143. loader: 'raw-loader'
  144. }
  145. ]
  146. },
  147. {
  148. test: /\.(graphql|gql)$/,
  149. exclude: /node_modules/,
  150. use: [
  151. { loader: 'graphql-persisted-document-loader' },
  152. { loader: 'graphql-tag/loader' }
  153. ]
  154. },
  155. {
  156. test: /.jsx$/,
  157. loader: 'babel-loader',
  158. exclude: /node_modules/,
  159. options: {
  160. presets: ['es2015', 'react']
  161. }
  162. },
  163. {
  164. test: /\.flow$/,
  165. loader: 'ignore-loader'
  166. }
  167. ]
  168. },
  169. plugins: [
  170. new VueLoaderPlugin(),
  171. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  172. new CopyWebpackPlugin([
  173. { from: 'client/static' },
  174. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  175. ], {}),
  176. new MiniCssExtractPlugin({
  177. filename: 'css/bundle.[hash].css',
  178. chunkFilename: 'css/[name].[chunkhash].css'
  179. }),
  180. new HtmlWebpackPlugin({
  181. template: 'dev/templates/master.pug',
  182. filename: '../server/views/master.pug',
  183. hash: false,
  184. inject: 'head'
  185. }),
  186. new HtmlWebpackPugPlugin(),
  187. new ScriptExtHtmlWebpackPlugin({
  188. sync: 'runtime.js',
  189. defaultAttribute: 'async'
  190. }),
  191. new SimpleProgressWebpackPlugin({
  192. format: 'expanded'
  193. }),
  194. new CleanWebpackPlugin([
  195. 'assets/js/*.*',
  196. 'assets/css/*.*',
  197. 'assets/*.js',
  198. 'assets/*.json'
  199. ], {
  200. root: process.cwd(),
  201. verbose: false
  202. }),
  203. new OptimizeCssAssetsPlugin({
  204. cssProcessorOptions: { discardComments: { removeAll: true } },
  205. canPrint: true
  206. }),
  207. new webpack.DefinePlugin({
  208. 'process.env.NODE_ENV': JSON.stringify('production'),
  209. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  210. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  211. })
  212. ],
  213. optimization: {
  214. namedModules: true,
  215. namedChunks: true,
  216. splitChunks: {
  217. name: 'vendor',
  218. minChunks: 2
  219. },
  220. runtimeChunk: 'single'
  221. },
  222. resolve: {
  223. mainFields: ['browser', 'main', 'module'],
  224. symlinks: true,
  225. alias: {
  226. '@': path.join(process.cwd(), 'client'),
  227. 'vue$': 'vue/dist/vue.esm.js',
  228. 'gql': path.join(process.cwd(), 'client/graph'),
  229. 'mdi': path.resolve(process.cwd(), 'node_modules/vue-material-design-icons'),
  230. // Duplicates fixes:
  231. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  232. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  233. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  234. },
  235. extensions: [
  236. '.js',
  237. '.json',
  238. 'jsx',
  239. '.vue'
  240. ],
  241. modules: [
  242. 'node_modules'
  243. ]
  244. },
  245. node: {
  246. fs: 'empty'
  247. },
  248. stats: {
  249. children: false,
  250. entrypoints: false
  251. },
  252. target: 'web'
  253. }