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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* global __dirname */
  2. const CircularDependencyPlugin = require('circular-dependency-plugin');
  3. const fs = require('fs');
  4. const { join } = 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: '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 bellow 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-transform-flow-strip-types'),
  103. require.resolve('@babel/plugin-proposal-class-properties'),
  104. require.resolve('@babel/plugin-proposal-export-default-from'),
  105. require.resolve('@babel/plugin-proposal-export-namespace-from'),
  106. require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
  107. require.resolve('@babel/plugin-proposal-optional-chaining')
  108. ],
  109. presets: [
  110. [
  111. require.resolve('@babel/preset-env'),
  112. // Tell babel to avoid compiling imports into CommonJS
  113. // so that webpack may do tree shaking.
  114. {
  115. modules: false,
  116. // Specify our target browsers so no transpiling is
  117. // done unnecessarily. For browsers not specified
  118. // here, the ES2015+ profile will be used.
  119. targets: {
  120. chrome: 58,
  121. electron: 2,
  122. firefox: 54,
  123. safari: 11
  124. }
  125. }
  126. ],
  127. require.resolve('@babel/preset-flow'),
  128. require.resolve('@babel/preset-react')
  129. ]
  130. },
  131. test: /\.jsx?$/
  132. }, {
  133. // TODO: get rid of this.
  134. // Expose jquery as the globals $ and jQuery because it is expected
  135. // to be available in such a form by lib-jitsi-meet.
  136. loader: 'expose-loader',
  137. options: {
  138. exposes: [ '$', 'jQuery' ]
  139. },
  140. test: require.resolve('jquery')
  141. }, {
  142. // Allow CSS to be imported into JavaScript.
  143. test: /\.css$/,
  144. use: [
  145. 'style-loader',
  146. 'css-loader'
  147. ]
  148. }, {
  149. test: /\/node_modules\/@atlaskit\/modal-dialog\/.*\.js$/,
  150. resolve: {
  151. alias: {
  152. 'react-focus-lock': `${__dirname}/react/features/base/util/react-focus-lock-wrapper.js`,
  153. '../styled/Modal': `${__dirname}/react/features/base/dialog/components/web/ThemedDialog.js`
  154. }
  155. }
  156. }, {
  157. test: /\/react\/features\/base\/util\/react-focus-lock-wrapper.js$/,
  158. resolve: {
  159. alias: {
  160. 'react-focus-lock': `${__dirname}/node_modules/react-focus-lock`
  161. }
  162. }
  163. }, {
  164. test: /\.svg$/,
  165. use: [ {
  166. loader: '@svgr/webpack',
  167. options: {
  168. dimensions: false,
  169. expandProps: 'start'
  170. }
  171. } ]
  172. } ]
  173. },
  174. node: {
  175. // Allow the use of the real filename of the module being executed. By
  176. // default Webpack does not leak path-related information and provides a
  177. // value that is a mock (/index.js).
  178. __filename: true
  179. },
  180. optimization: {
  181. concatenateModules: minimize,
  182. minimize
  183. },
  184. output: {
  185. filename: `[name]${minimize ? '.min' : ''}.js`,
  186. path: `${__dirname}/build`,
  187. publicPath: '/libs/',
  188. sourceMapFilename: `[name].${minimize ? 'min' : 'js'}.map`
  189. },
  190. plugins: [
  191. detectCircularDeps
  192. && new CircularDependencyPlugin({
  193. allowAsyncCycles: false,
  194. exclude: /node_modules/,
  195. failOnError: false
  196. })
  197. ].filter(Boolean),
  198. resolve: {
  199. alias: {
  200. 'focus-visible': 'focus-visible/dist/focus-visible.min.js'
  201. },
  202. aliasFields: [
  203. 'browser'
  204. ],
  205. extensions: [
  206. '.web.js',
  207. // Webpack defaults:
  208. '.js',
  209. '.json'
  210. ],
  211. fallback: {
  212. // Provide some empty Node modules (required by AtlasKit, olm).
  213. crypto: false,
  214. fs: false,
  215. path: false,
  216. process: false
  217. }
  218. }
  219. };
  220. }
  221. /**
  222. * Helper function to build the dev server config. It's necessary to split it in
  223. * Webpack 5 because only one devServer entry is supported, so we attach it to
  224. * the main bundle.
  225. *
  226. * @returns {Object} the dev server configuration.
  227. */
  228. function getDevServerConfig() {
  229. return {
  230. client: {
  231. overlay: {
  232. errors: true,
  233. warnings: false
  234. }
  235. },
  236. https: true,
  237. host: '127.0.0.1',
  238. proxy: {
  239. '/': {
  240. bypass: devServerProxyBypass,
  241. secure: false,
  242. target: devServerProxyTarget,
  243. headers: {
  244. 'Host': new URL(devServerProxyTarget).host
  245. }
  246. }
  247. },
  248. static: {
  249. directory: process.cwd()
  250. }
  251. };
  252. }
  253. module.exports = (_env, argv) => {
  254. const analyzeBundle = Boolean(process.env.ANALYZE_BUNDLE);
  255. const mode = typeof argv.mode === 'undefined' ? 'production' : argv.mode;
  256. const isProduction = mode === 'production';
  257. const configOptions = {
  258. detectCircularDeps: Boolean(process.env.DETECT_CIRCULAR_DEPS) || !isProduction,
  259. minimize: isProduction
  260. };
  261. const config = getConfig(configOptions);
  262. const perfHintOptions = {
  263. analyzeBundle,
  264. minimize: isProduction
  265. };
  266. return [
  267. Object.assign({}, config, {
  268. entry: {
  269. 'app.bundle': './app.js'
  270. },
  271. devServer: isProduction ? {} : getDevServerConfig(),
  272. plugins: [
  273. ...config.plugins,
  274. ...getBundleAnalyzerPlugin(analyzeBundle, 'app'),
  275. new webpack.IgnorePlugin({
  276. resourceRegExp: /^canvas$/,
  277. contextRegExp: /resemblejs$/
  278. }),
  279. new webpack.IgnorePlugin({
  280. resourceRegExp: /^\.\/locale$/,
  281. contextRegExp: /moment$/
  282. }),
  283. new webpack.ProvidePlugin({
  284. process: 'process/browser'
  285. })
  286. ],
  287. performance: getPerformanceHints(perfHintOptions, 4 * 1024 * 1024)
  288. }),
  289. Object.assign({}, config, {
  290. entry: {
  291. 'alwaysontop': './react/features/always-on-top/index.js'
  292. },
  293. plugins: [
  294. ...config.plugins,
  295. ...getBundleAnalyzerPlugin(analyzeBundle, 'alwaysontop')
  296. ],
  297. performance: getPerformanceHints(perfHintOptions, 800 * 1024)
  298. }),
  299. Object.assign({}, config, {
  300. entry: {
  301. 'dial_in_info_bundle': './react/features/invite/components/dial-in-info-page'
  302. },
  303. plugins: [
  304. ...config.plugins,
  305. ...getBundleAnalyzerPlugin(analyzeBundle, 'dial_in_info'),
  306. new webpack.IgnorePlugin({
  307. resourceRegExp: /^\.\/locale$/,
  308. contextRegExp: /moment$/
  309. })
  310. ],
  311. performance: getPerformanceHints(perfHintOptions, 500 * 1024)
  312. }),
  313. Object.assign({}, config, {
  314. entry: {
  315. 'do_external_connect': './connection_optimization/do_external_connect.js'
  316. },
  317. plugins: [
  318. ...config.plugins,
  319. ...getBundleAnalyzerPlugin(analyzeBundle, 'do_external_connect')
  320. ],
  321. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  322. }),
  323. Object.assign({}, config, {
  324. entry: {
  325. 'flacEncodeWorker': './react/features/local-recording/recording/flac/flacEncodeWorker.js'
  326. },
  327. plugins: [
  328. ...config.plugins,
  329. ...getBundleAnalyzerPlugin(analyzeBundle, 'flacEncodeWorker')
  330. ],
  331. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  332. }),
  333. Object.assign({}, config, {
  334. entry: {
  335. 'analytics-ga': './react/features/analytics/handlers/GoogleAnalyticsHandler.js'
  336. },
  337. plugins: [
  338. ...config.plugins,
  339. ...getBundleAnalyzerPlugin(analyzeBundle, 'analytics-ga')
  340. ],
  341. performance: getPerformanceHints(perfHintOptions, 5 * 1024)
  342. }),
  343. Object.assign({}, config, {
  344. entry: {
  345. 'close3': './static/close3.js'
  346. },
  347. plugins: [
  348. ...config.plugins,
  349. ...getBundleAnalyzerPlugin(analyzeBundle, 'close3')
  350. ],
  351. performance: getPerformanceHints(perfHintOptions, 128 * 1024)
  352. }),
  353. Object.assign({}, config, {
  354. entry: {
  355. 'external_api': './modules/API/external/index.js'
  356. },
  357. output: Object.assign({}, config.output, {
  358. library: 'JitsiMeetExternalAPI',
  359. libraryTarget: 'umd'
  360. }),
  361. plugins: [
  362. ...config.plugins,
  363. ...getBundleAnalyzerPlugin(analyzeBundle, 'external_api')
  364. ],
  365. performance: getPerformanceHints(perfHintOptions, 35 * 1024)
  366. })
  367. ];
  368. };