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

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