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.

287 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 { 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 SriWebpackPlugin = require('webpack-subresource-integrity')
  15. const { VueLoaderPlugin } = require('vue-loader')
  16. const babelConfig = fs.readJsonSync(path.join(process.cwd(), '.babelrc'))
  17. const cacheDir = '.webpack-cache/cache'
  18. const babelDir = path.join(process.cwd(), '.webpack-cache/babel')
  19. process.noDeprecation = true
  20. fs.emptyDirSync(path.join(process.cwd(), 'assets'))
  21. module.exports = {
  22. mode: 'production',
  23. entry: {
  24. app: './client/index-app.js',
  25. legacy: './client/index-legacy.js',
  26. setup: './client/index-setup.js'
  27. },
  28. output: {
  29. path: path.join(process.cwd(), 'assets'),
  30. publicPath: '/',
  31. filename: 'js/[name].[hash].js',
  32. chunkFilename: 'js/[name].[chunkhash].js',
  33. globalObject: 'this',
  34. crossOriginLoading: 'use-credentials'
  35. },
  36. module: {
  37. rules: [
  38. {
  39. test: /\.js$/,
  40. exclude: /node_modules/,
  41. use: [
  42. {
  43. loader: 'cache-loader',
  44. options: {
  45. cacheDirectory: cacheDir
  46. }
  47. },
  48. {
  49. loader: 'babel-loader',
  50. options: {
  51. ...babelConfig,
  52. cacheDirectory: babelDir
  53. }
  54. }
  55. ]
  56. },
  57. {
  58. test: /\.css$/,
  59. use: [
  60. 'style-loader',
  61. MiniCssExtractPlugin.loader,
  62. 'css-loader',
  63. 'postcss-loader'
  64. ]
  65. },
  66. {
  67. test: /\.scss$/,
  68. use: [
  69. {
  70. loader: 'cache-loader',
  71. options: {
  72. cacheDirectory: cacheDir
  73. }
  74. },
  75. 'style-loader',
  76. MiniCssExtractPlugin.loader,
  77. 'css-loader',
  78. 'postcss-loader',
  79. {
  80. loader: 'sass-loader',
  81. options: {
  82. sourceMap: false
  83. }
  84. },
  85. {
  86. loader: 'sass-resources-loader',
  87. options: {
  88. resources: path.join(process.cwd(), '/client/scss/global.scss')
  89. }
  90. }
  91. ]
  92. },
  93. {
  94. test: /\.styl$/,
  95. use: [
  96. 'style-loader',
  97. MiniCssExtractPlugin.loader,
  98. 'css-loader',
  99. 'postcss-loader',
  100. 'stylus-loader'
  101. ]
  102. },
  103. {
  104. test: /\.vue$/,
  105. loader: 'vue-loader'
  106. },
  107. {
  108. test: /\.pug$/,
  109. exclude: [
  110. path.join(process.cwd(), 'dev')
  111. ],
  112. loader: 'pug-plain-loader'
  113. },
  114. {
  115. test: /\.(png|jpg|gif)$/,
  116. use: [
  117. {
  118. loader: 'url-loader',
  119. options: {
  120. limit: 8192
  121. }
  122. }
  123. ]
  124. },
  125. {
  126. test: /\.svg$/,
  127. exclude: [
  128. path.join(process.cwd(), 'client/svg'),
  129. path.join(process.cwd(), 'node_modules/grapesjs')
  130. ],
  131. use: [
  132. {
  133. loader: 'file-loader',
  134. options: {
  135. name: '[name].[ext]',
  136. outputPath: 'svg/'
  137. }
  138. }
  139. ]
  140. },
  141. {
  142. test: /\.svg$/,
  143. include: [
  144. path.join(process.cwd(), 'client/svg')
  145. ],
  146. use: [
  147. {
  148. loader: 'raw-loader'
  149. }
  150. ]
  151. },
  152. {
  153. test: /\.(graphql|gql)$/,
  154. exclude: /node_modules/,
  155. use: [
  156. { loader: 'graphql-persisted-document-loader' },
  157. { loader: 'graphql-tag/loader' }
  158. ]
  159. },
  160. {
  161. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  162. exclude: [
  163. path.join(process.cwd(), 'client')
  164. ],
  165. use: [{
  166. loader: 'file-loader',
  167. options: {
  168. name: '[name].[ext]',
  169. outputPath: 'fonts/'
  170. }
  171. }]
  172. },
  173. {
  174. test: /.jsx$/,
  175. loader: 'babel-loader',
  176. exclude: /node_modules/,
  177. options: {
  178. presets: ['es2015', 'react']
  179. }
  180. },
  181. {
  182. test: /\.flow$/,
  183. loader: 'ignore-loader'
  184. }
  185. ]
  186. },
  187. plugins: [
  188. new VueLoaderPlugin(),
  189. new webpack.BannerPlugin('Wiki.js - wiki.js.org - Licensed under AGPL'),
  190. new CopyWebpackPlugin([
  191. { from: 'client/static' },
  192. { from: './node_modules/graphql-voyager/dist/voyager.worker.js', to: 'js/' }
  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.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 SriWebpackPlugin({
  225. hashFuncNames: ['sha256', 'sha512'],
  226. enabled: true
  227. }),
  228. new SimpleProgressWebpackPlugin({
  229. format: 'expanded'
  230. }),
  231. new CleanWebpackPlugin(),
  232. new OptimizeCssAssetsPlugin({
  233. cssProcessorOptions: { discardComments: { removeAll: true } },
  234. canPrint: true
  235. }),
  236. new webpack.DefinePlugin({
  237. 'process.env.NODE_ENV': JSON.stringify('production'),
  238. 'process.env.CURRENT_THEME': JSON.stringify(_.defaultTo(yargs.theme, 'default')),
  239. '__REACT_DEVTOOLS_GLOBAL_HOOK__': '({ isDisabled: true })'
  240. })
  241. ],
  242. optimization: {
  243. namedModules: true,
  244. namedChunks: true,
  245. splitChunks: {
  246. name: 'vendor',
  247. minChunks: 2
  248. },
  249. runtimeChunk: 'single'
  250. },
  251. resolve: {
  252. mainFields: ['browser', 'main', 'module'],
  253. symlinks: true,
  254. alias: {
  255. '@': path.join(process.cwd(), 'client'),
  256. 'vue$': 'vue/dist/vue.esm.js',
  257. 'gql': path.join(process.cwd(), 'client/graph'),
  258. 'mdi': path.resolve(process.cwd(), 'node_modules/vue-material-design-icons'),
  259. // Duplicates fixes:
  260. 'apollo-link': path.join(process.cwd(), 'node_modules/apollo-link'),
  261. 'apollo-utilities': path.join(process.cwd(), 'node_modules/apollo-utilities'),
  262. 'uc.micro': path.join(process.cwd(), 'node_modules/uc.micro')
  263. },
  264. extensions: [
  265. '.js',
  266. '.json',
  267. 'jsx',
  268. '.vue'
  269. ],
  270. modules: [
  271. 'node_modules'
  272. ]
  273. },
  274. node: {
  275. fs: 'empty'
  276. },
  277. stats: {
  278. children: false,
  279. entrypoints: false
  280. },
  281. target: 'web'
  282. }