Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

webpack.config.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import fastGlob from 'fast-glob';
  2. import wrapAnsi from 'wrap-ansi';
  3. import AddAssetPlugin from 'add-asset-webpack-plugin';
  4. import LicenseCheckerWebpackPlugin from 'license-checker-webpack-plugin';
  5. import MiniCssExtractPlugin from 'mini-css-extract-plugin';
  6. import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin';
  7. import {VueLoaderPlugin} from 'vue-loader';
  8. import EsBuildLoader from 'esbuild-loader';
  9. import {parse, dirname} from 'node:path';
  10. import webpack from 'webpack';
  11. import {fileURLToPath} from 'node:url';
  12. import {readFileSync} from 'node:fs';
  13. const {ESBuildMinifyPlugin} = EsBuildLoader;
  14. const {SourceMapDevToolPlugin} = webpack;
  15. const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();
  16. const glob = (pattern) => fastGlob.sync(pattern, {
  17. cwd: dirname(fileURLToPath(new URL(import.meta.url))),
  18. absolute: true,
  19. });
  20. const themes = {};
  21. for (const path of glob('web_src/css/themes/*.css')) {
  22. themes[parse(path).name] = [path];
  23. }
  24. const isProduction = process.env.NODE_ENV !== 'development';
  25. const filterCssImport = (url, ...args) => {
  26. const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import
  27. const importedFile = url.replace(/[?#].+/, '').toLowerCase();
  28. if (cssFile.includes('fomantic')) {
  29. if (/brand-icons/.test(importedFile)) return false;
  30. if (/(eot|ttf|otf|woff|svg)$/.test(importedFile)) return false;
  31. }
  32. if (cssFile.includes('katex') && /(ttf|woff)$/.test(importedFile)) {
  33. return false;
  34. }
  35. if (cssFile.includes('font-awesome') && /(eot|ttf|otf|woff|svg)$/.test(importedFile)) {
  36. return false;
  37. }
  38. return true;
  39. };
  40. /** @type {import("webpack").Configuration} */
  41. export default {
  42. mode: isProduction ? 'production' : 'development',
  43. entry: {
  44. index: [
  45. fileURLToPath(new URL('web_src/js/jquery.js', import.meta.url)),
  46. fileURLToPath(new URL('web_src/fomantic/build/semantic.js', import.meta.url)),
  47. fileURLToPath(new URL('web_src/js/index.js', import.meta.url)),
  48. fileURLToPath(new URL('node_modules/easymde/dist/easymde.min.css', import.meta.url)),
  49. fileURLToPath(new URL('web_src/fomantic/build/semantic.css', import.meta.url)),
  50. fileURLToPath(new URL('web_src/css/index.css', import.meta.url)),
  51. ],
  52. webcomponents: [
  53. fileURLToPath(new URL('web_src/js/webcomponents/GiteaOriginUrl.js', import.meta.url)),
  54. ],
  55. swagger: [
  56. fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)),
  57. fileURLToPath(new URL('web_src/css/standalone/swagger.css', import.meta.url)),
  58. ],
  59. serviceworker: [
  60. fileURLToPath(new URL('web_src/js/serviceworker.js', import.meta.url)),
  61. ],
  62. 'eventsource.sharedworker': [
  63. fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)),
  64. ],
  65. ...themes,
  66. },
  67. devtool: false,
  68. output: {
  69. path: fileURLToPath(new URL('public', import.meta.url)),
  70. filename: ({chunk}) => {
  71. // serviceworker can only manage assets below it's script's directory so
  72. // we have to put it in / instead of /js/
  73. return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js';
  74. },
  75. chunkFilename: ({chunk}) => {
  76. const language = (/monaco.*languages?_.+?_(.+?)_/.exec(chunk.id) || [])[1];
  77. return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;
  78. },
  79. },
  80. optimization: {
  81. minimize: isProduction,
  82. minimizer: [
  83. new ESBuildMinifyPlugin({
  84. target: 'es2015',
  85. minify: true,
  86. css: true,
  87. legalComments: 'none',
  88. }),
  89. ],
  90. splitChunks: {
  91. chunks: 'async',
  92. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  93. },
  94. moduleIds: 'named',
  95. chunkIds: 'named',
  96. },
  97. module: {
  98. rules: [
  99. {
  100. test: /\.vue$/,
  101. exclude: /node_modules/,
  102. loader: 'vue-loader',
  103. },
  104. {
  105. test: /\.worker\.js$/,
  106. exclude: /monaco/,
  107. use: [
  108. {
  109. loader: 'worker-loader',
  110. options: {
  111. inline: 'no-fallback',
  112. },
  113. },
  114. ],
  115. },
  116. {
  117. test: /\.js$/,
  118. exclude: /node_modules/,
  119. use: [
  120. {
  121. loader: 'esbuild-loader',
  122. options: {
  123. target: 'es2015'
  124. },
  125. },
  126. ],
  127. },
  128. {
  129. test: /\.css$/i,
  130. use: [
  131. {
  132. loader: MiniCssExtractPlugin.loader,
  133. },
  134. {
  135. loader: 'css-loader',
  136. options: {
  137. sourceMap: true,
  138. url: {filter: filterCssImport},
  139. import: {filter: filterCssImport},
  140. },
  141. },
  142. ],
  143. },
  144. {
  145. test: /\.svg$/,
  146. include: fileURLToPath(new URL('public/img/svg', import.meta.url)),
  147. type: 'asset/source',
  148. },
  149. {
  150. test: /\.(ttf|woff2?)$/,
  151. type: 'asset/resource',
  152. generator: {
  153. filename: 'fonts/[name].[contenthash:8][ext]',
  154. }
  155. },
  156. {
  157. test: /\.png$/i,
  158. type: 'asset/resource',
  159. generator: {
  160. filename: 'img/webpack/[name].[contenthash:8][ext]',
  161. }
  162. },
  163. ],
  164. },
  165. plugins: [
  166. new VueLoaderPlugin(),
  167. new MiniCssExtractPlugin({
  168. filename: 'css/[name].css',
  169. chunkFilename: 'css/[name].[contenthash:8].css',
  170. }),
  171. new SourceMapDevToolPlugin({
  172. filename: '[file].[contenthash:8].map',
  173. include: [
  174. 'js/index.js',
  175. 'css/index.css',
  176. ],
  177. }),
  178. new MonacoWebpackPlugin({
  179. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  180. }),
  181. isProduction ? new LicenseCheckerWebpackPlugin({
  182. outputFilename: 'js/licenses.txt',
  183. outputWriter: ({dependencies}) => {
  184. const line = '-'.repeat(80);
  185. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  186. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  187. return {name, body: formatLicenseText(licenseText)};
  188. });
  189. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  190. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  191. });
  192. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  193. return modules.map(({name, version, licenseName, body}) => {
  194. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  195. return `${line}\n${title}\n${line}\n${body}`;
  196. }).join('\n');
  197. },
  198. override: {
  199. 'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
  200. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  201. },
  202. emitError: true,
  203. allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC OR CPAL-1.0 OR Unlicense OR EPL-1.0 OR EPL-2.0)',
  204. ignore: [
  205. 'font-awesome',
  206. ],
  207. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  208. ],
  209. performance: {
  210. hints: false,
  211. maxEntrypointSize: Infinity,
  212. maxAssetSize: Infinity,
  213. },
  214. resolve: {
  215. symlinks: false,
  216. },
  217. watchOptions: {
  218. ignored: [
  219. 'node_modules/**',
  220. ],
  221. },
  222. stats: {
  223. assetsSort: 'name',
  224. assetsSpace: Infinity,
  225. cached: false,
  226. cachedModules: false,
  227. children: false,
  228. chunkModules: false,
  229. chunkOrigins: false,
  230. chunksSort: 'name',
  231. colors: true,
  232. entrypoints: false,
  233. excludeAssets: [
  234. /^js\/monaco-language-.+\.js$/,
  235. !isProduction && /^js\/licenses.txt$/,
  236. ].filter(Boolean),
  237. groupAssetsByChunk: false,
  238. groupAssetsByEmitStatus: false,
  239. groupAssetsByInfo: false,
  240. groupModulesByAttributes: false,
  241. modules: false,
  242. reasons: false,
  243. runtimeModules: false,
  244. },
  245. };