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.

291 lines
7.4 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. sassOptions: {
  88. fiber: false
  89. }
  90. }
  91. }
  92. ]
  93. },
  94. {
  95. test: /\.scss$/,
  96. use: [
  97. {
  98. loader: 'cache-loader',
  99. options: {
  100. cacheDirectory: cacheDir
  101. }
  102. },
  103. 'style-loader',
  104. MiniCssExtractPlugin.loader,
  105. 'css-loader',
  106. 'postcss-loader',
  107. {
  108. loader: 'sass-loader',
  109. options: {
  110. implementation: require('sass'),
  111. sourceMap: false,
  112. sassOptions: {
  113. fiber: false
  114. }
  115. }
  116. },
  117. {
  118. loader: 'sass-resources-loader',
  119. options: {
  120. resources: path.join(process.cwd(), '/client/scss/global.scss')
  121. }
  122. }
  123. ]
  124. },
  125. {
  126. test: /\.vue$/,
  127. loader: 'vue-loader'
  128. },
  129. {
  130. test: /\.pug$/,
  131. exclude: [
  132. path.join(process.cwd(), 'dev')
  133. ],
  134. loader: 'pug-plain-loader'
  135. },
  136. {
  137. test: /\.(png|jpg|gif)$/,
  138. use: [
  139. {
  140. loader: 'url-loader',
  141. options: {
  142. limit: 8192
  143. }
  144. }
  145. ]
  146. },
  147. {
  148. test: /\.svg$/,
  149. exclude: [
  150. path.join(process.cwd(), 'node_modules/grapesjs')
  151. ],
  152. use: [
  153. {
  154. loader: 'file-loader',
  155. options: {
  156. name: '[name].[ext]',
  157. outputPath: 'svg/'
  158. }
  159. }
  160. ]
  161. },
  162. {
  163. test: /\.(graphql|gql)$/,
  164. exclude: /node_modules/,
  165. use: [
  166. { loader: 'graphql-persisted-document-loader' },
  167. { loader: 'graphql-tag/loader' }
  168. ]
  169. },
  170. {
  171. test: /\.(woff2|woff|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  172. use: [{
  173. loader: 'file-loader',
  174. options: {
  175. name: '[name].[ext]',
  176. outputPath: 'fonts/'
  177. }
  178. }]
  179. },
  180. {
  181. loader: 'webpack-modernizr-loader',
  182. test: /\.modernizrrc\.js$/
  183. }
  184. ]
  185. },
  186. plugins: [
  187. new VueLoaderPlugin(),
  188. new VuetifyLoaderPlugin(),
  189. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  190. new MomentTimezoneDataPlugin({
  191. startYear: 2017,
  192. endYear: (new Date().getFullYear()) + 5
  193. }),
  194. new CopyWebpackPlugin({
  195. patterns: [
  196. { from: 'client/static' },
  197. { from: './node_modules/prismjs/components', to: 'js/prism' }
  198. ]
  199. }),
  200. new MiniCssExtractPlugin({
  201. filename: 'css/bundle.[hash].css',
  202. chunkFilename: 'css/[name].[chunkhash].css'
  203. }),
  204. new HtmlWebpackPlugin({
  205. template: 'dev/templates/master.pug',
  206. filename: '../server/views/master.pug',
  207. hash: false,
  208. inject: false,
  209. excludeChunks: ['setup', 'legacy']
  210. }),
  211. new HtmlWebpackPlugin({
  212. template: 'dev/templates/legacy.pug',
  213. filename: '../server/views/legacy/master.pug',
  214. hash: false,
  215. inject: false,
  216. excludeChunks: ['setup', 'app']
  217. }),
  218. new HtmlWebpackPlugin({
  219. template: 'dev/templates/setup.pug',
  220. filename: '../server/views/setup.pug',
  221. hash: false,
  222. inject: false,
  223. excludeChunks: ['app', 'legacy']
  224. }),
  225. new HtmlWebpackPugPlugin(),
  226. new ScriptExtHtmlWebpackPlugin({
  227. sync: 'runtime.js',
  228. defaultAttribute: 'async'
  229. }),
  230. new WebpackBarPlugin({
  231. name: 'Client Assets'
  232. }),
  233. new CleanWebpackPlugin(),
  234. new OptimizeCssAssetsPlugin({
  235. cssProcessorOptions: { discardComments: { removeAll: true } },
  236. canPrint: true
  237. }),
  238. new webpack.DefinePlugin({
  239. 'process.env.NODE_ENV': JSON.stringify('production'),
  240. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  241. }),
  242. new webpack.optimize.MinChunkSizePlugin({
  243. minChunkSize: 50000
  244. })
  245. ],
  246. optimization: {
  247. namedModules: true,
  248. namedChunks: true,
  249. splitChunks: {
  250. name: 'vendor',
  251. minChunks: 2
  252. },
  253. runtimeChunk: 'single'
  254. },
  255. resolve: {
  256. mainFields: ['browser', 'main', 'module'],
  257. symlinks: true,
  258. alias: {
  259. '@': path.join(process.cwd(), 'client'),
  260. 'vue$': 'vue/dist/vue.esm.js',
  261. 'gql': path.join(process.cwd(), 'client/graph'),
  262. // Duplicates fixes:
  263. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  264. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  265. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro'),
  266. 'modernizr$': path.resolve(process.cwd(), 'client/.modernizrrc.js')
  267. },
  268. extensions: [
  269. '.js',
  270. '.json',
  271. '.vue'
  272. ],
  273. modules: [
  274. 'node_modules'
  275. ]
  276. },
  277. node: {
  278. fs: 'empty'
  279. },
  280. stats: {
  281. children: false,
  282. entrypoints: false
  283. },
  284. target: 'web'
  285. }