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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. swagger: [
  53. fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)),
  54. fileURLToPath(new URL('web_src/less/standalone/swagger.less', import.meta.url)),
  55. ],
  56. serviceworker: [
  57. fileURLToPath(new URL('web_src/js/serviceworker.js', import.meta.url)),
  58. ],
  59. 'eventsource.sharedworker': [
  60. fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)),
  61. ],
  62. ...themes,
  63. },
  64. devtool: false,
  65. output: {
  66. path: fileURLToPath(new URL('public', import.meta.url)),
  67. filename: ({chunk}) => {
  68. // serviceworker can only manage assets below it's script's directory so
  69. // we have to put it in / instead of /js/
  70. return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js';
  71. },
  72. chunkFilename: ({chunk}) => {
  73. const language = (/monaco.*languages?_.+?_(.+?)_/.exec(chunk.id) || [])[1];
  74. return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;
  75. },
  76. },
  77. optimization: {
  78. minimize: isProduction,
  79. minimizer: [
  80. new ESBuildMinifyPlugin({
  81. target: 'es2015',
  82. minify: true,
  83. css: true,
  84. legalComments: 'none',
  85. }),
  86. ],
  87. splitChunks: {
  88. chunks: 'async',
  89. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  90. },
  91. moduleIds: 'named',
  92. chunkIds: 'named',
  93. },
  94. module: {
  95. rules: [
  96. {
  97. test: /\.vue$/,
  98. exclude: /node_modules/,
  99. loader: 'vue-loader',
  100. },
  101. {
  102. test: /\.worker\.js$/,
  103. exclude: /monaco/,
  104. use: [
  105. {
  106. loader: 'worker-loader',
  107. options: {
  108. inline: 'no-fallback',
  109. },
  110. },
  111. ],
  112. },
  113. {
  114. test: /\.js$/,
  115. exclude: /node_modules/,
  116. use: [
  117. {
  118. loader: 'esbuild-loader',
  119. options: {
  120. target: 'es2015'
  121. },
  122. },
  123. ],
  124. },
  125. {
  126. test: /.css$/i,
  127. use: [
  128. {
  129. loader: MiniCssExtractPlugin.loader,
  130. },
  131. {
  132. loader: 'css-loader',
  133. options: {
  134. sourceMap: true,
  135. url: {filter: filterCssImport},
  136. import: {filter: filterCssImport},
  137. },
  138. },
  139. ],
  140. },
  141. {
  142. test: /.less$/i,
  143. use: [
  144. {
  145. loader: MiniCssExtractPlugin.loader,
  146. },
  147. {
  148. loader: 'css-loader',
  149. options: {
  150. sourceMap: true,
  151. importLoaders: 1,
  152. url: {filter: filterCssImport},
  153. import: {filter: filterCssImport},
  154. },
  155. },
  156. {
  157. loader: 'less-loader',
  158. options: {
  159. sourceMap: true,
  160. },
  161. },
  162. ],
  163. },
  164. {
  165. test: /\.svg$/,
  166. include: fileURLToPath(new URL('public/img/svg', import.meta.url)),
  167. type: 'asset/source',
  168. },
  169. {
  170. test: /\.(ttf|woff2?)$/,
  171. type: 'asset/resource',
  172. generator: {
  173. filename: 'fonts/[name].[contenthash:8][ext]',
  174. }
  175. },
  176. {
  177. test: /\.png$/i,
  178. type: 'asset/resource',
  179. generator: {
  180. filename: 'img/webpack/[name].[contenthash:8][ext]',
  181. }
  182. },
  183. ],
  184. },
  185. plugins: [
  186. new VueLoaderPlugin(),
  187. new MiniCssExtractPlugin({
  188. filename: 'css/[name].css',
  189. chunkFilename: 'css/[name].[contenthash:8].css',
  190. }),
  191. new SourceMapDevToolPlugin({
  192. filename: '[file].[contenthash:8].map',
  193. include: [
  194. 'js/index.js',
  195. 'css/index.css',
  196. ],
  197. }),
  198. new MonacoWebpackPlugin({
  199. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  200. }),
  201. isProduction ? new LicenseCheckerWebpackPlugin({
  202. outputFilename: 'js/licenses.txt',
  203. outputWriter: ({dependencies}) => {
  204. const line = '-'.repeat(80);
  205. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  206. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  207. return {name, body: formatLicenseText(licenseText)};
  208. });
  209. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  210. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  211. });
  212. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  213. return modules.map(({name, version, licenseName, body}) => {
  214. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  215. return `${line}\n${title}\n${line}\n${body}`;
  216. }).join('\n');
  217. },
  218. override: {
  219. 'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
  220. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  221. },
  222. emitError: true,
  223. allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC OR CPAL-1.0 OR Unlicense)',
  224. ignore: [
  225. 'font-awesome',
  226. ],
  227. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  228. ],
  229. performance: {
  230. hints: false,
  231. maxEntrypointSize: Infinity,
  232. maxAssetSize: Infinity,
  233. },
  234. resolve: {
  235. symlinks: false,
  236. },
  237. watchOptions: {
  238. ignored: [
  239. 'node_modules/**',
  240. ],
  241. },
  242. stats: {
  243. assetsSort: 'name',
  244. assetsSpace: Infinity,
  245. cached: false,
  246. cachedModules: false,
  247. children: false,
  248. chunkModules: false,
  249. chunkOrigins: false,
  250. chunksSort: 'name',
  251. colors: true,
  252. entrypoints: false,
  253. excludeAssets: [
  254. /^js\/monaco-language-.+\.js$/,
  255. !isProduction && /^js\/licenses.txt$/,
  256. ].filter(Boolean),
  257. groupAssetsByChunk: false,
  258. groupAssetsByEmitStatus: false,
  259. groupAssetsByInfo: false,
  260. groupModulesByAttributes: false,
  261. modules: false,
  262. reasons: false,
  263. runtimeModules: false,
  264. },
  265. };