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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // in case lightningcss fails to load, fall back to esbuild for css minify
  45. let LightningCssMinifyPlugin;
  46. try {
  47. ({LightningCssMinifyPlugin} = await import('lightningcss-loader'));
  48. } catch {}
  49. /** @type {import("webpack").Configuration} */
  50. export default {
  51. mode: isProduction ? 'production' : 'development',
  52. entry: {
  53. index: [
  54. fileURLToPath(new URL('web_src/js/jquery.js', import.meta.url)),
  55. fileURLToPath(new URL('web_src/fomantic/build/semantic.js', import.meta.url)),
  56. fileURLToPath(new URL('web_src/js/index.js', import.meta.url)),
  57. fileURLToPath(new URL('node_modules/easymde/dist/easymde.min.css', import.meta.url)),
  58. fileURLToPath(new URL('web_src/fomantic/build/semantic.css', import.meta.url)),
  59. fileURLToPath(new URL('web_src/css/index.css', import.meta.url)),
  60. ],
  61. webcomponents: [
  62. fileURLToPath(new URL('web_src/js/webcomponents/webcomponents.js', import.meta.url)),
  63. ],
  64. swagger: [
  65. fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)),
  66. fileURLToPath(new URL('web_src/css/standalone/swagger.css', import.meta.url)),
  67. ],
  68. 'eventsource.sharedworker': [
  69. fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)),
  70. ],
  71. ...(!isProduction && {
  72. devtest: [
  73. fileURLToPath(new URL('web_src/js/standalone/devtest.js', import.meta.url)),
  74. fileURLToPath(new URL('web_src/css/standalone/devtest.css', import.meta.url)),
  75. ],
  76. }),
  77. ...themes,
  78. },
  79. devtool: false,
  80. output: {
  81. path: fileURLToPath(new URL('public/assets', import.meta.url)),
  82. filename: () => 'js/[name].js',
  83. chunkFilename: ({chunk}) => {
  84. const language = (/monaco.*languages?_.+?_(.+?)_/.exec(chunk.id) || [])[1];
  85. return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;
  86. },
  87. },
  88. optimization: {
  89. minimize: isProduction,
  90. minimizer: [
  91. new EsbuildPlugin({
  92. target: 'es2015',
  93. minify: true,
  94. css: !LightningCssMinifyPlugin,
  95. legalComments: 'none',
  96. }),
  97. LightningCssMinifyPlugin && new LightningCssMinifyPlugin(),
  98. ],
  99. splitChunks: {
  100. chunks: 'async',
  101. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  102. },
  103. moduleIds: 'named',
  104. chunkIds: 'named',
  105. },
  106. module: {
  107. rules: [
  108. {
  109. test: /\.vue$/,
  110. exclude: /node_modules/,
  111. loader: 'vue-loader',
  112. },
  113. {
  114. test: /\.js$/,
  115. exclude: /node_modules/,
  116. use: [
  117. {
  118. loader: 'esbuild-loader',
  119. options: {
  120. loader: 'js',
  121. target: 'es2015',
  122. },
  123. },
  124. ],
  125. },
  126. {
  127. test: /\.css$/i,
  128. use: [
  129. {
  130. loader: MiniCssExtractPlugin.loader,
  131. },
  132. {
  133. loader: 'css-loader',
  134. options: {
  135. sourceMap: sourceMapEnabled,
  136. url: {filter: filterCssImport},
  137. import: {filter: filterCssImport},
  138. },
  139. },
  140. ],
  141. },
  142. {
  143. test: /\.svg$/,
  144. include: fileURLToPath(new URL('public/assets/img/svg', import.meta.url)),
  145. type: 'asset/source',
  146. },
  147. {
  148. test: /\.(ttf|woff2?)$/,
  149. type: 'asset/resource',
  150. generator: {
  151. filename: 'fonts/[name].[contenthash:8][ext]',
  152. }
  153. },
  154. {
  155. test: /\.png$/i,
  156. type: 'asset/resource',
  157. generator: {
  158. filename: 'img/webpack/[name].[contenthash:8][ext]',
  159. }
  160. },
  161. ],
  162. },
  163. plugins: [
  164. new DefinePlugin({
  165. __VUE_OPTIONS_API__: true, // at the moment, many Vue components still use the Vue Options API
  166. __VUE_PROD_DEVTOOLS__: false, // do not enable devtools support in production
  167. }),
  168. new VueLoaderPlugin(),
  169. new MiniCssExtractPlugin({
  170. filename: 'css/[name].css',
  171. chunkFilename: 'css/[name].[contenthash:8].css',
  172. }),
  173. sourceMapEnabled && (new SourceMapDevToolPlugin({
  174. filename: '[file].[contenthash:8].map',
  175. })),
  176. new MonacoWebpackPlugin({
  177. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  178. }),
  179. isProduction ? new LicenseCheckerWebpackPlugin({
  180. outputFilename: 'js/licenses.txt',
  181. outputWriter: ({dependencies}) => {
  182. const line = '-'.repeat(80);
  183. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  184. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  185. return {name, body: formatLicenseText(licenseText)};
  186. });
  187. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  188. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  189. });
  190. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  191. return modules.map(({name, version, licenseName, body}) => {
  192. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  193. return `${line}\n${title}\n${line}\n${body}`;
  194. }).join('\n');
  195. },
  196. override: {
  197. 'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
  198. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  199. },
  200. emitError: true,
  201. 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)',
  202. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  203. ],
  204. performance: {
  205. hints: false,
  206. maxEntrypointSize: Infinity,
  207. maxAssetSize: Infinity,
  208. },
  209. resolve: {
  210. symlinks: false,
  211. },
  212. watchOptions: {
  213. ignored: [
  214. 'node_modules/**',
  215. ],
  216. },
  217. stats: {
  218. assetsSort: 'name',
  219. assetsSpace: Infinity,
  220. cached: false,
  221. cachedModules: false,
  222. children: false,
  223. chunkModules: false,
  224. chunkOrigins: false,
  225. chunksSort: 'name',
  226. colors: true,
  227. entrypoints: false,
  228. excludeAssets: [
  229. /^js\/monaco-language-.+\.js$/,
  230. !isProduction && /^js\/licenses.txt$/,
  231. ].filter(Boolean),
  232. groupAssetsByChunk: false,
  233. groupAssetsByEmitStatus: false,
  234. groupAssetsByInfo: false,
  235. groupModulesByAttributes: false,
  236. modules: false,
  237. reasons: false,
  238. runtimeModules: false,
  239. },
  240. };