Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

webpack.config.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 'path';
  10. import webpack from 'webpack';
  11. import {fileURLToPath} from 'url';
  12. import {readFileSync} from 'fs';
  13. const {ESBuildMinifyPlugin} = EsBuildLoader;
  14. const {SourceMapDevToolPlugin} = webpack;
  15. const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();
  16. const glob = (pattern) => fastGlob.sync(pattern, {
  17. cwd: dirname(fileURLToPath(new URL(import.meta.url))),
  18. absolute: true,
  19. });
  20. const themes = {};
  21. for (const path of glob('web_src/less/themes/*.less')) {
  22. themes[parse(path).name] = [path];
  23. }
  24. const isProduction = process.env.NODE_ENV !== 'development';
  25. const filterCssImport = (url, ...args) => {
  26. const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import
  27. const importedFile = url.replace(/[?#].+/, '').toLowerCase();
  28. if (cssFile.includes('fomantic')) {
  29. if (/brand-icons/.test(importedFile)) return false;
  30. if (/(eot|ttf|otf|woff|svg)$/.test(importedFile)) return false;
  31. }
  32. if (cssFile.includes('katex') && /(ttf|woff)$/.test(importedFile)) {
  33. return false;
  34. }
  35. if (cssFile.includes('font-awesome') && /(eot|ttf|otf|woff|svg)$/.test(importedFile)) {
  36. return false;
  37. }
  38. return true;
  39. };
  40. export default {
  41. mode: isProduction ? 'production' : 'development',
  42. entry: {
  43. index: [
  44. fileURLToPath(new URL('web_src/js/jquery.js', import.meta.url)),
  45. fileURLToPath(new URL('web_src/fomantic/build/semantic.js', import.meta.url)),
  46. fileURLToPath(new URL('web_src/js/index.js', import.meta.url)),
  47. fileURLToPath(new URL('node_modules/easymde/dist/easymde.min.css', import.meta.url)),
  48. fileURLToPath(new URL('web_src/fomantic/build/semantic.css', import.meta.url)),
  49. fileURLToPath(new URL('web_src/less/index.less', import.meta.url)),
  50. ],
  51. swagger: [
  52. fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)),
  53. fileURLToPath(new URL('web_src/less/standalone/swagger.less', import.meta.url)),
  54. ],
  55. serviceworker: [
  56. fileURLToPath(new URL('web_src/js/serviceworker.js', import.meta.url)),
  57. ],
  58. 'eventsource.sharedworker': [
  59. fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)),
  60. ],
  61. ...themes,
  62. },
  63. devtool: false,
  64. output: {
  65. path: fileURLToPath(new URL('public', import.meta.url)),
  66. filename: ({chunk}) => {
  67. // serviceworker can only manage assets below it's script's directory so
  68. // we have to put it in / instead of /js/
  69. return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js';
  70. },
  71. chunkFilename: ({chunk}) => {
  72. const language = (/monaco.*languages?_.+?_(.+?)_/.exec(chunk.id) || [])[1];
  73. return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;
  74. },
  75. },
  76. optimization: {
  77. minimize: isProduction,
  78. minimizer: [
  79. new ESBuildMinifyPlugin({
  80. target: 'es2015',
  81. minify: true,
  82. css: true,
  83. legalComments: 'none',
  84. }),
  85. ],
  86. splitChunks: {
  87. chunks: 'async',
  88. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  89. },
  90. moduleIds: 'named',
  91. chunkIds: 'named',
  92. },
  93. module: {
  94. rules: [
  95. {
  96. test: /\.vue$/,
  97. exclude: /node_modules/,
  98. loader: 'vue-loader',
  99. },
  100. {
  101. test: /\.worker\.js$/,
  102. exclude: /monaco/,
  103. use: [
  104. {
  105. loader: 'worker-loader',
  106. options: {
  107. inline: 'no-fallback',
  108. },
  109. },
  110. ],
  111. },
  112. {
  113. test: /\.js$/,
  114. exclude: /node_modules/,
  115. use: [
  116. {
  117. loader: 'esbuild-loader',
  118. options: {
  119. target: 'es2015'
  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: true,
  134. url: {filter: filterCssImport},
  135. import: {filter: filterCssImport},
  136. },
  137. },
  138. ],
  139. },
  140. {
  141. test: /.less$/i,
  142. use: [
  143. {
  144. loader: MiniCssExtractPlugin.loader,
  145. },
  146. {
  147. loader: 'css-loader',
  148. options: {
  149. sourceMap: true,
  150. importLoaders: 1,
  151. url: {filter: filterCssImport},
  152. import: {filter: filterCssImport},
  153. },
  154. },
  155. {
  156. loader: 'less-loader',
  157. options: {
  158. sourceMap: true,
  159. },
  160. },
  161. ],
  162. },
  163. {
  164. test: /\.svg$/,
  165. include: fileURLToPath(new URL('public/img/svg', import.meta.url)),
  166. type: 'asset/source',
  167. },
  168. {
  169. test: /\.(ttf|woff2?)$/,
  170. type: 'asset/resource',
  171. generator: {
  172. filename: 'fonts/[name].[contenthash:8][ext]',
  173. }
  174. },
  175. {
  176. test: /\.png$/i,
  177. type: 'asset/resource',
  178. generator: {
  179. filename: 'img/webpack/[name].[contenthash:8][ext]',
  180. }
  181. },
  182. ],
  183. },
  184. plugins: [
  185. new VueLoaderPlugin(),
  186. new MiniCssExtractPlugin({
  187. filename: 'css/[name].css',
  188. chunkFilename: 'css/[name].[contenthash:8].css',
  189. }),
  190. new SourceMapDevToolPlugin({
  191. filename: '[file].[contenthash:8].map',
  192. include: [
  193. 'js/index.js',
  194. 'css/index.css',
  195. ],
  196. }),
  197. new MonacoWebpackPlugin({
  198. filename: 'js/monaco-[name].[contenthash:8].worker.js',
  199. }),
  200. isProduction ? new LicenseCheckerWebpackPlugin({
  201. outputFilename: 'js/licenses.txt',
  202. outputWriter: ({dependencies}) => {
  203. const line = '-'.repeat(80);
  204. const goJson = readFileSync('assets/go-licenses.json', 'utf8');
  205. const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
  206. return {name, body: formatLicenseText(licenseText)};
  207. });
  208. const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
  209. return {name, version, licenseName, body: formatLicenseText(licenseText)};
  210. });
  211. const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
  212. return modules.map(({name, version, licenseName, body}) => {
  213. const title = licenseName ? `${name}@${version} - ${licenseName}` : name;
  214. return `${line}\n${title}\n${line}\n${body}`;
  215. }).join('\n');
  216. },
  217. override: {
  218. 'jquery.are-you-sure@*': {licenseName: 'MIT'},
  219. },
  220. allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC)',
  221. ignore: [
  222. 'font-awesome',
  223. ],
  224. }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
  225. ],
  226. performance: {
  227. hints: false,
  228. maxEntrypointSize: Infinity,
  229. maxAssetSize: Infinity,
  230. },
  231. resolve: {
  232. symlinks: false,
  233. },
  234. watchOptions: {
  235. ignored: [
  236. 'node_modules/**',
  237. ],
  238. },
  239. stats: {
  240. assetsSort: 'name',
  241. assetsSpace: Infinity,
  242. cached: false,
  243. cachedModules: false,
  244. children: false,
  245. chunkModules: false,
  246. chunkOrigins: false,
  247. chunksSort: 'name',
  248. colors: true,
  249. entrypoints: false,
  250. excludeAssets: [
  251. /^js\/monaco-language-.+\.js$/,
  252. !isProduction && /^js\/licenses.txt$/,
  253. ].filter(Boolean),
  254. groupAssetsByChunk: false,
  255. groupAssetsByEmitStatus: false,
  256. groupAssetsByInfo: false,
  257. groupModulesByAttributes: false,
  258. modules: false,
  259. reasons: false,
  260. runtimeModules: false,
  261. },
  262. };