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

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