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.

webpack.config.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. const cssnano = require('cssnano');
  2. const fastGlob = require('fast-glob');
  3. const CopyPlugin = require('copy-webpack-plugin');
  4. const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  7. const PostCSSPresetEnv = require('postcss-preset-env');
  8. const PostCSSSafeParser = require('postcss-safe-parser');
  9. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  10. const TerserPlugin = require('terser-webpack-plugin');
  11. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  12. const { statSync } = require('fs');
  13. const { resolve, parse } = require('path');
  14. const { SourceMapDevToolPlugin } = require('webpack');
  15. const glob = (pattern) => fastGlob.sync(pattern, { cwd: __dirname, absolute: true });
  16. const themes = {};
  17. for (const path of glob('web_src/less/themes/*.less')) {
  18. themes[parse(path).name] = [path];
  19. }
  20. const isProduction = process.env.NODE_ENV !== 'development';
  21. module.exports = {
  22. mode: isProduction ? 'production' : 'development',
  23. entry: {
  24. index: [
  25. resolve(__dirname, 'web_src/js/index.js'),
  26. resolve(__dirname, 'web_src/less/index.less'),
  27. ],
  28. swagger: [
  29. resolve(__dirname, 'web_src/js/standalone/swagger.js'),
  30. ],
  31. jquery: [
  32. resolve(__dirname, 'web_src/js/jquery.js'),
  33. ],
  34. icons: glob('node_modules/@primer/octicons/build/svg/**/*.svg'),
  35. ...themes,
  36. },
  37. devtool: false,
  38. output: {
  39. path: resolve(__dirname, 'public'),
  40. filename: 'js/[name].js',
  41. chunkFilename: 'js/[name].js',
  42. },
  43. optimization: {
  44. minimize: isProduction,
  45. minimizer: [
  46. new TerserPlugin({
  47. sourceMap: true,
  48. extractComments: false,
  49. terserOptions: {
  50. output: {
  51. comments: false,
  52. },
  53. },
  54. }),
  55. new OptimizeCSSAssetsPlugin({
  56. cssProcessor: cssnano,
  57. cssProcessorOptions: {
  58. parser: PostCSSSafeParser,
  59. },
  60. cssProcessorPluginOptions: {
  61. preset: [
  62. 'default',
  63. {
  64. discardComments: {
  65. removeAll: true,
  66. },
  67. },
  68. ],
  69. },
  70. }),
  71. ],
  72. splitChunks: {
  73. chunks: 'async',
  74. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  75. }
  76. },
  77. module: {
  78. rules: [
  79. {
  80. test: /\.vue$/,
  81. exclude: /node_modules/,
  82. loader: 'vue-loader',
  83. },
  84. {
  85. test: /\.js$/,
  86. exclude: /node_modules/,
  87. use: [
  88. {
  89. loader: 'babel-loader',
  90. options: {
  91. cacheDirectory: true,
  92. cacheCompression: false,
  93. cacheIdentifier: [
  94. resolve(__dirname, 'package.json'),
  95. resolve(__dirname, 'package-lock.json'),
  96. resolve(__dirname, 'webpack.config.js'),
  97. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  98. sourceMaps: true,
  99. presets: [
  100. [
  101. '@babel/preset-env',
  102. {
  103. useBuiltIns: 'usage',
  104. corejs: 3,
  105. },
  106. ],
  107. ],
  108. plugins: [
  109. [
  110. '@babel/plugin-transform-runtime',
  111. {
  112. regenerator: true,
  113. }
  114. ],
  115. '@babel/plugin-proposal-object-rest-spread',
  116. ],
  117. },
  118. },
  119. ],
  120. },
  121. {
  122. test: /\.(less|css)$/i,
  123. use: [
  124. {
  125. loader: MiniCssExtractPlugin.loader,
  126. },
  127. {
  128. loader: 'css-loader',
  129. options: {
  130. importLoaders: 2,
  131. url: false,
  132. }
  133. },
  134. {
  135. loader: 'postcss-loader',
  136. options: {
  137. plugins: () => [
  138. PostCSSPresetEnv(),
  139. ],
  140. },
  141. },
  142. {
  143. loader: 'less-loader',
  144. },
  145. ],
  146. },
  147. {
  148. test: /\.svg$/,
  149. use: [
  150. {
  151. loader: 'svg-sprite-loader',
  152. options: {
  153. extract: true,
  154. spriteFilename: 'img/svg/icons.svg',
  155. symbolId: (path) => {
  156. const { name } = parse(path);
  157. if (/@primer[/\\]octicons/.test(path)) {
  158. return `octicon-${name}`;
  159. }
  160. return name;
  161. },
  162. },
  163. },
  164. {
  165. loader: 'svgo-loader',
  166. },
  167. ],
  168. },
  169. ],
  170. },
  171. plugins: [
  172. new VueLoaderPlugin(),
  173. // avoid generating useless js output files for css- and svg-only chunks
  174. new FixStyleOnlyEntriesPlugin({
  175. extensions: ['less', 'scss', 'css', 'svg'],
  176. silent: true,
  177. }),
  178. new MiniCssExtractPlugin({
  179. filename: 'css/[name].css',
  180. chunkFilename: 'css/[name].css',
  181. }),
  182. new SourceMapDevToolPlugin({
  183. filename: 'js/[name].js.map',
  184. include: [
  185. 'js/index.js',
  186. ],
  187. }),
  188. new SpriteLoaderPlugin({
  189. plainSprite: true,
  190. }),
  191. new CopyPlugin([
  192. // workaround for https://github.com/go-gitea/gitea/issues/10653
  193. { from: 'node_modules/fomantic-ui/dist/semantic.min.css', to: 'fomantic/semantic.min.css' },
  194. ]),
  195. ],
  196. performance: {
  197. hints: isProduction ? 'warning' : false,
  198. maxEntrypointSize: 512000,
  199. maxAssetSize: 512000,
  200. assetFilter: (filename) => {
  201. if (filename.endsWith('.map')) return false;
  202. if (['js/swagger.js', 'js/highlight.js', 'fomantic/semantic.min.css'].includes(filename)) return false;
  203. return true;
  204. },
  205. },
  206. resolve: {
  207. symlinks: false,
  208. alias: {
  209. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  210. },
  211. },
  212. watchOptions: {
  213. ignored: [
  214. 'node_modules/**',
  215. ],
  216. },
  217. };