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.1 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. 'assets/js/*.*',
  225. 'assets/css/*.*',
  226. 'assets/*.js',
  227. 'assets/*.json'
  228. ], {
  229. root: process.cwd(),
  230. verbose: false
  231. }),
  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. }