Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

webpack.config.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 DefinePlugin({
  163. __VUE_OPTIONS_API__: true, // at the moment, many Vue components still use the Vue Options API
  164. __VUE_PROD_DEVTOOLS__: false, // do not enable devtools support in production
  165. }),
  166. new VueLoaderPlugin(),
  167. new MiniCssExtractPlugin({
  168. filename: 'css/[name].css',
  169. chunkFilename: 'css/[name].[contenthash:8].css',
  170. }),
  171. sourceMaps !== 'false' && new SourceMapDevToolPlugin({
  172. filename: '[file].[contenthash:8].map',
  173. ...(sourceMaps === 'reduced' && {include: /^js\/index\.js$/}),
  174. }),
  175. new MonacoWebpackPlugin({
  176. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  177. }),
  178. isProduction ? new LicenseCheckerWebpackPlugin({
  179. outputFilename: 'licenses.txt',
  180. outputWriter: ({dependencies}) => {
  181. const line = '-'.repeat(80);
  182. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  183. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  184. return {name, body: formatLicenseText(licenseText)};
  185. });
  186. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  187. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  188. });
  189. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  190. return modules.map(({name, version, licenseName, body}) => {
  191. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  192. return `${line}\n${title}\n${line}\n${body}`;
  193. }).join('\n');
  194. },
  195. override: {
  196. 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
  197. 'htmx.org@1.9.10': {licenseName: 'BSD-2-Clause'}, // "BSD 2-Clause" -> "BSD-2-Clause"
  198. },
  199. emitError: true,
  200. 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)',
  201. }) : new AddAssetPlugin('licenses.txt', `Licenses are disabled during development`),
  202. ],
  203. performance: {
  204. hints: false,
  205. maxEntrypointSize: Infinity,
  206. maxAssetSize: Infinity,
  207. },
  208. resolve: {
  209. symlinks: false,
  210. },
  211. watchOptions: {
  212. ignored: [
  213. 'node_modules/**',
  214. ],
  215. },
  216. stats: {
  217. assetsSort: 'name',
  218. assetsSpace: Infinity,
  219. cached: false,
  220. cachedModules: false,
  221. children: false,
  222. chunkModules: false,
  223. chunkOrigins: false,
  224. chunksSort: 'name',
  225. colors: true,
  226. entrypoints: false,
  227. excludeAssets: [
  228. /^js\/monaco-language-.+\.js$/,
  229. !isProduction && /^licenses.txt$/,
  230. ].filter(Boolean),
  231. groupAssetsByChunk: false,
  232. groupAssetsByEmitStatus: false,
  233. groupAssetsByInfo: false,
  234. groupModulesByAttributes: false,
  235. modules: false,
  236. reasons: false,
  237. runtimeModules: false,
  238. },
  239. };