webpack.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. // Define how file types are handled by webpack
  96. module: {
  97. rules: [
  98. {
  99. // Pass application JS/TS files through babel-loader,
  100. // transpiling to targets defined in browserslist
  101. test: /\.([jt]sx?|[cm]js)$/,
  102. // Only compile application files and specific dependencies
  103. // (other npm and vendored dependencies must be in ES5 already)
  104. exclude: [/node_modules\/(?!(react-dnd|chart\.js)\/)/, vendorDir],
  105. use: [
  106. {
  107. loader: 'babel-loader',
  108. options: {
  109. // Configure babel-loader to cache compiled output so that
  110. // subsequent compile runs are much faster
  111. cacheDirectory: true,
  112. configFile: path.join(__dirname, './babel.config.json'),
  113. plugins: [
  114. process.env.REACT_REFRESH && 'react-refresh/babel',
  115. ].filter(Boolean),
  116. },
  117. },
  118. ],
  119. type: 'javascript/auto',
  120. },
  121. {
  122. // Pass Less files through less-loader/css-loader/mini-css-extract-
  123. // plugin (note: run in reverse order)
  124. test: /\.less$/,
  125. use: [
  126. // Allows the CSS to be extracted to a separate .css file
  127. { loader: MiniCssExtractPlugin.loader },
  128. // Resolves any CSS dependencies (e.g. url())
  129. { loader: 'css-loader' },
  130. {
  131. // Runs autoprefixer on CSS via postcss
  132. loader: 'postcss-loader',
  133. options: {
  134. postcssOptions: {
  135. plugins: ['autoprefixer'],
  136. },
  137. },
  138. },
  139. // Compiles the Less syntax to CSS
  140. { loader: 'less-loader' },
  141. ],
  142. },
  143. {
  144. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  145. test: /\.css$/i,
  146. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  147. },
  148. {
  149. // Load fonts
  150. test: /\.(woff2?|ttf|otf)$/,
  151. type: 'asset/resource',
  152. generator: {
  153. filename: 'fonts/[name]-[contenthash][ext]',
  154. },
  155. },
  156. {
  157. // Load images (static files)
  158. test: /\.(svg|gif|png|jpg|pdf)$/,
  159. type: 'asset/resource',
  160. generator: {
  161. filename: 'images/[name]-[contenthash][ext]',
  162. },
  163. },
  164. {
  165. // These options are necessary for handlebars to have access to helper
  166. // methods
  167. test: /\.handlebars$/,
  168. loader: 'handlebars-loader',
  169. options: {
  170. compat: true,
  171. knownHelpersOnly: false,
  172. runtimePath: 'handlebars/runtime',
  173. },
  174. },
  175. {
  176. // Load translations files with custom loader, to extract and apply
  177. // fallbacks
  178. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  179. use: [
  180. {
  181. loader: path.join(__dirname, 'frontend/translations-loader.js'),
  182. },
  183. ],
  184. },
  185. // Allow for injection of modules dependencies by reading contents of
  186. // modules directory and adding necessary dependencies
  187. {
  188. test: path.join(__dirname, 'modules/modules-main.js'),
  189. use: [
  190. {
  191. loader: 'val-loader',
  192. },
  193. ],
  194. },
  195. {
  196. test: path.join(__dirname, 'modules/modules-ide.js'),
  197. use: [
  198. {
  199. loader: 'val-loader',
  200. },
  201. ],
  202. },
  203. {
  204. // Expose jQuery and $ global variables
  205. test: require.resolve('jquery'),
  206. use: [
  207. {
  208. loader: 'expose-loader',
  209. options: {
  210. exposes: ['$', 'jQuery'],
  211. },
  212. },
  213. ],
  214. },
  215. ],
  216. },
  217. resolve: {
  218. alias: {
  219. // Aliases for AMD modules
  220. // Enables ace/ace shortcut
  221. ace: 'ace-builds/src-noconflict',
  222. // custom prefixes for import paths
  223. '@': path.resolve(__dirname, './frontend/js/'),
  224. },
  225. // symlinks: false, // enable this while using `npm link`
  226. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  227. fallback: {
  228. events: require.resolve('events'),
  229. },
  230. },
  231. plugins: [
  232. new LezerGrammarCompilerPlugin(),
  233. // Generate a manifest.json file which is used by the backend to map the
  234. // base filenames to the generated output filenames
  235. new WebpackAssetsManifest({
  236. entrypoints: true,
  237. publicPath: true,
  238. output: 'manifest.json',
  239. }),
  240. new webpack.EnvironmentPlugin({
  241. // Ensure that process.env.RESET_APP_DATA_TIMER is defined, to avoid an error.
  242. // https://github.com/algolia/algoliasearch-client-javascript/issues/756
  243. RESET_APP_DATA_TIMER: '120000',
  244. // Ensure that process.env.CYPRESS is defined (see utils/worker.js)
  245. CYPRESS: false,
  246. }),
  247. // Prevent moment from loading (very large) locale files that aren't used
  248. new webpack.IgnorePlugin({
  249. resourceRegExp: /^\.\/locale$/,
  250. contextRegExp: /moment$/,
  251. }),
  252. // Copy the required files for loading MathJax from MathJax NPM package
  253. new CopyPlugin({
  254. patterns: [
  255. // https://www.npmjs.com/package/mathjax#user-content-hosting-your-own-copy-of-the-mathjax-components
  256. {
  257. from: 'es5/tex-svg-full.js',
  258. to: 'js/libs/mathjax3/es5',
  259. context: mathjax3Dir,
  260. },
  261. {
  262. from: 'es5/input/tex/extensions/**/*.js',
  263. to: 'js/libs/mathjax3',
  264. context: mathjax3Dir,
  265. },
  266. {
  267. from: 'es5/ui/**/*',
  268. to: 'js/libs/mathjax3',
  269. context: mathjax3Dir,
  270. },
  271. { from: 'MathJax.js', to: 'js/libs/mathjax', context: mathjaxDir },
  272. { from: 'config/**/*', to: 'js/libs/mathjax', context: mathjaxDir },
  273. {
  274. from: 'extensions/**/*',
  275. globOptions: {
  276. // https://github.com/mathjax/MathJax/issues/2403
  277. ignore: ['**/mathmaps/*.js'],
  278. },
  279. to: 'js/libs/mathjax',
  280. context: mathjaxDir,
  281. },
  282. {
  283. from: 'localization/en/**/*',
  284. to: 'js/libs/mathjax',
  285. context: mathjaxDir,
  286. },
  287. {
  288. from: 'jax/output/HTML-CSS/fonts/TeX/**/*',
  289. to: 'js/libs/mathjax',
  290. context: mathjaxDir,
  291. },
  292. {
  293. from: 'jax/output/HTML-CSS/**/*.js',
  294. to: 'js/libs/mathjax',
  295. context: mathjaxDir,
  296. },
  297. {
  298. from: 'jax/element/**/*',
  299. to: 'js/libs/mathjax',
  300. context: mathjaxDir,
  301. },
  302. { from: 'jax/input/**/*', to: 'js/libs/mathjax', context: mathjaxDir },
  303. {
  304. from: 'fonts/HTML-CSS/TeX/woff/*',
  305. to: 'js/libs/mathjax',
  306. context: mathjaxDir,
  307. },
  308. {
  309. from: 'src-min-noconflict',
  310. to: `js/ace-${PackageVersions.version.ace}/`,
  311. context: aceDir,
  312. },
  313. ...pdfjsVersions.flatMap(version => {
  314. const dir = getModuleDirectory(version)
  315. // Copy CMap files (used to provide support for non-Latin characters)
  316. // and static images from pdfjs-dist package to build output.
  317. return [
  318. { from: `cmaps`, to: `js/${version}/cmaps`, context: dir },
  319. {
  320. from: `standard_fonts`,
  321. to: `fonts/${version}`,
  322. context: dir,
  323. },
  324. {
  325. from: `legacy/web/images`,
  326. to: `images/${version}`,
  327. context: dir,
  328. },
  329. ]
  330. }),
  331. ],
  332. }),
  333. ],
  334. }