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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. 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.isProduction - whether this is a production build or not.
  83. * @returns {Object} the base config object.
  84. */
  85. function getConfig(options = {}) {
  86. const { detectCircularDeps, isProduction } = options;
  87. return {
  88. devtool: isProduction ? 'source-map' : 'eval-source-map',
  89. mode: isProduction ? '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. presets: [
  99. [
  100. require.resolve('@babel/preset-env'),
  101. // Tell babel to avoid compiling imports into CommonJS
  102. // so that webpack may do tree shaking.
  103. {
  104. modules: false,
  105. // Specify our target browsers so no transpiling is
  106. // done unnecessarily. For browsers not specified
  107. // here, the ES2015+ profile will be used.
  108. targets: {
  109. chrome: 80,
  110. electron: 10,
  111. firefox: 68,
  112. safari: 14
  113. }
  114. }
  115. ],
  116. require.resolve('@babel/preset-react')
  117. ]
  118. },
  119. test: /\.jsx?$/,
  120. exclude: /node_modules/
  121. }, {
  122. // Allow CSS to be imported into JavaScript.
  123. test: /\.css$/,
  124. use: [
  125. 'style-loader',
  126. 'css-loader'
  127. ]
  128. }, {
  129. test: /\.svg$/,
  130. use: [ {
  131. loader: '@svgr/webpack',
  132. options: {
  133. dimensions: false,
  134. expandProps: 'start'
  135. }
  136. } ]
  137. }, {
  138. test: /\.tsx?$/,
  139. exclude: /node_modules/,
  140. loader: 'ts-loader',
  141. options: {
  142. configFile: 'tsconfig.web.json',
  143. transpileOnly: !isProduction // Skip type checking for dev builds.,
  144. }
  145. } ]
  146. },
  147. node: {
  148. // Allow the use of the real filename of the module being executed. By
  149. // default Webpack does not leak path-related information and provides a
  150. // value that is a mock (/index.js).
  151. __filename: true
  152. },
  153. optimization: {
  154. concatenateModules: isProduction,
  155. minimize: isProduction
  156. },
  157. output: {
  158. filename: `[name]${isProduction ? '.min' : ''}.js`,
  159. path: `${__dirname}/build`,
  160. publicPath: '/libs/',
  161. sourceMapFilename: '[file].map'
  162. },
  163. plugins: [
  164. detectCircularDeps
  165. && new CircularDependencyPlugin({
  166. allowAsyncCycles: false,
  167. exclude: /node_modules/,
  168. failOnError: false
  169. })
  170. ].filter(Boolean),
  171. resolve: {
  172. alias: {
  173. 'focus-visible': 'focus-visible/dist/focus-visible.min.js'
  174. },
  175. aliasFields: [
  176. 'browser'
  177. ],
  178. extensions: [
  179. '.web.js',
  180. '.web.ts',
  181. '.web.tsx',
  182. // Typescript:
  183. '.tsx',
  184. '.ts',
  185. // Webpack defaults:
  186. '.js',
  187. '.json'
  188. ],
  189. fallback: {
  190. // Provide some empty Node modules (required by AtlasKit, olm).
  191. crypto: false,
  192. fs: false,
  193. path: false,
  194. process: false
  195. }
  196. }
  197. };
  198. }
  199. /**
  200. * Helper function to build the dev server config. It's necessary to split it in
  201. * Webpack 5 because only one devServer entry is supported, so we attach it to
  202. * the main bundle.
  203. *
  204. * @returns {Object} the dev server configuration.
  205. */
  206. function getDevServerConfig() {
  207. return {
  208. client: {
  209. overlay: {
  210. errors: true,
  211. warnings: false
  212. }
  213. },
  214. host: '127.0.0.1',
  215. hot: true,
  216. proxy: [
  217. {
  218. context: [ '/' ],
  219. bypass: devServerProxyBypass,
  220. secure: false,
  221. target: devServerProxyTarget,
  222. headers: {
  223. 'Host': new URL(devServerProxyTarget).host
  224. }
  225. }
  226. ],
  227. server: process.env.CODESPACES ? 'http' : 'https',
  228. static: {
  229. directory: process.cwd(),
  230. watch: {
  231. ignored: file => file.endsWith('.log')
  232. }
  233. }
  234. };
  235. }
  236. module.exports = (_env, argv) => {
  237. const analyzeBundle = Boolean(process.env.ANALYZE_BUNDLE);
  238. const mode = typeof argv.mode === 'undefined' ? 'production' : argv.mode;
  239. const isProduction = mode === 'production';
  240. const configOptions = {
  241. detectCircularDeps: Boolean(process.env.DETECT_CIRCULAR_DEPS),
  242. isProduction
  243. };
  244. const config = getConfig(configOptions);
  245. const perfHintOptions = {
  246. analyzeBundle,
  247. isProduction
  248. };
  249. return [
  250. Object.assign({}, config, {
  251. entry: {
  252. 'app.bundle': './app.js'
  253. },
  254. devServer: isProduction ? {} : getDevServerConfig(),
  255. plugins: [
  256. ...config.plugins,
  257. ...getBundleAnalyzerPlugin(analyzeBundle, 'app'),
  258. new webpack.IgnorePlugin({
  259. resourceRegExp: /^canvas$/,
  260. contextRegExp: /resemblejs$/
  261. }),
  262. new webpack.IgnorePlugin({
  263. resourceRegExp: /^\.\/locale$/,
  264. contextRegExp: /moment$/
  265. }),
  266. new webpack.ProvidePlugin({
  267. process: 'process/browser'
  268. })
  269. ],
  270. performance: getPerformanceHints(perfHintOptions, 5 * 1024 * 1024)
  271. }),
  272. Object.assign({}, config, {
  273. entry: {
  274. 'alwaysontop': './react/features/always-on-top/index.tsx'
  275. },
  276. plugins: [
  277. ...config.plugins,
  278. ...getBundleAnalyzerPlugin(analyzeBundle, 'alwaysontop')
  279. ],
  280. performance: getPerformanceHints(perfHintOptions, 800 * 1024)
  281. }),
  282. Object.assign({}, config, {
  283. entry: {
  284. 'analytics-ga': './react/features/analytics/handlers/GoogleAnalyticsHandler.ts'
  285. },
  286. plugins: [
  287. ...config.plugins,
  288. ...getBundleAnalyzerPlugin(analyzeBundle, 'analytics-ga')
  289. ],
  290. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  291. }),
  292. Object.assign({}, config, {
  293. entry: {
  294. 'close3': './static/close3.js'
  295. },
  296. plugins: [
  297. ...config.plugins,
  298. ...getBundleAnalyzerPlugin(analyzeBundle, 'close3')
  299. ],
  300. performance: getPerformanceHints(perfHintOptions, 128 * 1024)
  301. }),
  302. Object.assign({}, config, {
  303. entry: {
  304. 'external_api': './modules/API/external/index.js'
  305. },
  306. output: Object.assign({}, config.output, {
  307. library: 'JitsiMeetExternalAPI',
  308. libraryTarget: 'umd'
  309. }),
  310. plugins: [
  311. ...config.plugins,
  312. ...getBundleAnalyzerPlugin(analyzeBundle, 'external_api')
  313. ],
  314. performance: getPerformanceHints(perfHintOptions, 40 * 1024)
  315. }),
  316. Object.assign({}, 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. }),
  326. Object.assign({}, config, {
  327. /**
  328. * The NoiseSuppressorWorklet is loaded in an audio worklet which doesn't have the same
  329. * context as a normal window, (e.g. self/window is not defined).
  330. * While running a production build webpack's boilerplate code doesn't introduce any
  331. * audio worklet "unfriendly" code however when running the dev server, hot module replacement
  332. * and live reload add javascript code that can't be ran by the worklet, so we explicitly ignore
  333. * those parts with the null-loader.
  334. * The dev server also expects a `self` global object that's not available in the `AudioWorkletGlobalScope`,
  335. * so we replace it.
  336. */
  337. entry: {
  338. 'noise-suppressor-worklet':
  339. './react/features/stream-effects/noise-suppression/NoiseSuppressorWorklet.ts'
  340. },
  341. module: { rules: [
  342. ...config.module.rules,
  343. {
  344. test: resolve(__dirname, 'node_modules/webpack-dev-server/client'),
  345. loader: 'null-loader'
  346. }
  347. ] },
  348. plugins: [
  349. ],
  350. performance: getPerformanceHints(perfHintOptions, 1024 * 1024 * 2),
  351. output: {
  352. ...config.output,
  353. globalObject: 'AudioWorkletGlobalScope'
  354. }
  355. }),
  356. Object.assign({}, config, {
  357. entry: {
  358. 'screenshot-capture-worker': './react/features/screenshot-capture/worker.ts'
  359. },
  360. plugins: [
  361. ...config.plugins,
  362. ...getBundleAnalyzerPlugin(analyzeBundle, 'screenshot-capture-worker')
  363. ],
  364. performance: getPerformanceHints(perfHintOptions, 4 * 1024)
  365. })
  366. ];
  367. };