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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* global __dirname */
  2. const CircularDependencyPlugin = require('circular-dependency-plugin');
  3. const fs = require('fs');
  4. const { join, resolve } = require('path');
  5. const process = require('process');
  6. const webpack = require('webpack');
  7. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  8. /**
  9. * The URL of the Jitsi Meet deployment to be proxy to in the context of
  10. * development with webpack-dev-server.
  11. */
  12. const devServerProxyTarget
  13. = process.env.WEBPACK_DEV_SERVER_PROXY_TARGET || 'https://alpha.jitsi.net';
  14. /**
  15. * Build a Performance configuration object for the given size.
  16. * See: https://webpack.js.org/configuration/performance/
  17. *
  18. * @param {Object} options - options for the bundles configuration.
  19. * @param {boolean} options.analyzeBundle - whether the bundle needs to be analyzed for size.
  20. * @param {boolean} options.isProduction - whether this is a production build or not.
  21. * @param {number} size - the size limit to apply.
  22. * @returns {Object} a performance hints object.
  23. */
  24. function getPerformanceHints(options, size) {
  25. const { analyzeBundle, isProduction } = options;
  26. return {
  27. hints: isProduction && !analyzeBundle ? 'error' : false,
  28. maxAssetSize: size,
  29. maxEntrypointSize: size
  30. };
  31. }
  32. /**
  33. * Build a BundleAnalyzerPlugin plugin instance for the given bundle name.
  34. *
  35. * @param {boolean} analyzeBundle - whether the bundle needs to be analyzed for size.
  36. * @param {string} name - the name of the bundle.
  37. * @returns {Array} a configured list of plugins.
  38. */
  39. function getBundleAnalyzerPlugin(analyzeBundle, name) {
  40. if (!analyzeBundle) {
  41. return [];
  42. }
  43. return [ new BundleAnalyzerPlugin({
  44. analyzerMode: 'disabled',
  45. generateStatsFile: true,
  46. statsFilename: `${name}-stats.json`
  47. }) ];
  48. }
  49. /**
  50. * Determines whether a specific (HTTP) request is to bypass the proxy of
  51. * webpack-dev-server (i.e. is to be handled by the proxy target) and, if not,
  52. * which local file is to be served in response to the request.
  53. *
  54. * @param {Object} request - The (HTTP) request received by the proxy.
  55. * @returns {string|undefined} If the request is to be served by the proxy
  56. * target, undefined; otherwise, the path to the local file to be served.
  57. */
  58. function devServerProxyBypass({ path }) {
  59. let tpath = path;
  60. if (tpath.startsWith('/v1/_cdn/')) {
  61. // The CDN is not available in the dev server, so we need to bypass it.
  62. tpath = tpath.replace(/\/v1\/_cdn\/[^/]+\//, '/');
  63. }
  64. if (tpath.startsWith('/css/')
  65. || tpath.startsWith('/doc/')
  66. || tpath.startsWith('/fonts/')
  67. || tpath.startsWith('/images/')
  68. || tpath.startsWith('/lang/')
  69. || tpath.startsWith('/sounds/')
  70. || tpath.startsWith('/static/')
  71. || tpath.endsWith('.wasm')) {
  72. return tpath;
  73. }
  74. if (tpath.startsWith('/libs/')) {
  75. if (tpath.endsWith('.min.js') && !fs.existsSync(join(process.cwd(), tpath))) {
  76. return tpath.replace('.min.js', '.js');
  77. }
  78. return tpath;
  79. }
  80. }
  81. /**
  82. * The base Webpack configuration to bundle the JavaScript artifacts of
  83. * jitsi-meet such as app.bundle.js and external_api.js.
  84. *
  85. * @param {Object} options - options for the bundles configuration.
  86. * @param {boolean} options.detectCircularDeps - whether to detect circular dependencies or not.
  87. * @param {boolean} options.isProduction - whether this is a production build or not.
  88. * @returns {Object} the base config object.
  89. */
  90. function getConfig(options = {}) {
  91. const { detectCircularDeps, isProduction } = options;
  92. return {
  93. devtool: isProduction ? 'source-map' : 'eval-source-map',
  94. mode: isProduction ? 'production' : 'development',
  95. module: {
  96. rules: [ {
  97. // Transpile ES2015 (aka ES6) to ES5. Accept the JSX syntax by React
  98. // as well.
  99. loader: 'babel-loader',
  100. options: {
  101. // Avoid loading babel.config.js, since we only use it for React Native.
  102. configFile: false,
  103. presets: [
  104. [
  105. require.resolve('@babel/preset-env'),
  106. // Tell babel to avoid compiling imports into CommonJS
  107. // so that webpack may do tree shaking.
  108. {
  109. modules: false,
  110. // Specify our target browsers so no transpiling is
  111. // done unnecessarily. For browsers not specified
  112. // here, the ES2015+ profile will be used.
  113. targets: {
  114. chrome: 80,
  115. electron: 10,
  116. firefox: 68,
  117. safari: 14
  118. },
  119. // Consider stage 3 proposals which are implemented by some browsers already.
  120. shippedProposals: true,
  121. // Detect usage of modern JavaScript features and automatically polyfill them
  122. // with core-js.
  123. useBuiltIns: 'usage',
  124. // core-js version to use, must be in sync with the version in package.json.
  125. corejs: '3.40'
  126. }
  127. ],
  128. require.resolve('@babel/preset-react')
  129. ]
  130. },
  131. test: /\.(j|t)sx?$/,
  132. exclude: /node_modules/
  133. }, {
  134. // Allow CSS to be imported into JavaScript.
  135. test: /\.css$/,
  136. use: [
  137. 'style-loader',
  138. 'css-loader'
  139. ]
  140. }, {
  141. test: /\.svg$/,
  142. use: [ {
  143. loader: '@svgr/webpack',
  144. options: {
  145. dimensions: false,
  146. expandProps: 'start'
  147. }
  148. } ]
  149. }, {
  150. test: /\.tsx?$/,
  151. exclude: /node_modules/,
  152. loader: 'ts-loader',
  153. options: {
  154. configFile: 'tsconfig.web.json',
  155. transpileOnly: !isProduction // Skip type checking for dev builds.,
  156. }
  157. } ]
  158. },
  159. node: {
  160. // Allow the use of the real filename of the module being executed. By
  161. // default Webpack does not leak path-related information and provides a
  162. // value that is a mock (/index.js).
  163. __filename: true
  164. },
  165. optimization: {
  166. concatenateModules: isProduction,
  167. minimize: isProduction
  168. },
  169. output: {
  170. filename: `[name]${isProduction ? '.min' : ''}.js`,
  171. path: `${__dirname}/build`,
  172. publicPath: '/libs/',
  173. sourceMapFilename: '[file].map'
  174. },
  175. plugins: [
  176. detectCircularDeps
  177. && new CircularDependencyPlugin({
  178. allowAsyncCycles: false,
  179. exclude: /node_modules/,
  180. failOnError: false
  181. })
  182. ].filter(Boolean),
  183. resolve: {
  184. alias: {
  185. 'focus-visible': 'focus-visible/dist/focus-visible.min.js'
  186. },
  187. aliasFields: [
  188. 'browser'
  189. ],
  190. extensions: [
  191. '.web.js',
  192. '.web.ts',
  193. '.web.tsx',
  194. // Typescript:
  195. '.tsx',
  196. '.ts',
  197. // Webpack defaults:
  198. '.js',
  199. '.json'
  200. ],
  201. fallback: {
  202. // Provide some empty Node modules (required by AtlasKit, olm).
  203. crypto: false,
  204. fs: false,
  205. path: false,
  206. process: false
  207. }
  208. }
  209. };
  210. }
  211. /**
  212. * Helper function to build the dev server config. It's necessary to split it in
  213. * Webpack 5 because only one devServer entry is supported, so we attach it to
  214. * the main bundle.
  215. *
  216. * @returns {Object} the dev server configuration.
  217. */
  218. function getDevServerConfig() {
  219. return {
  220. client: {
  221. overlay: {
  222. errors: true,
  223. warnings: false
  224. }
  225. },
  226. host: '::',
  227. hot: true,
  228. proxy: [
  229. {
  230. context: [ '/' ],
  231. bypass: devServerProxyBypass,
  232. secure: false,
  233. target: devServerProxyTarget,
  234. headers: {
  235. 'Host': new URL(devServerProxyTarget).host
  236. }
  237. }
  238. ],
  239. server: process.env.CODESPACES ? 'http' : 'https',
  240. static: {
  241. directory: process.cwd(),
  242. watch: {
  243. ignored: file => file.endsWith('.log')
  244. }
  245. }
  246. };
  247. }
  248. module.exports = (_env, argv) => {
  249. const analyzeBundle = Boolean(process.env.ANALYZE_BUNDLE);
  250. const mode = typeof argv.mode === 'undefined' ? 'production' : argv.mode;
  251. const isProduction = mode === 'production';
  252. const configOptions = {
  253. detectCircularDeps: Boolean(process.env.DETECT_CIRCULAR_DEPS),
  254. isProduction
  255. };
  256. const config = getConfig(configOptions);
  257. const perfHintOptions = {
  258. analyzeBundle,
  259. isProduction
  260. };
  261. return [
  262. { ...config,
  263. entry: {
  264. 'app.bundle': './app.js'
  265. },
  266. devServer: isProduction ? {} : getDevServerConfig(),
  267. plugins: [
  268. ...config.plugins,
  269. ...getBundleAnalyzerPlugin(analyzeBundle, 'app'),
  270. new webpack.DefinePlugin({
  271. '__DEV__': !isProduction
  272. }),
  273. new webpack.IgnorePlugin({
  274. resourceRegExp: /^canvas$/,
  275. contextRegExp: /resemblejs$/
  276. }),
  277. new webpack.IgnorePlugin({
  278. resourceRegExp: /^\.\/locale$/,
  279. contextRegExp: /moment$/
  280. }),
  281. new webpack.ProvidePlugin({
  282. process: 'process/browser'
  283. })
  284. ],
  285. performance: getPerformanceHints(perfHintOptions, 5 * 1024 * 1024) },
  286. { ...config,
  287. entry: {
  288. 'alwaysontop': './react/features/always-on-top/index.tsx'
  289. },
  290. plugins: [
  291. ...config.plugins,
  292. ...getBundleAnalyzerPlugin(analyzeBundle, 'alwaysontop')
  293. ],
  294. performance: getPerformanceHints(perfHintOptions, 800 * 1024) },
  295. { ...config,
  296. entry: {
  297. 'close3': './static/close3.js'
  298. },
  299. plugins: [
  300. ...config.plugins,
  301. ...getBundleAnalyzerPlugin(analyzeBundle, 'close3')
  302. ],
  303. performance: getPerformanceHints(perfHintOptions, 128 * 1024) },
  304. { ...config,
  305. entry: {
  306. 'external_api': './modules/API/external/index.js'
  307. },
  308. output: { ...config.output,
  309. library: 'JitsiMeetExternalAPI',
  310. libraryTarget: 'umd' },
  311. plugins: [
  312. ...config.plugins,
  313. ...getBundleAnalyzerPlugin(analyzeBundle, 'external_api')
  314. ],
  315. performance: getPerformanceHints(perfHintOptions, 95 * 1024) },
  316. { ...config,
  317. entry: {
  318. 'face-landmarks-worker': './react/features/face-landmarks/faceLandmarksWorker.ts'
  319. },
  320. plugins: [
  321. ...config.plugins,
  322. ...getBundleAnalyzerPlugin(analyzeBundle, 'face-landmarks-worker')
  323. ],
  324. performance: getPerformanceHints(perfHintOptions, 1024 * 1024 * 2) },
  325. { ...config, /**
  326. * The NoiseSuppressorWorklet is loaded in an audio worklet which doesn't have the same
  327. * context as a normal window, (e.g. self/window is not defined).
  328. * While running a production build webpack's boilerplate code doesn't introduce any
  329. * audio worklet "unfriendly" code however when running the dev server, hot module replacement
  330. * and live reload add javascript code that can't be ran by the worklet, so we explicitly ignore
  331. * those parts with the null-loader.
  332. * The dev server also expects a `self` global object that's not available in the `AudioWorkletGlobalScope`,
  333. * so we replace it.
  334. */
  335. entry: {
  336. 'noise-suppressor-worklet':
  337. './react/features/stream-effects/noise-suppression/NoiseSuppressorWorklet.ts'
  338. },
  339. module: { rules: [
  340. ...config.module.rules,
  341. {
  342. test: resolve(__dirname, 'node_modules/webpack-dev-server/client'),
  343. loader: 'null-loader'
  344. }
  345. ] },
  346. plugins: [
  347. ],
  348. performance: getPerformanceHints(perfHintOptions, 1024 * 1024 * 2),
  349. output: {
  350. ...config.output,
  351. globalObject: 'AudioWorkletGlobalScope'
  352. } },
  353. { ...config,
  354. entry: {
  355. 'screenshot-capture-worker': './react/features/screenshot-capture/worker.ts'
  356. },
  357. plugins: [
  358. ...config.plugins,
  359. ...getBundleAnalyzerPlugin(analyzeBundle, 'screenshot-capture-worker')
  360. ],
  361. performance: getPerformanceHints(perfHintOptions, 30 * 1024) }
  362. ];
  363. };