Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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