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

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