webpack.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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')
  11. // Generate a hash of entry points, including modules
  12. const entryPoints = {
  13. tracing: './frontend/js/tracing.js',
  14. main: './frontend/js/main.js',
  15. ide: './frontend/js/ide.js',
  16. 'ide-detached': './frontend/js/ide-detached.js',
  17. marketing: './frontend/js/marketing.js',
  18. style: './frontend/stylesheets/style.less',
  19. 'ieee-style': './frontend/stylesheets/ieee-style.less',
  20. 'light-style': './frontend/stylesheets/light-style.less',
  21. 'main-style': './frontend/stylesheets/main-style.less',
  22. 'main-ieee-style': './frontend/stylesheets/main-ieee-style.less',
  23. 'main-light-style': './frontend/stylesheets/main-light-style.less',
  24. }
  25. // Add entrypoints for each "page"
  26. glob
  27. .sync(path.join(__dirname, 'modules/*/frontend/js/pages/**/*.js'))
  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$/, '')
  35. entryPoints[name] = './' + path.relative(__dirname, page)
  36. })
  37. glob.sync(path.join(__dirname, 'frontend/js/pages/**/*.js')).forEach(page => {
  38. // in: /workspace/services/web/frontend/js/pages/marketing/homepage.js
  39. // out: pages/marketing/homepage
  40. const name = path
  41. .relative(path.join(__dirname, 'frontend/js/'), page)
  42. .replace(/.js$/, '')
  43. entryPoints[name] = './' + path.relative(__dirname, page)
  44. })
  45. function getModuleDirectory(moduleName) {
  46. const entrypointPath = require.resolve(moduleName)
  47. const suffix = `node_modules/${moduleName}`
  48. const idx = entrypointPath.indexOf(suffix)
  49. if (idx === -1) {
  50. throw new Error(`could not find Node module: ${moduleName}`)
  51. }
  52. return entrypointPath.slice(0, idx + suffix.length)
  53. }
  54. const mathjaxDir = getModuleDirectory('mathjax')
  55. const mathjax3Dir = getModuleDirectory('mathjax-3')
  56. const aceDir = getModuleDirectory('ace-builds')
  57. const pdfjsVersions = ['pdfjs-dist213', 'pdfjs-dist36']
  58. const vendorDir = path.join(__dirname, 'frontend/js/vendor')
  59. const ACE_VERSION = require('ace-builds/version')
  60. if (ACE_VERSION !== PackageVersions.version.ace) {
  61. throw new Error(
  62. '"ace-builds" version de-synced, update services/web/app/src/infrastructure/PackageVersions.js'
  63. )
  64. }
  65. const MATHJAX_VERSION = require('mathjax/package.json').version
  66. if (MATHJAX_VERSION !== PackageVersions.version.mathjax) {
  67. throw new Error(
  68. '"mathjax" version de-synced, update services/web/app/src/infrastructure/PackageVersions.js'
  69. )
  70. }
  71. const MATHJAX_3_VERSION = require('mathjax-3/package.json').version
  72. if (MATHJAX_3_VERSION !== PackageVersions.version['mathjax-3']) {
  73. throw new Error(
  74. '"mathjax-3" version de-synced, update services/web/app/src/infrastructure/PackageVersions.js'
  75. )
  76. }
  77. module.exports = {
  78. // Defines the "entry point(s)" for the application - i.e. the file which
  79. // bootstraps the application
  80. entry: entryPoints,
  81. // Define where and how the bundle will be output to disk
  82. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  83. // kept in memory for speed
  84. output: {
  85. path: path.join(__dirname, 'public'),
  86. publicPath: '/',
  87. // By default write into js directory
  88. filename: 'js/[name]-[contenthash].js',
  89. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  90. // style code bundles
  91. libraryTarget: 'umd',
  92. // Name the exported variable from output bundle
  93. library: ['Frontend', '[name]'],
  94. },
  95. optimization: {
  96. // https://webpack.js.org/plugins/split-chunks-plugin/#splitchunkschunks
  97. splitChunks: {
  98. chunks: 'all', // allow non-async chunks to be analysed for shared modules
  99. },
  100. },
  101. // Define how file types are handled by webpack
  102. module: {
  103. rules: [
  104. {
  105. // Pass application JS/TS files through babel-loader,
  106. // transpiling to targets defined in browserslist
  107. test: /\.([jt]sx?|[cm]js)$/,
  108. // Only compile application files and specific dependencies
  109. // (other npm and vendored dependencies must be in ES5 already)
  110. exclude: [/node_modules\/(?!(react-dnd|chart\.js)\/)/, vendorDir],
  111. use: [
  112. {
  113. loader: 'babel-loader',
  114. options: {
  115. // Configure babel-loader to cache compiled output so that
  116. // subsequent compile runs are much faster
  117. cacheDirectory: true,
  118. configFile: path.join(__dirname, './babel.config.json'),
  119. plugins: [
  120. process.env.REACT_REFRESH && 'react-refresh/babel',
  121. ].filter(Boolean),
  122. },
  123. },
  124. ],
  125. type: 'javascript/auto',
  126. },
  127. {
  128. // Pass Less files through less-loader/css-loader/mini-css-extract-
  129. // plugin (note: run in reverse order)
  130. test: /\.less$/,
  131. use: [
  132. // Allows the CSS to be extracted to a separate .css file
  133. { loader: MiniCssExtractPlugin.loader },
  134. // Resolves any CSS dependencies (e.g. url())
  135. { loader: 'css-loader' },
  136. {
  137. // Runs autoprefixer on CSS via postcss
  138. loader: 'postcss-loader',
  139. options: {
  140. postcssOptions: {
  141. plugins: ['autoprefixer'],
  142. },
  143. },
  144. },
  145. // Compiles the Less syntax to CSS
  146. { loader: 'less-loader' },
  147. ],
  148. },
  149. {
  150. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  151. test: /\.css$/i,
  152. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  153. },
  154. {
  155. // Load fonts
  156. test: /\.(woff2?|ttf|otf)$/,
  157. type: 'asset/resource',
  158. generator: {
  159. filename: 'fonts/[name]-[contenthash][ext]',
  160. },
  161. },
  162. {
  163. // Load images (static files)
  164. test: /\.(svg|gif|png|jpg|pdf)$/,
  165. type: 'asset/resource',
  166. generator: {
  167. filename: 'images/[name]-[contenthash][ext]',
  168. },
  169. },
  170. {
  171. // These options are necessary for handlebars to have access to helper
  172. // methods
  173. test: /\.handlebars$/,
  174. loader: 'handlebars-loader',
  175. options: {
  176. compat: true,
  177. knownHelpersOnly: false,
  178. runtimePath: 'handlebars/runtime',
  179. },
  180. },
  181. {
  182. // Load translations files with custom loader, to extract and apply
  183. // fallbacks
  184. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  185. use: [
  186. {
  187. loader: path.join(__dirname, 'frontend/translations-loader.js'),
  188. },
  189. ],
  190. },
  191. // Allow for injection of modules dependencies by reading contents of
  192. // modules directory and adding necessary dependencies
  193. {
  194. test: path.join(__dirname, 'modules/modules-main.js'),
  195. use: [
  196. {
  197. loader: 'val-loader',
  198. },
  199. ],
  200. },
  201. {
  202. test: path.join(__dirname, 'modules/modules-ide.js'),
  203. use: [
  204. {
  205. loader: 'val-loader',
  206. },
  207. ],
  208. },
  209. {
  210. // Expose jQuery and $ global variables
  211. test: require.resolve('jquery'),
  212. use: [
  213. {
  214. loader: 'expose-loader',
  215. options: {
  216. exposes: ['$', 'jQuery'],
  217. },
  218. },
  219. ],
  220. },
  221. ],
  222. },
  223. resolve: {
  224. alias: {
  225. // Aliases for AMD modules
  226. // Enables ace/ace shortcut
  227. ace: 'ace-builds/src-noconflict',
  228. // custom prefixes for import paths
  229. '@': path.resolve(__dirname, './frontend/js/'),
  230. },
  231. // symlinks: false, // enable this while using `npm link`
  232. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  233. fallback: {
  234. events: require.resolve('events'),
  235. },
  236. },
  237. plugins: [
  238. new LezerGrammarCompilerPlugin(),
  239. // Generate a manifest.json file which is used by the backend to map the
  240. // base filenames to the generated output filenames
  241. new WebpackAssetsManifest({
  242. entrypoints: true,
  243. publicPath: true,
  244. output: 'manifest.json',
  245. }),
  246. new webpack.EnvironmentPlugin({
  247. // Ensure that process.env.RESET_APP_DATA_TIMER is defined, to avoid an error.
  248. // https://github.com/algolia/algoliasearch-client-javascript/issues/756
  249. RESET_APP_DATA_TIMER: '120000',
  250. // Ensure that process.env.CYPRESS is defined (see utils/worker.js)
  251. CYPRESS: false,
  252. }),
  253. // Prevent moment from loading (very large) locale files that aren't used
  254. new webpack.IgnorePlugin({
  255. resourceRegExp: /^\.\/locale$/,
  256. contextRegExp: /moment$/,
  257. }),
  258. // Copy the required files for loading MathJax from MathJax NPM package
  259. new CopyPlugin({
  260. patterns: [
  261. // https://www.npmjs.com/package/mathjax#user-content-hosting-your-own-copy-of-the-mathjax-components
  262. {
  263. from: 'es5/tex-svg-full.js',
  264. to: 'js/libs/mathjax3/es5',
  265. context: mathjax3Dir,
  266. },
  267. {
  268. from: 'es5/input/tex/extensions/**/*.js',
  269. to: 'js/libs/mathjax3',
  270. context: mathjax3Dir,
  271. },
  272. {
  273. from: 'es5/ui/**/*',
  274. to: 'js/libs/mathjax3',
  275. context: mathjax3Dir,
  276. },
  277. { from: 'MathJax.js', to: 'js/libs/mathjax', context: mathjaxDir },
  278. { from: 'config/**/*', to: 'js/libs/mathjax', context: mathjaxDir },
  279. {
  280. from: 'extensions/**/*',
  281. globOptions: {
  282. // https://github.com/mathjax/MathJax/issues/2403
  283. ignore: ['**/mathmaps/*.js'],
  284. },
  285. to: 'js/libs/mathjax',
  286. context: mathjaxDir,
  287. },
  288. {
  289. from: 'localization/en/**/*',
  290. to: 'js/libs/mathjax',
  291. context: mathjaxDir,
  292. },
  293. {
  294. from: 'jax/output/HTML-CSS/fonts/TeX/**/*',
  295. to: 'js/libs/mathjax',
  296. context: mathjaxDir,
  297. },
  298. {
  299. from: 'jax/output/HTML-CSS/**/*.js',
  300. to: 'js/libs/mathjax',
  301. context: mathjaxDir,
  302. },
  303. {
  304. from: 'jax/element/**/*',
  305. to: 'js/libs/mathjax',
  306. context: mathjaxDir,
  307. },
  308. { from: 'jax/input/**/*', to: 'js/libs/mathjax', context: mathjaxDir },
  309. {
  310. from: 'fonts/HTML-CSS/TeX/woff/*',
  311. to: 'js/libs/mathjax',
  312. context: mathjaxDir,
  313. },
  314. {
  315. from: 'src-min-noconflict',
  316. to: `js/ace-${PackageVersions.version.ace}/`,
  317. context: aceDir,
  318. },
  319. ...pdfjsVersions.flatMap(version => {
  320. const dir = getModuleDirectory(version)
  321. // Copy CMap files (used to provide support for non-Latin characters)
  322. // and static images from pdfjs-dist package to build output.
  323. return [
  324. { from: `cmaps`, to: `js/${version}/cmaps`, context: dir },
  325. {
  326. from: `standard_fonts`,
  327. to: `fonts/${version}`,
  328. context: dir,
  329. },
  330. {
  331. from: `legacy/web/images`,
  332. to: `images/${version}`,
  333. context: dir,
  334. },
  335. ]
  336. }),
  337. ],
  338. }),
  339. ],
  340. }