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.

285 lines
7.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 { 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: /\.(woff2|woff|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  166. use: [{
  167. loader: 'file-loader',
  168. options: {
  169. name: '[name].[ext]',
  170. outputPath: 'fonts/'
  171. }
  172. }]
  173. },
  174. {
  175. loader: 'webpack-modernizr-loader',
  176. test: /\.modernizrrc\.js$/
  177. }
  178. ]
  179. },
  180. plugins: [
  181. new VueLoaderPlugin(),
  182. new VuetifyLoaderPlugin(),
  183. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  184. new MomentTimezoneDataPlugin({
  185. startYear: 2017,
  186. endYear: (new Date().getFullYear()) + 5
  187. }),
  188. new CopyWebpackPlugin({
  189. patterns: [
  190. { from: 'client/static' },
  191. { from: './node_modules/prismjs/components', to: 'js/prism' }
  192. ]
  193. }),
  194. new MiniCssExtractPlugin({
  195. filename: 'css/bundle.[hash].css',
  196. chunkFilename: 'css/[name].[chunkhash].css'
  197. }),
  198. new HtmlWebpackPlugin({
  199. template: 'dev/templates/master.pug',
  200. filename: '../server/views/master.pug',
  201. hash: false,
  202. inject: false,
  203. excludeChunks: ['setup', 'legacy']
  204. }),
  205. new HtmlWebpackPlugin({
  206. template: 'dev/templates/legacy.pug',
  207. filename: '../server/views/legacy/master.pug',
  208. hash: false,
  209. inject: false,
  210. excludeChunks: ['setup', 'app']
  211. }),
  212. new HtmlWebpackPlugin({
  213. template: 'dev/templates/setup.pug',
  214. filename: '../server/views/setup.pug',
  215. hash: false,
  216. inject: false,
  217. excludeChunks: ['app', 'legacy']
  218. }),
  219. new HtmlWebpackPugPlugin(),
  220. new ScriptExtHtmlWebpackPlugin({
  221. sync: 'runtime.js',
  222. defaultAttribute: 'async'
  223. }),
  224. new WebpackBarPlugin({
  225. name: 'Client Assets'
  226. }),
  227. new CleanWebpackPlugin(),
  228. new OptimizeCssAssetsPlugin({
  229. cssProcessorOptions: { discardComments: { removeAll: true } },
  230. canPrint: true
  231. }),
  232. new webpack.DefinePlugin({
  233. 'process.env.NODE_ENV': JSON.stringify('production'),
  234. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default'))
  235. }),
  236. new webpack.optimize.MinChunkSizePlugin({
  237. minChunkSize: 50000
  238. })
  239. ],
  240. optimization: {
  241. namedModules: true,
  242. namedChunks: true,
  243. splitChunks: {
  244. name: 'vendor',
  245. minChunks: 2
  246. },
  247. runtimeChunk: 'single'
  248. },
  249. resolve: {
  250. mainFields: ['browser', 'main', 'module'],
  251. symlinks: true,
  252. alias: {
  253. '@': path.join(process.cwd(), 'client'),
  254. 'vue$': 'vue/dist/vue.esm.js',
  255. 'gql': path.join(process.cwd(), 'client/graph'),
  256. // Duplicates fixes:
  257. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  258. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  259. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro'),
  260. 'modernizr$': path.resolve(process.cwd(), 'client/.modernizrrc.js')
  261. },
  262. extensions: [
  263. '.js',
  264. '.json',
  265. '.vue'
  266. ],
  267. modules: [
  268. 'node_modules'
  269. ]
  270. },
  271. node: {
  272. fs: 'empty'
  273. },
  274. stats: {
  275. children: false,
  276. entrypoints: false
  277. },
  278. target: 'web'
  279. }