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.

283 lines
7.2 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 { 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. },
  179. plugins: [
  180. new VueLoaderPlugin(),
  181. new VuetifyLoaderPlugin(),
  182. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  183. new MomentTimezoneDataPlugin({
  184. startYear: 2017,
  185. endYear: (new Date().getFullYear()) + 5
  186. }),
  187. new CopyWebpackPlugin({
  188. patterns: [
  189. { from: 'client/static' },
  190. { from: './node_modules/prismjs/components', to: 'js/prism' }
  191. ]
  192. }),
  193. new MiniCssExtractPlugin({
  194. filename: 'css/bundle.[hash].css',
  195. chunkFilename: 'css/[name].[chunkhash].css'
  196. }),
  197. new HtmlWebpackPlugin({
  198. template: 'dev/templates/master.pug',
  199. filename: '../server/views/master.pug',
  200. hash: false,
  201. inject: false,
  202. excludeChunks: ['setup', 'legacy']
  203. }),
  204. new HtmlWebpackPlugin({
  205. template: 'dev/templates/legacy.pug',
  206. filename: '../server/views/legacy/master.pug',
  207. hash: false,
  208. inject: false,
  209. excludeChunks: ['setup', 'app']
  210. }),
  211. new HtmlWebpackPlugin({
  212. template: 'dev/templates/setup.pug',
  213. filename: '../server/views/setup.pug',
  214. hash: false,
  215. inject: false,
  216. excludeChunks: ['app', 'legacy']
  217. }),
  218. new HtmlWebpackPugPlugin(),
  219. new ScriptExtHtmlWebpackPlugin({
  220. sync: 'runtime.js',
  221. defaultAttribute: 'async'
  222. }),
  223. new WebpackBarPlugin({
  224. name: 'Client Assets'
  225. }),
  226. new CleanWebpackPlugin(),
  227. new OptimizeCssAssetsPlugin({
  228. cssProcessorOptions: { discardComments: { removeAll: true } },
  229. canPrint: true
  230. }),
  231. new webpack.DefinePlugin({
  232. 'process.env.NODE_ENV': JSON.stringify('production'),
  233. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  234. }),
  235. new webpack.optimize.MinChunkSizePlugin({
  236. minChunkSize: 50000
  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. 'gql': path.join(process.cwd(), 'client/graph'),
  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. '.vue'
  264. ],
  265. modules: [
  266. 'node_modules'
  267. ]
  268. },
  269. node: {
  270. fs: 'empty'
  271. },
  272. stats: {
  273. children: false,
  274. entrypoints: false
  275. },
  276. target: 'web'
  277. }