webpack.config.js 9.8 KB

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