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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: require.resolve('jquery-datetimepicker'),
  86. use: 'imports-loader?define=>false,exports=>false',
  87. },
  88. {
  89. test: /\.js$/,
  90. exclude: /node_modules/,
  91. use: [
  92. {
  93. loader: 'babel-loader',
  94. options: {
  95. cacheDirectory: true,
  96. cacheCompression: false,
  97. cacheIdentifier: [
  98. resolve(__dirname, 'package.json'),
  99. resolve(__dirname, 'package-lock.json'),
  100. resolve(__dirname, 'webpack.config.js'),
  101. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  102. sourceMaps: true,
  103. presets: [
  104. [
  105. '@babel/preset-env',
  106. {
  107. useBuiltIns: 'usage',
  108. corejs: 3,
  109. },
  110. ],
  111. ],
  112. plugins: [
  113. [
  114. '@babel/plugin-transform-runtime',
  115. {
  116. regenerator: true,
  117. }
  118. ],
  119. '@babel/plugin-proposal-object-rest-spread',
  120. ],
  121. },
  122. },
  123. ],
  124. },
  125. {
  126. test: /\.(less|css)$/i,
  127. use: [
  128. {
  129. loader: MiniCssExtractPlugin.loader,
  130. },
  131. {
  132. loader: 'css-loader',
  133. options: {
  134. importLoaders: 2,
  135. url: false,
  136. }
  137. },
  138. {
  139. loader: 'postcss-loader',
  140. options: {
  141. plugins: () => [
  142. PostCSSPresetEnv(),
  143. ],
  144. },
  145. },
  146. {
  147. loader: 'less-loader',
  148. },
  149. ],
  150. },
  151. {
  152. test: /\.svg$/,
  153. use: [
  154. {
  155. loader: 'svg-sprite-loader',
  156. options: {
  157. extract: true,
  158. spriteFilename: 'img/svg/icons.svg',
  159. symbolId: (path) => {
  160. const {name} = parse(path);
  161. if (/@primer[/\\]octicons/.test(path)) {
  162. return `octicon-${name}`;
  163. }
  164. return name;
  165. },
  166. },
  167. },
  168. {
  169. loader: 'svgo-loader',
  170. },
  171. ],
  172. },
  173. ],
  174. },
  175. plugins: [
  176. new VueLoaderPlugin(),
  177. // avoid generating useless js output files for css- and svg-only chunks
  178. new FixStyleOnlyEntriesPlugin({
  179. extensions: ['less', 'scss', 'css', 'svg'],
  180. silent: true,
  181. }),
  182. new MiniCssExtractPlugin({
  183. filename: 'css/[name].css',
  184. chunkFilename: 'css/[name].css',
  185. }),
  186. new SourceMapDevToolPlugin({
  187. filename: 'js/[name].js.map',
  188. include: [
  189. 'js/index.js',
  190. ],
  191. }),
  192. new SpriteLoaderPlugin({
  193. plainSprite: true,
  194. }),
  195. new CopyPlugin([
  196. // workaround for https://github.com/go-gitea/gitea/issues/10653
  197. {from: 'node_modules/fomantic-ui/dist/semantic.min.css', to: 'fomantic/semantic.min.css'},
  198. ]),
  199. ],
  200. performance: {
  201. hints: false,
  202. },
  203. resolve: {
  204. symlinks: false,
  205. alias: {
  206. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  207. },
  208. },
  209. watchOptions: {
  210. ignored: [
  211. 'node_modules/**',
  212. ],
  213. },
  214. };