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

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