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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/less/themes/*.less')) {
  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/less/index.less', 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/less/standalone/swagger.less', 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: /.less$/i,
  146. use: [
  147. {
  148. loader: MiniCssExtractPlugin.loader,
  149. },
  150. {
  151. loader: 'css-loader',
  152. options: {
  153. sourceMap: true,
  154. importLoaders: 1,
  155. url: {filter: filterCssImport},
  156. import: {filter: filterCssImport},
  157. },
  158. },
  159. {
  160. loader: 'less-loader',
  161. options: {
  162. sourceMap: true,
  163. },
  164. },
  165. ],
  166. },
  167. {
  168. test: /\.svg$/,
  169. include: fileURLToPath(new URL('public/img/svg', import.meta.url)),
  170. type: 'asset/source',
  171. },
  172. {
  173. test: /\.(ttf|woff2?)$/,
  174. type: 'asset/resource',
  175. generator: {
  176. filename: 'fonts/[name].[contenthash:8][ext]',
  177. }
  178. },
  179. {
  180. test: /\.png$/i,
  181. type: 'asset/resource',
  182. generator: {
  183. filename: 'img/webpack/[name].[contenthash:8][ext]',
  184. }
  185. },
  186. ],
  187. },
  188. plugins: [
  189. new VueLoaderPlugin(),
  190. new MiniCssExtractPlugin({
  191. filename: 'css/[name].css',
  192. chunkFilename: 'css/[name].[contenthash:8].css',
  193. }),
  194. new SourceMapDevToolPlugin({
  195. filename: '[file].[contenthash:8].map',
  196. include: [
  197. 'js/index.js',
  198. 'css/index.css',
  199. ],
  200. }),
  201. new MonacoWebpackPlugin({
  202. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  203. }),
  204. isProduction ? new LicenseCheckerWebpackPlugin({
  205. outputFilename: 'js/licenses.txt',
  206. outputWriter: ({dependencies}) => {
  207. const line = '-'.repeat(80);
  208. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  209. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  210. return {name, body: formatLicenseText(licenseText)};
  211. });
  212. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  213. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  214. });
  215. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  216. return modules.map(({name, version, licenseName, body}) => {
  217. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  218. return `${line}\n${title}\n${line}\n${body}`;
  219. }).join('\n');
  220. },
  221. override: {
  222. 'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
  223. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  224. },
  225. emitError: true,
  226. 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)',
  227. ignore: [
  228. 'font-awesome',
  229. ],
  230. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  231. ],
  232. performance: {
  233. hints: false,
  234. maxEntrypointSize: Infinity,
  235. maxAssetSize: Infinity,
  236. },
  237. resolve: {
  238. symlinks: false,
  239. },
  240. watchOptions: {
  241. ignored: [
  242. 'node_modules/**',
  243. ],
  244. },
  245. stats: {
  246. assetsSort: 'name',
  247. assetsSpace: Infinity,
  248. cached: false,
  249. cachedModules: false,
  250. children: false,
  251. chunkModules: false,
  252. chunkOrigins: false,
  253. chunksSort: 'name',
  254. colors: true,
  255. entrypoints: false,
  256. excludeAssets: [
  257. /^js\/monaco-language-.+\.js$/,
  258. !isProduction && /^js\/licenses.txt$/,
  259. ].filter(Boolean),
  260. groupAssetsByChunk: false,
  261. groupAssetsByEmitStatus: false,
  262. groupAssetsByInfo: false,
  263. groupModulesByAttributes: false,
  264. modules: false,
  265. reasons: false,
  266. runtimeModules: false,
  267. },
  268. };