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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. keep_fnames: /^(HTML|SVG)/, // https://github.com/fgnass/domino/issues/144
  51. output: {
  52. comments: false,
  53. },
  54. },
  55. }),
  56. new OptimizeCSSAssetsPlugin({
  57. cssProcessor: cssnano,
  58. cssProcessorOptions: {
  59. parser: PostCSSSafeParser,
  60. },
  61. cssProcessorPluginOptions: {
  62. preset: [
  63. 'default',
  64. {
  65. discardComments: {
  66. removeAll: true,
  67. },
  68. },
  69. ],
  70. },
  71. }),
  72. ],
  73. splitChunks: {
  74. chunks: 'async',
  75. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  76. }
  77. },
  78. module: {
  79. rules: [
  80. {
  81. test: /\.vue$/,
  82. exclude: /node_modules/,
  83. loader: 'vue-loader',
  84. },
  85. {
  86. test: require.resolve('jquery-datetimepicker'),
  87. use: 'imports-loader?define=>false,exports=>false',
  88. },
  89. {
  90. test: /\.worker\.js$/,
  91. use: [
  92. {
  93. loader: 'worker-loader',
  94. options: {
  95. name: '[name].js',
  96. inline: true,
  97. fallback: false,
  98. },
  99. },
  100. ],
  101. },
  102. {
  103. test: /\.js$/,
  104. exclude: /node_modules/,
  105. use: [
  106. {
  107. loader: 'babel-loader',
  108. options: {
  109. cacheDirectory: true,
  110. cacheCompression: false,
  111. cacheIdentifier: [
  112. resolve(__dirname, 'package.json'),
  113. resolve(__dirname, 'package-lock.json'),
  114. resolve(__dirname, 'webpack.config.js'),
  115. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  116. sourceMaps: true,
  117. presets: [
  118. [
  119. '@babel/preset-env',
  120. {
  121. useBuiltIns: 'usage',
  122. corejs: 3,
  123. },
  124. ],
  125. ],
  126. plugins: [
  127. [
  128. '@babel/plugin-transform-runtime',
  129. {
  130. regenerator: true,
  131. }
  132. ],
  133. '@babel/plugin-proposal-object-rest-spread',
  134. ],
  135. },
  136. },
  137. ],
  138. },
  139. {
  140. test: /\.(less|css)$/i,
  141. use: [
  142. {
  143. loader: MiniCssExtractPlugin.loader,
  144. },
  145. {
  146. loader: 'css-loader',
  147. options: {
  148. importLoaders: 2,
  149. url: false,
  150. }
  151. },
  152. {
  153. loader: 'postcss-loader',
  154. options: {
  155. plugins: () => [
  156. PostCSSPresetEnv(),
  157. ],
  158. },
  159. },
  160. {
  161. loader: 'less-loader',
  162. },
  163. ],
  164. },
  165. {
  166. test: /\.svg$/,
  167. use: [
  168. {
  169. loader: 'svg-sprite-loader',
  170. options: {
  171. extract: true,
  172. spriteFilename: 'img/svg/icons.svg',
  173. symbolId: (path) => {
  174. const {name} = parse(path);
  175. if (/@primer[/\\]octicons/.test(path)) {
  176. return `octicon-${name}`;
  177. }
  178. return name;
  179. },
  180. },
  181. },
  182. {
  183. loader: 'svgo-loader',
  184. },
  185. ],
  186. },
  187. ],
  188. },
  189. plugins: [
  190. new VueLoaderPlugin(),
  191. // avoid generating useless js output files for css- and svg-only chunks
  192. new FixStyleOnlyEntriesPlugin({
  193. extensions: ['less', 'scss', 'css', 'svg'],
  194. silent: true,
  195. }),
  196. new MiniCssExtractPlugin({
  197. filename: 'css/[name].css',
  198. chunkFilename: 'css/[name].css',
  199. }),
  200. new SourceMapDevToolPlugin({
  201. filename: 'js/[name].js.map',
  202. include: [
  203. 'js/index.js',
  204. ],
  205. }),
  206. new SpriteLoaderPlugin({
  207. plainSprite: true,
  208. }),
  209. new CopyPlugin([
  210. // workaround for https://github.com/go-gitea/gitea/issues/10653
  211. {from: 'node_modules/fomantic-ui/dist/semantic.min.css', to: 'fomantic/semantic.min.css'},
  212. ]),
  213. ],
  214. performance: {
  215. hints: false,
  216. },
  217. resolve: {
  218. symlinks: false,
  219. alias: {
  220. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  221. },
  222. },
  223. watchOptions: {
  224. ignored: [
  225. 'node_modules/**',
  226. ],
  227. },
  228. };