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.

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