webpack.config.js 10 KB

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