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

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