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

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