webpack.config.js 9.7 KB

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