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

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