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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.minimize - whether the code should be minimized 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, minimize } = options;
  26. return {
  27. hints: minimize && !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. if (path.startsWith('/css/')
  60. || path.startsWith('/doc/')
  61. || path.startsWith('/fonts/')
  62. || path.startsWith('/images/')
  63. || path.startsWith('/lang/')
  64. || path.startsWith('/sounds/')
  65. || path.startsWith('/static/')
  66. || path.endsWith('.wasm')) {
  67. return path;
  68. }
  69. if (path.startsWith('/libs/')) {
  70. if (path.endsWith('.min.js') && !fs.existsSync(join(process.cwd(), path))) {
  71. return path.replace('.min.js', '.js');
  72. }
  73. return path;
  74. }
  75. }
  76. /**
  77. * The base Webpack configuration to bundle the JavaScript artifacts of
  78. * jitsi-meet such as app.bundle.js and external_api.js.
  79. *
  80. * @param {Object} options - options for the bundles configuration.
  81. * @param {boolean} options.detectCircularDeps - whether to detect circular dependencies or not.
  82. * @param {boolean} options.minimize - whether the code should be minimized or not.
  83. * @returns {Object} the base config object.
  84. */
  85. function getConfig(options = {}) {
  86. const { detectCircularDeps, minimize } = options;
  87. return {
  88. devtool: minimize ? 'source-map' : 'eval-source-map',
  89. mode: minimize ? 'production' : 'development',
  90. module: {
  91. rules: [ {
  92. // Transpile ES2015 (aka ES6) to ES5. Accept the JSX syntax by React
  93. // as well.
  94. loader: 'babel-loader',
  95. options: {
  96. // Avoid loading babel.config.js, since we only use it for React Native.
  97. configFile: false,
  98. // XXX The require.resolve below solves failures to locate the
  99. // presets when lib-jitsi-meet, for example, is npm linked in
  100. // jitsi-meet.
  101. plugins: [
  102. require.resolve('@babel/plugin-proposal-export-default-from')
  103. ],
  104. presets: [
  105. [
  106. require.resolve('@babel/preset-env'),
  107. // Tell babel to avoid compiling imports into CommonJS
  108. // so that webpack may do tree shaking.
  109. {
  110. modules: false,
  111. // Specify our target browsers so no transpiling is
  112. // done unnecessarily. For browsers not specified
  113. // here, the ES2015+ profile will be used.
  114. targets: {
  115. chrome: 80,
  116. electron: 10,
  117. firefox: 68,
  118. safari: 14
  119. }
  120. }
  121. ],
  122. require.resolve('@babel/preset-flow'),
  123. require.resolve('@babel/preset-react')
  124. ]
  125. },
  126. test: /\.jsx?$/
  127. }, {
  128. // Allow CSS to be imported into JavaScript.
  129. test: /\.css$/,
  130. use: [
  131. 'style-loader',
  132. 'css-loader'
  133. ]
  134. }, {
  135. test: /\/node_modules\/@atlaskit\/modal-dialog\/.*\.js$/,
  136. resolve: {
  137. alias: {
  138. 'react-focus-lock': `${__dirname}/react/features/base/util/react-focus-lock-wrapper.js`,
  139. '../styled/Modal': `${__dirname}/react/features/base/dialog/components/web/ThemedDialog.js`
  140. }
  141. }
  142. }, {
  143. test: /\/react\/features\/base\/util\/react-focus-lock-wrapper.js$/,
  144. resolve: {
  145. alias: {
  146. 'react-focus-lock': `${__dirname}/node_modules/react-focus-lock`
  147. }
  148. }
  149. }, {
  150. test: /\.svg$/,
  151. use: [ {
  152. loader: '@svgr/webpack',
  153. options: {
  154. dimensions: false,
  155. expandProps: 'start'
  156. }
  157. } ]
  158. }, {
  159. test: /\.tsx?$/,
  160. exclude: /node_modules/,
  161. loader: 'ts-loader'
  162. } ]
  163. },
  164. node: {
  165. // Allow the use of the real filename of the module being executed. By
  166. // default Webpack does not leak path-related information and provides a
  167. // value that is a mock (/index.js).
  168. __filename: true
  169. },
  170. optimization: {
  171. concatenateModules: minimize,
  172. minimize
  173. },
  174. output: {
  175. filename: `[name]${minimize ? '.min' : ''}.js`,
  176. path: `${__dirname}/build`,
  177. publicPath: '/libs/',
  178. sourceMapFilename: '[file].map'
  179. },
  180. plugins: [
  181. detectCircularDeps
  182. && new CircularDependencyPlugin({
  183. allowAsyncCycles: false,
  184. exclude: /node_modules/,
  185. failOnError: false
  186. })
  187. ].filter(Boolean),
  188. resolve: {
  189. alias: {
  190. 'focus-visible': 'focus-visible/dist/focus-visible.min.js'
  191. },
  192. aliasFields: [
  193. 'browser'
  194. ],
  195. extensions: [
  196. '.web.js',
  197. '.web.ts',
  198. // Typescript:
  199. '.tsx',
  200. '.ts',
  201. // Webpack defaults:
  202. '.js',
  203. '.json'
  204. ],
  205. fallback: {
  206. // Provide some empty Node modules (required by AtlasKit, olm).
  207. crypto: false,
  208. fs: false,
  209. path: false,
  210. process: false
  211. }
  212. }
  213. };
  214. }
  215. /**
  216. * Helper function to build the dev server config. It's necessary to split it in
  217. * Webpack 5 because only one devServer entry is supported, so we attach it to
  218. * the main bundle.
  219. *
  220. * @returns {Object} the dev server configuration.
  221. */
  222. function getDevServerConfig() {
  223. return {
  224. client: {
  225. overlay: {
  226. errors: true,
  227. warnings: false
  228. }
  229. },
  230. host: '127.0.0.1',
  231. hot: true,
  232. proxy: {
  233. '/': {
  234. bypass: devServerProxyBypass,
  235. secure: false,
  236. target: devServerProxyTarget,
  237. headers: {
  238. 'Host': new URL(devServerProxyTarget).host
  239. }
  240. }
  241. },
  242. server: 'https',
  243. static: {
  244. directory: process.cwd()
  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. minimize: isProduction
  255. };
  256. const config = getConfig(configOptions);
  257. const perfHintOptions = {
  258. analyzeBundle,
  259. minimize: isProduction
  260. };
  261. return [
  262. Object.assign({}, 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.IgnorePlugin({
  271. resourceRegExp: /^canvas$/,
  272. contextRegExp: /resemblejs$/
  273. }),
  274. new webpack.IgnorePlugin({
  275. resourceRegExp: /^\.\/locale$/,
  276. contextRegExp: /moment$/
  277. }),
  278. new webpack.ProvidePlugin({
  279. process: 'process/browser'
  280. })
  281. ],
  282. performance: getPerformanceHints(perfHintOptions, 4 * 1024 * 1024)
  283. }),
  284. Object.assign({}, config, {
  285. entry: {
  286. 'alwaysontop': './react/features/always-on-top/index.js'
  287. },
  288. plugins: [
  289. ...config.plugins,
  290. ...getBundleAnalyzerPlugin(analyzeBundle, 'alwaysontop')
  291. ],
  292. performance: getPerformanceHints(perfHintOptions, 800 * 1024)
  293. }),
  294. Object.assign({}, config, {
  295. entry: {
  296. 'dial_in_info_bundle': './react/features/invite/components/dial-in-info-page'
  297. },
  298. plugins: [
  299. ...config.plugins,
  300. ...getBundleAnalyzerPlugin(analyzeBundle, 'dial_in_info'),
  301. new webpack.IgnorePlugin({
  302. resourceRegExp: /^\.\/locale$/,
  303. contextRegExp: /moment$/
  304. })
  305. ],
  306. performance: getPerformanceHints(perfHintOptions, 500 * 1024)
  307. }),
  308. Object.assign({}, config, {
  309. entry: {
  310. 'do_external_connect': './connection_optimization/do_external_connect.js'
  311. },
  312. plugins: [
  313. ...config.plugins,
  314. ...getBundleAnalyzerPlugin(analyzeBundle, 'do_external_connect')
  315. ],
  316. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  317. }),
  318. Object.assign({}, config, {
  319. entry: {
  320. 'analytics-ga': './react/features/analytics/handlers/GoogleAnalyticsHandler.ts'
  321. },
  322. plugins: [
  323. ...config.plugins,
  324. ...getBundleAnalyzerPlugin(analyzeBundle, 'analytics-ga')
  325. ],
  326. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  327. }),
  328. Object.assign({}, config, {
  329. entry: {
  330. 'close3': './static/close3.js'
  331. },
  332. plugins: [
  333. ...config.plugins,
  334. ...getBundleAnalyzerPlugin(analyzeBundle, 'close3')
  335. ],
  336. performance: getPerformanceHints(perfHintOptions, 128 * 1024)
  337. }),
  338. Object.assign({}, config, {
  339. entry: {
  340. 'external_api': './modules/API/external/index.js'
  341. },
  342. output: Object.assign({}, config.output, {
  343. library: 'JitsiMeetExternalAPI',
  344. libraryTarget: 'umd'
  345. }),
  346. plugins: [
  347. ...config.plugins,
  348. ...getBundleAnalyzerPlugin(analyzeBundle, 'external_api')
  349. ],
  350. performance: getPerformanceHints(perfHintOptions, 35 * 1024)
  351. }),
  352. Object.assign({}, config, {
  353. entry: {
  354. 'face-landmarks-worker': './react/features/face-landmarks/faceLandmarksWorker.ts'
  355. },
  356. plugins: [
  357. ...config.plugins,
  358. ...getBundleAnalyzerPlugin(analyzeBundle, 'face-landmarks-worker')
  359. ],
  360. performance: getPerformanceHints(perfHintOptions, 1024 * 1024 * 2)
  361. }),
  362. Object.assign({}, config, {
  363. /**
  364. * The NoiseSuppressorWorklet is loaded in an audio worklet which doesn't have the same
  365. * context as a normal window, (e.g. self/window is not defined).
  366. * While running a production build webpack's boilerplate code doesn't introduce any
  367. * audio worklet "unfriendly" code however when running the dev server, hot module replacement
  368. * and live reload add javascript code that can't be ran by the worklet, so we explicitly ignore
  369. * those parts with the null-loader.
  370. * The dev server also expects a `self` global object that's not available in the `AudioWorkletGlobalScope`,
  371. * so we replace it.
  372. */
  373. entry: {
  374. 'noise-suppressor-worklet':
  375. './react/features/stream-effects/noise-suppression/NoiseSuppressorWorklet.ts'
  376. },
  377. module: { rules: [
  378. ...config.module.rules,
  379. {
  380. test: resolve(__dirname, 'node_modules/webpack-dev-server/client'),
  381. loader: 'null-loader'
  382. }
  383. ] },
  384. plugins: [
  385. ],
  386. performance: getPerformanceHints(perfHintOptions, 200 * 1024),
  387. output: {
  388. ...config.output,
  389. globalObject: 'AudioWorkletGlobalScope'
  390. }
  391. })
  392. ];
  393. };