webpack.config.js 11 KB

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