webpack.config.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. },
  91. optimization: {
  92. // https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkschunks
  93. splitChunks: {
  94. chunks: 'all', // allow non-async chunks to be analysed for shared modules
  95. },
  96. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  97. runtimeChunk: {
  98. name: 'runtime',
  99. },
  100. },
  101. // Define how file types are handled by webpack
  102. module: {
  103. rules: [
  104. {
  105. test: /\.tsx?$/,
  106. include: path.resolve(
  107. __dirname,
  108. 'modules/writefull/frontend/js/integration'
  109. ),
  110. use: [
  111. {
  112. loader: 'ts-loader',
  113. options: {
  114. configFile: path.resolve(
  115. __dirname,
  116. 'modules/writefull/frontend/js/integration/tsconfig.json'
  117. ),
  118. transpileOnly: true,
  119. },
  120. },
  121. ],
  122. },
  123. {
  124. // Pass application JS/TS files through babel-loader,
  125. // transpiling to targets defined in browserslist
  126. test: /\.([jt]sx?|[cm]js)$/,
  127. // Only compile application files and specific dependencies
  128. // (other npm and vendored dependencies must be in ES5 already)
  129. exclude: [
  130. /node_modules\/(?!(react-dnd|chart\.js|@uppy|pdfjs-dist|react-resizable-panels)\/)/,
  131. vendorDir,
  132. path.resolve(__dirname, 'modules/writefull/frontend/js/integration'),
  133. ],
  134. use: [
  135. {
  136. loader: 'babel-loader',
  137. options: {
  138. // Configure babel-loader to cache compiled output so that
  139. // subsequent compile runs are much faster
  140. cacheDirectory: true,
  141. configFile: path.join(__dirname, './babel.config.json'),
  142. plugins: [
  143. process.env.REACT_REFRESH_ENABLED === 'true' &&
  144. 'react-refresh/babel',
  145. ].filter(Boolean),
  146. },
  147. },
  148. ],
  149. type: 'javascript/auto',
  150. },
  151. {
  152. test: /\.wasm$/,
  153. type: 'asset/resource',
  154. generator: {
  155. filename: 'js/[name]-[contenthash][ext]',
  156. },
  157. },
  158. {
  159. test: /\.txt$/,
  160. type: 'asset/source',
  161. generator: {
  162. filename: 'js/[name]-[contenthash][ext]',
  163. },
  164. },
  165. {
  166. // Pass Sass files through sass-loader/css-loader/mini-css-extract-
  167. // plugin (note: run in reverse order)
  168. test: /\.s[ac]ss$/,
  169. use: [
  170. // Allows the CSS to be extracted to a separate .css file
  171. { loader: MiniCssExtractPlugin.loader },
  172. // Resolves any CSS dependencies (e.g. url())
  173. { loader: 'css-loader' },
  174. // Resolve relative paths sensibly in SASS
  175. { loader: 'resolve-url-loader' },
  176. {
  177. // Runs autoprefixer on CSS via postcss
  178. loader: 'postcss-loader',
  179. options: {
  180. postcssOptions: {
  181. plugins: ['autoprefixer'],
  182. },
  183. },
  184. },
  185. // Compile Sass off the main event loop
  186. {
  187. loader: 'thread-loader',
  188. options: {
  189. // keep workers alive for dev-server, and shut them down when not needed
  190. poolTimeout:
  191. process.env.NODE_ENV === 'development' ? 10 * 60 * 1000 : 500,
  192. // bring up more workers after they timed out
  193. poolRespawn: true,
  194. // limit concurrency (one per entrypoint and let the small includes queue up)
  195. workers: 6,
  196. },
  197. },
  198. // Compiles Sass to CSS
  199. {
  200. loader: 'sass-loader',
  201. options: { sourceMap: true }, // sourceMap: true is required for resolve-url-loader
  202. },
  203. ],
  204. },
  205. {
  206. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  207. test: /\.css$/i,
  208. oneOf: [
  209. {
  210. // Import as a string (for Shadow DOM usage): import styles from './file.css?inline'
  211. resourceQuery: /inline/,
  212. use: [
  213. {
  214. loader: 'css-loader',
  215. },
  216. {
  217. loader: 'postcss-loader',
  218. },
  219. ],
  220. },
  221. {
  222. // CSS from writefull module - inject directly into DOM
  223. include: path.resolve(__dirname, 'modules/writefull/'),
  224. use: [
  225. 'style-loader',
  226. {
  227. loader: 'css-loader',
  228. options: {
  229. importLoaders: 1,
  230. },
  231. },
  232. {
  233. loader: 'postcss-loader',
  234. options: {
  235. postcssOptions: {
  236. config: path.resolve(
  237. __dirname,
  238. 'modules/writefull/frontend/js/integration/postcss.config.js'
  239. ),
  240. },
  241. },
  242. },
  243. ],
  244. },
  245. {
  246. // CSS from AI module
  247. include: dirname(require.resolve('@overleaf/ai')),
  248. use: [
  249. {
  250. loader: 'css-loader',
  251. options: {
  252. exportType: 'css-style-sheet',
  253. },
  254. },
  255. {
  256. loader: 'postcss-loader',
  257. options: {
  258. postcssOptions: {
  259. config: require.resolve('@overleaf/ai/postcss.config.js'),
  260. },
  261. },
  262. },
  263. ],
  264. },
  265. {
  266. // Standard CSS processing (extracted into separate file)
  267. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  268. },
  269. ],
  270. },
  271. {
  272. // Load fonts
  273. test: /\.(woff2?|ttf|otf)$/,
  274. type: 'asset/resource',
  275. generator: {
  276. filename: 'fonts/[name]-[contenthash][ext]',
  277. },
  278. },
  279. {
  280. // Load images and videos (static files)
  281. test: /\.(svg|gif|png|jpg|pdf|mp4)$/,
  282. type: 'asset/resource',
  283. generator: {
  284. filename: 'images/[name]-[contenthash][ext]',
  285. },
  286. },
  287. {
  288. // These options are necessary for handlebars to have access to helper
  289. // methods
  290. test: /\.handlebars$/,
  291. loader: 'handlebars-loader',
  292. options: {
  293. compat: true,
  294. knownHelpersOnly: false,
  295. runtimePath: 'handlebars/runtime',
  296. },
  297. },
  298. {
  299. // Load translations files with custom loader, to extract and apply
  300. // fallbacks
  301. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  302. use: [
  303. {
  304. loader: path.join(__dirname, 'frontend/translations-loader.js'),
  305. },
  306. ],
  307. },
  308. ],
  309. },
  310. resolve: {
  311. alias: {
  312. // custom prefixes for import paths
  313. '@': path.resolve(__dirname, './frontend/js/'),
  314. '@modules': path.resolve(__dirname, './modules/'),
  315. '@ol-types': path.resolve(__dirname, './types/'),
  316. '@wf': path.resolve(
  317. __dirname,
  318. './modules/writefull/frontend/js/integration/src/'
  319. ),
  320. },
  321. // symlinks: false, // enable this while using `npm link`
  322. extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.json'],
  323. fallback: {
  324. events: require.resolve('events'),
  325. // for react-dnd + React 17
  326. 'react/jsx-runtime': 'react/jsx-runtime.js',
  327. 'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
  328. },
  329. },
  330. experiments: {
  331. asyncWebAssembly: true,
  332. },
  333. plugins: [
  334. new LezerGrammarCompilerPlugin(),
  335. // Generate a manifest.json file which is used by the backend to map the
  336. // base filenames to the generated output filenames
  337. new WebpackAssetsManifest({
  338. entrypoints: true,
  339. publicPath: true,
  340. output: 'manifest.json',
  341. }),
  342. new webpack.EnvironmentPlugin({
  343. // Ensure that process.env.RESET_APP_DATA_TIMER is defined, to avoid an error.
  344. // https://github.com/algolia/algoliasearch-client-javascript/issues/756
  345. RESET_APP_DATA_TIMER: '120000',
  346. // Ensure that process.env.CYPRESS is defined (see utils/worker.js)
  347. CYPRESS: false,
  348. }),
  349. // Prevent moment from loading (very large) locale files that aren't used
  350. new webpack.IgnorePlugin({
  351. resourceRegExp: /^\.\/locale$/,
  352. contextRegExp: /moment$/,
  353. }),
  354. // Set window.$ and window.jQuery
  355. new webpack.ProvidePlugin({
  356. $: 'jquery',
  357. jQuery: 'jquery',
  358. }),
  359. new CopyPlugin({
  360. patterns: [
  361. // Copy the required files for loading MathJax from MathJax NPM package
  362. // https://www.npmjs.com/package/mathjax#user-content-hosting-your-own-copy-of-the-mathjax-components
  363. {
  364. from: 'es5/tex-svg-full.js',
  365. to: `js/libs/mathjax-${PackageVersions.version.mathjax}/es5`,
  366. toType: 'dir',
  367. context: mathjaxDir,
  368. },
  369. {
  370. from: 'es5/input/tex/extensions/**/*.js',
  371. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  372. toType: 'dir',
  373. context: mathjaxDir,
  374. },
  375. {
  376. from: 'es5/ui/**/*',
  377. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  378. toType: 'dir',
  379. context: mathjaxDir,
  380. },
  381. {
  382. from: 'es5/a11y/**/*',
  383. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  384. toType: 'dir',
  385. context: mathjaxDir,
  386. },
  387. {
  388. from: 'es5/input/mml.js',
  389. to: `js/libs/mathjax-${PackageVersions.version.mathjax}/es5/input`,
  390. toType: 'dir',
  391. context: mathjaxDir,
  392. },
  393. {
  394. from: 'es5/sre/**/*',
  395. to: `js/libs/mathjax-${PackageVersions.version.mathjax}`,
  396. toType: 'dir',
  397. context: mathjaxDir,
  398. },
  399. {
  400. from: '*',
  401. to: `js/dictionaries/${PackageVersions.version.dictionaries}`,
  402. toType: 'dir',
  403. context: `${dictionariesDir}/dictionaries`,
  404. },
  405. // Copy CMap files (used to provide support for non-Latin characters),
  406. // wasm, ICC profiles, fonts and images from pdfjs-dist package to build output.
  407. {
  408. from: 'cmaps',
  409. to: 'js/pdfjs-dist/cmaps',
  410. context: pdfjsDir,
  411. },
  412. {
  413. from: 'iccs',
  414. to: 'js/pdfjs-dist/iccs',
  415. context: pdfjsDir,
  416. },
  417. {
  418. from: 'wasm',
  419. to: 'js/pdfjs-dist/wasm',
  420. context: pdfjsDir,
  421. },
  422. {
  423. from: 'standard_fonts',
  424. to: 'fonts/pdfjs-dist',
  425. context: pdfjsDir,
  426. },
  427. {
  428. from: 'legacy/web/images',
  429. to: 'images/pdfjs-dist',
  430. context: pdfjsDir,
  431. },
  432. ],
  433. }),
  434. ],
  435. }