webpack.config.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. const path = require('path')
  2. const glob = require('glob')
  3. const webpack = require('webpack')
  4. const CopyPlugin = require('copy-webpack-plugin')
  5. const WebpackAssetsManifest = require('webpack-assets-manifest')
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  7. const {
  8. LezerGrammarCompilerPlugin,
  9. } = require('./webpack-plugins/lezer-grammar-compiler')
  10. const PackageVersions = require('./app/src/infrastructure/PackageVersions.js')
  11. const invalidateBabelCacheIfNeeded = require('./frontend/macros/invalidate-babel-cache-if-needed')
  12. const { dirname } = require('node:path')
  13. // Make sure that babel-macros are re-evaluated after changing the modules config
  14. invalidateBabelCacheIfNeeded()
  15. // Generate a hash of entry points, including modules
  16. const entryPoints = {
  17. bootstrap: './frontend/js/bootstrap.ts',
  18. devToolbar: './frontend/js/dev-toolbar.ts',
  19. 'ide-detached': './frontend/js/ide-detached.ts',
  20. marketing: './frontend/js/marketing.ts',
  21. 'main-style': './frontend/stylesheets/main-style.scss',
  22. tracking: './frontend/js/infrastructure/tracking.ts',
  23. }
  24. // Add entrypoints for each "page"
  25. glob
  26. .sync(
  27. path.join(__dirname, 'modules/*/frontend/js/pages/**/*.{js,jsx,ts,tsx}')
  28. )
  29. .forEach(page => {
  30. // in: /workspace/services/web/modules/foo/frontend/js/pages/bar.js
  31. // out: modules/foo/pages/bar
  32. const name = path
  33. .relative(__dirname, page)
  34. .replace(/frontend[/]js[/]/, '')
  35. .replace(/.(js|jsx|ts|tsx)$/, '')
  36. entryPoints[name] = './' + path.relative(__dirname, page)
  37. })
  38. glob
  39. .sync(path.join(__dirname, 'frontend/js/pages/**/*.{js,jsx,ts,tsx}'))
  40. .forEach(page => {
  41. // in: /workspace/services/web/frontend/js/pages/marketing/homepage.ts
  42. // out: pages/marketing/homepage
  43. const name = path
  44. .relative(path.join(__dirname, 'frontend/js/'), page)
  45. .replace(/.(js|jsx|ts|tsx)$/, '')
  46. entryPoints[name] = './' + path.relative(__dirname, page)
  47. })
  48. function getModuleDirectory(moduleName) {
  49. const entrypointPath = require.resolve(moduleName)
  50. const suffix = `node_modules/${moduleName}`
  51. const idx = entrypointPath.indexOf(suffix)
  52. if (idx === -1) {
  53. throw new Error(`could not find Node module: ${moduleName}`)
  54. }
  55. return entrypointPath.slice(0, idx + suffix.length)
  56. }
  57. const mathjaxDir = getModuleDirectory('mathjax')
  58. const pdfjsDir = getModuleDirectory('pdfjs-dist')
  59. const dictionariesDir = getModuleDirectory('@overleaf/dictionaries')
  60. const vendorDir = path.join(__dirname, 'frontend/js/vendor')
  61. const MATHJAX_VERSION = require('mathjax/package.json').version
  62. if (MATHJAX_VERSION !== PackageVersions.version.mathjax) {
  63. throw new Error(
  64. '"mathjax" version de-synced, update services/web/app/src/infrastructure/PackageVersions.js'
  65. )
  66. }
  67. const DICTIONARIES_VERSION =
  68. require('@overleaf/dictionaries/package.json').version
  69. if (DICTIONARIES_VERSION !== PackageVersions.version.dictionaries) {
  70. throw new Error(
  71. '"@overleaf/dictionaries" version de-synced, update services/web/app/src/infrastructure/PackageVersions.js'
  72. )
  73. }
  74. module.exports = {
  75. // Defines the "entry point(s)" for the application - i.e. the file which
  76. // bootstraps the application
  77. entry: entryPoints,
  78. // Define where and how the bundle will be output to disk
  79. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  80. // kept in memory for speed
  81. output: {
  82. path: path.join(__dirname, 'public'),
  83. publicPath: '/',
  84. workerPublicPath: '/',
  85. // By default write into js directory
  86. filename: 'js/[name]-[contenthash].js',
  87. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  88. // style code bundles
  89. libraryTarget: 'umd',
  90. // Name the exported variable from output bundle
  91. library: ['Frontend', '[name]'],
  92. },
  93. optimization: {
  94. // https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkschunks
  95. splitChunks: {
  96. chunks: 'all', // allow non-async chunks to be analysed for shared modules
  97. },
  98. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  99. runtimeChunk: {
  100. name: 'runtime',
  101. },
  102. },
  103. // Define how file types are handled by webpack
  104. module: {
  105. rules: [
  106. {
  107. test: /\.tsx?$/,
  108. include: path.resolve(
  109. __dirname,
  110. 'modules/writefull/frontend/js/integration'
  111. ),
  112. use: [
  113. {
  114. loader: 'ts-loader',
  115. options: {
  116. configFile: path.resolve(
  117. __dirname,
  118. 'modules/writefull/frontend/js/integration/tsconfig.json'
  119. ),
  120. transpileOnly: true,
  121. },
  122. },
  123. ],
  124. },
  125. {
  126. // Pass application JS/TS files through babel-loader,
  127. // transpiling to targets defined in browserslist
  128. test: /\.([jt]sx?|[cm]js)$/,
  129. // Only compile application files and specific dependencies
  130. // (other npm and vendored dependencies must be in ES5 already)
  131. exclude: [
  132. /node_modules\/(?!(react-dnd|chart\.js|@uppy|pdfjs-dist|react-resizable-panels)\/)/,
  133. vendorDir,
  134. path.resolve(__dirname, 'modules/writefull/frontend/js/integration'),
  135. ],
  136. use: [
  137. {
  138. loader: 'babel-loader',
  139. options: {
  140. // Configure babel-loader to cache compiled output so that
  141. // subsequent compile runs are much faster
  142. cacheDirectory: true,
  143. configFile: path.join(__dirname, './babel.config.json'),
  144. plugins: [
  145. process.env.REACT_REFRESH_ENABLED === 'true' &&
  146. 'react-refresh/babel',
  147. ].filter(Boolean),
  148. },
  149. },
  150. ],
  151. type: 'javascript/auto',
  152. },
  153. {
  154. test: /\.wasm$/,
  155. type: 'asset/resource',
  156. generator: {
  157. filename: 'js/[name]-[contenthash][ext]',
  158. },
  159. },
  160. {
  161. test: /\.txt$/,
  162. type: 'asset/source',
  163. generator: {
  164. filename: 'js/[name]-[contenthash][ext]',
  165. },
  166. },
  167. {
  168. // Pass Sass files through sass-loader/css-loader/mini-css-extract-
  169. // plugin (note: run in reverse order)
  170. test: /\.s[ac]ss$/,
  171. use: [
  172. // Allows the CSS to be extracted to a separate .css file
  173. { loader: MiniCssExtractPlugin.loader },
  174. // Resolves any CSS dependencies (e.g. url())
  175. { loader: 'css-loader' },
  176. // Resolve relative paths sensibly in SASS
  177. { loader: 'resolve-url-loader' },
  178. {
  179. // Runs autoprefixer on CSS via postcss
  180. loader: 'postcss-loader',
  181. options: {
  182. postcssOptions: {
  183. plugins: ['autoprefixer'],
  184. },
  185. },
  186. },
  187. // Compile Sass off the main event loop
  188. {
  189. loader: 'thread-loader',
  190. options: {
  191. // keep workers alive for dev-server, and shut them down when not needed
  192. poolTimeout:
  193. process.env.NODE_ENV === 'development' ? 10 * 60 * 1000 : 500,
  194. // bring up more workers after they timed out
  195. poolRespawn: true,
  196. // limit concurrency (one per entrypoint and let the small includes queue up)
  197. workers: 6,
  198. },
  199. },
  200. // Compiles Sass to CSS
  201. {
  202. loader: 'sass-loader',
  203. options: { sourceMap: true }, // sourceMap: true is required for resolve-url-loader
  204. },
  205. ],
  206. },
  207. {
  208. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  209. test: /\.css$/i,
  210. oneOf: [
  211. {
  212. // Import as a string (for Shadow DOM usage): import styles from './file.css?inline'
  213. resourceQuery: /inline/,
  214. use: [
  215. {
  216. loader: 'css-loader',
  217. },
  218. {
  219. loader: 'postcss-loader',
  220. },
  221. ],
  222. },
  223. {
  224. // CSS from writefull module - inject directly into DOM
  225. include: path.resolve(__dirname, 'modules/writefull/'),
  226. use: [
  227. 'style-loader',
  228. {
  229. loader: 'css-loader',
  230. options: {
  231. importLoaders: 1,
  232. },
  233. },
  234. {
  235. loader: 'postcss-loader',
  236. options: {
  237. postcssOptions: {
  238. config: path.resolve(
  239. __dirname,
  240. 'modules/writefull/frontend/js/integration/postcss.config.js'
  241. ),
  242. },
  243. },
  244. },
  245. ],
  246. },
  247. {
  248. // CSS from AI module
  249. include: dirname(require.resolve('@overleaf/ai')),
  250. use: [
  251. {
  252. loader: 'css-loader',
  253. options: {
  254. exportType: 'css-style-sheet',
  255. },
  256. },
  257. {
  258. loader: 'postcss-loader',
  259. options: {
  260. postcssOptions: {
  261. config: require.resolve('@overleaf/ai/postcss.config.js'),
  262. },
  263. },
  264. },
  265. ],
  266. },
  267. {
  268. // Standard CSS processing (extracted into separate file)
  269. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  270. },
  271. ],
  272. },
  273. {
  274. // Load fonts
  275. test: /\.(woff2?|ttf|otf)$/,
  276. type: 'asset/resource',
  277. generator: {
  278. filename: 'fonts/[name]-[contenthash][ext]',
  279. },
  280. },
  281. {
  282. // Load images and videos (static files)
  283. test: /\.(svg|gif|png|jpg|pdf|mp4)$/,
  284. type: 'asset/resource',
  285. generator: {
  286. filename: 'images/[name]-[contenthash][ext]',
  287. },
  288. },
  289. {
  290. // These options are necessary for handlebars to have access to helper
  291. // methods
  292. test: /\.handlebars$/,
  293. loader: 'handlebars-loader',
  294. options: {
  295. compat: true,
  296. knownHelpersOnly: false,
  297. runtimePath: 'handlebars/runtime',
  298. },
  299. },
  300. {
  301. // Load translations files with custom loader, to extract and apply
  302. // fallbacks
  303. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  304. use: [
  305. {
  306. loader: path.join(__dirname, 'frontend/translations-loader.js'),
  307. },
  308. ],
  309. },
  310. ],
  311. },
  312. resolve: {
  313. alias: {
  314. // custom prefixes for import paths
  315. '@': path.resolve(__dirname, './frontend/js/'),
  316. '@modules': path.resolve(__dirname, './modules/'),
  317. '@ol-types': path.resolve(__dirname, './types/'),
  318. '@wf': path.resolve(
  319. __dirname,
  320. './modules/writefull/frontend/js/integration/src/'
  321. ),
  322. },
  323. // symlinks: false, // enable this while using `npm link`
  324. extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.json'],
  325. fallback: {
  326. events: require.resolve('events'),
  327. // for react-dnd + React 17
  328. 'react/jsx-runtime': 'react/jsx-runtime.js',
  329. 'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
  330. },
  331. },
  332. experiments: {
  333. asyncWebAssembly: true,
  334. },
  335. plugins: [
  336. new LezerGrammarCompilerPlugin(),
  337. // Generate a manifest.json file which is used by the backend to map the
  338. // base filenames to the generated output filenames
  339. new WebpackAssetsManifest({
  340. entrypoints: true,
  341. publicPath: true,
  342. output: 'manifest.json',
  343. }),
  344. new webpack.EnvironmentPlugin({
  345. // Ensure that process.env.RESET_APP_DATA_TIMER is defined, to avoid an error.
  346. // https://github.com/algolia/algoliasearch-client-javascript/issues/756
  347. RESET_APP_DATA_TIMER: '120000',
  348. // Ensure that process.env.CYPRESS is defined (see utils/worker.js)
  349. CYPRESS: false,
  350. }),
  351. // Prevent moment from loading (very large) locale files that aren't used
  352. new webpack.IgnorePlugin({
  353. resourceRegExp: /^\.\/locale$/,
  354. contextRegExp: /moment$/,
  355. }),
  356. // Set window.$ and window.jQuery
  357. new webpack.ProvidePlugin({
  358. $: 'jquery',
  359. jQuery: 'jquery',
  360. }),
  361. new CopyPlugin({
  362. patterns: [
  363. // Copy the required files for loading MathJax from MathJax NPM package
  364. // https://www.npmjs.com/package/mathjax#user-content-hosting-your-own-copy-of-the-mathjax-components
  365. {
  366. from: 'es5/tex-svg-full.js',
  367. to: `js/libs/mathjax-${PackageVersions.version.mathjax}/es5`,
  368. toType: 'dir',
  369. context: mathjaxDir,
  370. },
  371. {
  372. from: 'es5/input/tex/extensions/**/*.js',
  373. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  374. toType: 'dir',
  375. context: mathjaxDir,
  376. },
  377. {
  378. from: 'es5/ui/**/*',
  379. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  380. toType: 'dir',
  381. context: mathjaxDir,
  382. },
  383. {
  384. from: 'es5/a11y/**/*',
  385. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  386. toType: 'dir',
  387. context: mathjaxDir,
  388. },
  389. {
  390. from: 'es5/input/mml.js',
  391. to: `js/libs/mathjax-${PackageVersions.version.mathjax}/es5/input`,
  392. toType: 'dir',
  393. context: mathjaxDir,
  394. },
  395. {
  396. from: 'es5/sre/**/*',
  397. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  398. toType: 'dir',
  399. context: mathjaxDir,
  400. },
  401. {
  402. from: '*',
  403. to: `js/dictionaries/${PackageVersions.version.dictionaries}`,
  404. toType: 'dir',
  405. context: `${dictionariesDir}/dictionaries`,
  406. },
  407. // Copy CMap files (used to provide support for non-Latin characters),
  408. // wasm, ICC profiles, fonts and images from pdfjs-dist package to build output.
  409. {
  410. from: 'cmaps',
  411. to: 'js/pdfjs-dist/cmaps',
  412. context: pdfjsDir,
  413. },
  414. {
  415. from: 'iccs',
  416. to: 'js/pdfjs-dist/iccs',
  417. context: pdfjsDir,
  418. },
  419. {
  420. from: 'wasm',
  421. to: 'js/pdfjs-dist/wasm',
  422. context: pdfjsDir,
  423. },
  424. {
  425. from: 'standard_fonts',
  426. to: 'fonts/pdfjs-dist',
  427. context: pdfjsDir,
  428. },
  429. {
  430. from: 'legacy/web/images',
  431. to: 'images/pdfjs-dist',
  432. context: pdfjsDir,
  433. },
  434. ],
  435. }),
  436. ],
  437. }