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.

276 lines
7.0 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 OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  13. const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
  14. const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin')
  15. const WebpackBarPlugin = require('webpackbar')
  16. // const SriWebpackPlugin = require('webpack-subresource-integrity')
  17. const babelConfig = fs.readJsonSync(path.join(process.cwd(), '.babelrc'))
  18. const cacheDir = '.webpack-cache/cache'
  19. const babelDir = path.join(process.cwd(), '.webpack-cache/babel')
  20. process.noDeprecation = true
  21. fs.emptyDirSync(path.join(process.cwd(), 'assets'))
  22. module.exports = {
  23. mode: 'production',
  24. entry: {
  25. app: './client/index-app.js',
  26. legacy: './client/index-legacy.js',
  27. setup: './client/index-setup.js'
  28. },
  29. output: {
  30. path: path.join(process.cwd(), 'assets'),
  31. publicPath: '/',
  32. filename: 'js/[name].[hash].js',
  33. chunkFilename: 'js/[name].[chunkhash].js',
  34. globalObject: 'this',
  35. crossOriginLoading: 'use-credentials'
  36. },
  37. module: {
  38. rules: [
  39. {
  40. test: /\.js$/,
  41. exclude: (modulePath) => {
  42. return modulePath.includes('node_modules') && !modulePath.includes('vuetify')
  43. },
  44. use: [
  45. {
  46. loader: 'cache-loader',
  47. options: {
  48. cacheDirectory: cacheDir
  49. }
  50. },
  51. {
  52. loader: 'babel-loader',
  53. options: {
  54. ...babelConfig,
  55. cacheDirectory: babelDir
  56. }
  57. }
  58. ]
  59. },
  60. {
  61. test: /\.css$/,
  62. use: [
  63. 'style-loader',
  64. MiniCssExtractPlugin.loader,
  65. 'css-loader',
  66. 'postcss-loader'
  67. ]
  68. },
  69. {
  70. test: /\.sass$/,
  71. use: [
  72. {
  73. loader: 'cache-loader',
  74. options: {
  75. cacheDirectory: cacheDir
  76. }
  77. },
  78. 'style-loader',
  79. 'css-loader',
  80. 'postcss-loader',
  81. {
  82. loader: 'sass-loader',
  83. options: {
  84. implementation: require('sass'),
  85. sourceMap: false
  86. }
  87. }
  88. ]
  89. },
  90. {
  91. test: /\.scss$/,
  92. use: [
  93. {
  94. loader: 'cache-loader',
  95. options: {
  96. cacheDirectory: cacheDir
  97. }
  98. },
  99. 'style-loader',
  100. MiniCssExtractPlugin.loader,
  101. 'css-loader',
  102. 'postcss-loader',
  103. {
  104. loader: 'sass-loader',
  105. options: {
  106. implementation: require('sass'),
  107. sourceMap: false
  108. }
  109. },
  110. {
  111. loader: 'sass-resources-loader',
  112. options: {
  113. resources: path.join(process.cwd(), '/client/scss/global.scss')
  114. }
  115. }
  116. ]
  117. },
  118. {
  119. test: /\.vue$/,
  120. loader: 'vue-loader'
  121. },
  122. {
  123. test: /\.pug$/,
  124. exclude: [
  125. path.join(process.cwd(), 'dev')
  126. ],
  127. loader: 'pug-plain-loader'
  128. },
  129. {
  130. test: /\.(png|jpg|gif)$/,
  131. use: [
  132. {
  133. loader: 'url-loader',
  134. options: {
  135. limit: 8192
  136. }
  137. }
  138. ]
  139. },
  140. {
  141. test: /\.svg$/,
  142. exclude: [
  143. path.join(process.cwd(), 'node_modules/grapesjs')
  144. ],
  145. use: [
  146. {
  147. loader: 'file-loader',
  148. options: {
  149. name: '[name].[ext]',
  150. outputPath: 'svg/'
  151. }
  152. }
  153. ]
  154. },
  155. {
  156. test: /\.(graphql|gql)$/,
  157. exclude: /node_modules/,
  158. use: [
  159. { loader: 'graphql-persisted-document-loader' },
  160. { loader: 'graphql-tag/loader' }
  161. ]
  162. },
  163. {
  164. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  165. exclude: [
  166. path.join(process.cwd(), 'client')
  167. ],
  168. use: [{
  169. loader: 'file-loader',
  170. options: {
  171. name: '[name].[ext]',
  172. outputPath: 'fonts/'
  173. }
  174. }]
  175. }
  176. ]
  177. },
  178. plugins: [
  179. new VueLoaderPlugin(),
  180. new VuetifyLoaderPlugin(),
  181. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  182. new CopyWebpackPlugin([
  183. { from: 'client/static' },
  184. { from: './node_modules/prismjs/components', to: 'js/prism' }
  185. ], {}),
  186. new MiniCssExtractPlugin({
  187. filename: 'css/bundle.[hash].css',
  188. chunkFilename: 'css/[name].[chunkhash].css'
  189. }),
  190. new HtmlWebpackPlugin({
  191. template: 'dev/templates/master.pug',
  192. filename: '../server/views/master.pug',
  193. hash: false,
  194. inject: false,
  195. excludeChunks: ['setup', 'legacy']
  196. }),
  197. new HtmlWebpackPlugin({
  198. template: 'dev/templates/legacy.pug',
  199. filename: '../server/views/legacy/master.pug',
  200. hash: false,
  201. inject: false,
  202. excludeChunks: ['setup', 'app']
  203. }),
  204. new HtmlWebpackPlugin({
  205. template: 'dev/templates/setup.pug',
  206. filename: '../server/views/setup.pug',
  207. hash: false,
  208. inject: false,
  209. excludeChunks: ['app', 'legacy']
  210. }),
  211. new HtmlWebpackPugPlugin(),
  212. new ScriptExtHtmlWebpackPlugin({
  213. sync: 'runtime.js',
  214. defaultAttribute: 'async'
  215. }),
  216. // new SriWebpackPlugin({
  217. // hashFuncNames: ['sha256', 'sha512'],
  218. // enabled: true
  219. // }),
  220. new WebpackBarPlugin({
  221. name: 'Client Assets'
  222. }),
  223. new CleanWebpackPlugin(),
  224. new OptimizeCssAssetsPlugin({
  225. cssProcessorOptions: { discardComments: { removeAll: true } },
  226. canPrint: true
  227. }),
  228. new webpack.DefinePlugin({
  229. 'process.env.NODE_ENV': JSON.stringify('production'),
  230. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  231. })
  232. ],
  233. optimization: {
  234. namedModules: true,
  235. namedChunks: true,
  236. splitChunks: {
  237. name: 'vendor',
  238. minChunks: 2
  239. },
  240. runtimeChunk: 'single'
  241. },
  242. resolve: {
  243. mainFields: ['browser', 'main', 'module'],
  244. symlinks: true,
  245. alias: {
  246. '@': path.join(process.cwd(), 'client'),
  247. 'vue$': 'vue/dist/vue.esm.js',
  248. 'gql': path.join(process.cwd(), 'client/graph'),
  249. // Duplicates fixes:
  250. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  251. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  252. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  253. },
  254. extensions: [
  255. '.js',
  256. '.json',
  257. '.vue'
  258. ],
  259. modules: [
  260. 'node_modules'
  261. ]
  262. },
  263. node: {
  264. fs: 'empty'
  265. },
  266. stats: {
  267. children: false,
  268. entrypoints: false
  269. },
  270. target: 'web'
  271. }