webpack.config.js 9.5 KB

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