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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. import {env} from 'node:process';
  14. const {EsbuildPlugin} = EsBuildLoader;
  15. const {SourceMapDevToolPlugin, DefinePlugin} = webpack;
  16. const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();
  17. const glob = (pattern) => fastGlob.sync(pattern, {
  18. cwd: dirname(fileURLToPath(new URL(import.meta.url))),
  19. absolute: true,
  20. });
  21. const themes = {};
  22. for (const path of glob('web_src/css/themes/*.css')) {
  23. themes[parse(path).name] = [path];
  24. }
  25. const isProduction = env.NODE_ENV !== 'development';
  26. let sourceMapEnabled;
  27. if ('ENABLE_SOURCEMAP' in env) {
  28. sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true';
  29. } else {
  30. sourceMapEnabled = !isProduction;
  31. }
  32. const filterCssImport = (url, ...args) => {
  33. const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import
  34. const importedFile = url.replace(/[?#].+/, '').toLowerCase();
  35. if (cssFile.includes('fomantic')) {
  36. if (/brand-icons/.test(importedFile)) return false;
  37. if (/(eot|ttf|otf|woff|svg)$/.test(importedFile)) return false;
  38. }
  39. if (cssFile.includes('katex') && /(ttf|woff)$/.test(importedFile)) {
  40. return false;
  41. }
  42. return true;
  43. };
  44. /** @type {import("webpack").Configuration} */
  45. export default {
  46. mode: isProduction ? 'production' : 'development',
  47. entry: {
  48. index: [
  49. fileURLToPath(new URL('web_src/js/jquery.js', import.meta.url)),
  50. fileURLToPath(new URL('web_src/fomantic/build/semantic.js', import.meta.url)),
  51. fileURLToPath(new URL('web_src/js/index.js', import.meta.url)),
  52. fileURLToPath(new URL('node_modules/easymde/dist/easymde.min.css', import.meta.url)),
  53. fileURLToPath(new URL('web_src/fomantic/build/semantic.css', import.meta.url)),
  54. fileURLToPath(new URL('web_src/css/index.css', import.meta.url)),
  55. ],
  56. webcomponents: [
  57. fileURLToPath(new URL('web_src/js/webcomponents/webcomponents.js', import.meta.url)),
  58. ],
  59. swagger: [
  60. fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)),
  61. fileURLToPath(new URL('web_src/css/standalone/swagger.css', import.meta.url)),
  62. ],
  63. 'eventsource.sharedworker': [
  64. fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)),
  65. ],
  66. ...themes,
  67. },
  68. devtool: false,
  69. output: {
  70. path: fileURLToPath(new URL('public', import.meta.url)),
  71. filename: () => 'js/[name].js',
  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 EsbuildPlugin({
  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: /\.js$/,
  103. exclude: /node_modules/,
  104. use: [
  105. {
  106. loader: 'esbuild-loader',
  107. options: {
  108. loader: 'js',
  109. target: 'es2015',
  110. },
  111. },
  112. ],
  113. },
  114. {
  115. test: /\.css$/i,
  116. use: [
  117. {
  118. loader: MiniCssExtractPlugin.loader,
  119. },
  120. {
  121. loader: 'css-loader',
  122. options: {
  123. sourceMap: sourceMapEnabled,
  124. url: {filter: filterCssImport},
  125. import: {filter: filterCssImport},
  126. },
  127. },
  128. ],
  129. },
  130. {
  131. test: /\.svg$/,
  132. include: fileURLToPath(new URL('public/img/svg', import.meta.url)),
  133. type: 'asset/source',
  134. },
  135. {
  136. test: /\.(ttf|woff2?)$/,
  137. type: 'asset/resource',
  138. generator: {
  139. filename: 'fonts/[name].[contenthash:8][ext]',
  140. }
  141. },
  142. {
  143. test: /\.png$/i,
  144. type: 'asset/resource',
  145. generator: {
  146. filename: 'img/webpack/[name].[contenthash:8][ext]',
  147. }
  148. },
  149. ],
  150. },
  151. plugins: [
  152. new DefinePlugin({
  153. __VUE_OPTIONS_API__: true, // at the moment, many Vue components still use the Vue Options API
  154. __VUE_PROD_DEVTOOLS__: false, // do not enable devtools support in production
  155. }),
  156. new VueLoaderPlugin(),
  157. new MiniCssExtractPlugin({
  158. filename: 'css/[name].css',
  159. chunkFilename: 'css/[name].[contenthash:8].css',
  160. }),
  161. sourceMapEnabled && (new SourceMapDevToolPlugin({
  162. filename: '[file].[contenthash:8].map',
  163. })),
  164. new MonacoWebpackPlugin({
  165. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  166. }),
  167. isProduction ? new LicenseCheckerWebpackPlugin({
  168. outputFilename: 'js/licenses.txt',
  169. outputWriter: ({dependencies}) => {
  170. const line = '-'.repeat(80);
  171. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  172. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  173. return {name, body: formatLicenseText(licenseText)};
  174. });
  175. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  176. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  177. });
  178. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  179. return modules.map(({name, version, licenseName, body}) => {
  180. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  181. return `${line}\n${title}\n${line}\n${body}`;
  182. }).join('\n');
  183. },
  184. override: {
  185. 'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
  186. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  187. },
  188. emitError: true,
  189. 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)',
  190. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  191. ],
  192. performance: {
  193. hints: false,
  194. maxEntrypointSize: Infinity,
  195. maxAssetSize: Infinity,
  196. },
  197. resolve: {
  198. symlinks: false,
  199. },
  200. watchOptions: {
  201. ignored: [
  202. 'node_modules/**',
  203. ],
  204. },
  205. stats: {
  206. assetsSort: 'name',
  207. assetsSpace: Infinity,
  208. cached: false,
  209. cachedModules: false,
  210. children: false,
  211. chunkModules: false,
  212. chunkOrigins: false,
  213. chunksSort: 'name',
  214. colors: true,
  215. entrypoints: false,
  216. excludeAssets: [
  217. /^js\/monaco-language-.+\.js$/,
  218. !isProduction && /^js\/licenses.txt$/,
  219. ].filter(Boolean),
  220. groupAssetsByChunk: false,
  221. groupAssetsByEmitStatus: false,
  222. groupAssetsByInfo: false,
  223. groupModulesByAttributes: false,
  224. modules: false,
  225. reasons: false,
  226. runtimeModules: false,
  227. },
  228. };