webpack.config.js 14 KB

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