webpack.config.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. const fs = require('fs')
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const CopyPlugin = require('copy-webpack-plugin')
  5. const ManifestPlugin = require('webpack-manifest-plugin')
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  7. const PackageVersions = require('./app/src/infrastructure/PackageVersions')
  8. const MODULES_PATH = path.join(__dirname, '/modules')
  9. // Generate a hash of entry points, including modules
  10. const entryPoints = {
  11. serviceWorker: './frontend/js/serviceWorker.js',
  12. main: './frontend/js/main.js',
  13. ide: './frontend/js/ide.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. // Attempt to load frontend entry-points from modules, if they exist
  19. if (fs.existsSync(MODULES_PATH)) {
  20. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  21. const entryPath = path.join(MODULES_PATH, module, '/frontend/js/index.js')
  22. if (fs.existsSync(entryPath)) {
  23. acc[module] = entryPath
  24. }
  25. return acc
  26. }, entryPoints)
  27. }
  28. module.exports = {
  29. // Defines the "entry point(s)" for the application - i.e. the file which
  30. // bootstraps the application
  31. entry: entryPoints,
  32. // Define where and how the bundle will be output to disk
  33. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  34. // kept in memory for speed
  35. output: {
  36. path: path.join(__dirname, '/public'),
  37. // By default write into js directory
  38. filename: 'js/[name].js',
  39. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  40. // style code bundles
  41. libraryTarget: 'umd',
  42. // Name the exported variable from output bundle
  43. library: ['Frontend', '[name]'],
  44. },
  45. // Define how file types are handled by webpack
  46. module: {
  47. rules: [
  48. {
  49. // Pass application JS files through babel-loader, compiling to ES5
  50. test: /\.js$/,
  51. // Only compile application files (npm and vendored dependencies are in
  52. // ES5 already)
  53. exclude: [
  54. /node_modules\/(?!react-dnd\/)/,
  55. path.resolve(__dirname, 'frontend/js/vendor'),
  56. ],
  57. use: [
  58. {
  59. loader: 'babel-loader',
  60. options: {
  61. // Configure babel-loader to cache compiled output so that
  62. // subsequent compile runs are much faster
  63. cacheDirectory: true,
  64. },
  65. },
  66. ],
  67. },
  68. {
  69. // Wrap PDF.js worker in a Web Worker
  70. test: /pdf\.worker\.js$/,
  71. use: [
  72. {
  73. loader: 'worker-loader',
  74. options: {
  75. // Write into js directory (note: customising this is not possible
  76. // with pdfjs-dist/webpack auto loader)
  77. name: 'js/pdfjs-worker.[hash].js',
  78. // Override dynamically-set publicPath to explicitly use root.
  79. // This prevents a security problem where the Worker - normally
  80. // loaded from a CDN - has cross-origin issues, by forcing it to not
  81. // be loaded from the CDN
  82. publicPath: '/',
  83. },
  84. },
  85. ],
  86. },
  87. {
  88. test: /serviceWorker.js$/,
  89. use: [
  90. {
  91. loader: 'worker-loader',
  92. options: {
  93. name: 'serviceWorker.js',
  94. },
  95. },
  96. ],
  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. // Uniquely identifies the postcss plugin (required by webpack)
  112. ident: 'postcss',
  113. plugins: [require('autoprefixer')],
  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. use: [
  129. {
  130. loader: 'file-loader',
  131. options: {
  132. // Output to public/font
  133. outputPath: 'fonts',
  134. publicPath: '/fonts/',
  135. name: '[name].[ext]',
  136. },
  137. },
  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.resolve('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: 'jQuery',
  186. },
  187. {
  188. loader: 'expose-loader',
  189. options: '$',
  190. },
  191. ],
  192. },
  193. {
  194. // Expose angular global variable
  195. test: require.resolve('angular'),
  196. use: [
  197. {
  198. loader: 'expose-loader',
  199. options: 'angular',
  200. },
  201. ],
  202. },
  203. ],
  204. },
  205. resolve: {
  206. alias: {
  207. // Aliases for AMD modules
  208. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  209. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  210. // Enables ace/ace shortcut
  211. ace: 'ace-builds/src-noconflict',
  212. // fineupload vendored dependency (which we're aliasing to fineuploadER
  213. // for some reason)
  214. fineuploader: path.join(
  215. __dirname,
  216. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  217. ),
  218. },
  219. },
  220. // Split out vendored dependencies that are shared between 2 or more "real
  221. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  222. // that they are de-duplicated from the other bundles. This allows the
  223. // libraries bundle to be independently cached (as it likely will change less
  224. // than the other bundles)
  225. optimization: {
  226. splitChunks: {
  227. cacheGroups: {
  228. libraries: {
  229. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  230. name: 'libraries',
  231. chunks: 'initial',
  232. minChunks: 2,
  233. },
  234. },
  235. },
  236. },
  237. plugins: [
  238. // Generate a manifest.json file which is used by the backend to map the
  239. // base filenames to the generated output filenames
  240. new ManifestPlugin({
  241. // Always write the manifest file to disk (even if in dev mode, where
  242. // files are held in memory). This is needed because the server will read
  243. // this file (from disk) when building the script's url
  244. writeToFileEmit: true,
  245. }),
  246. // Silence react messages in the dev-tools console
  247. new webpack.DefinePlugin({
  248. __REACT_DEVTOOLS_GLOBAL_HOOK__: '({ isDisabled: true })',
  249. }),
  250. // Prevent moment from loading (very large) locale files that aren't used
  251. new webpack.IgnorePlugin({
  252. resourceRegExp: /^\.\/locale$/,
  253. contextRegExp: /moment$/,
  254. }),
  255. // Copy the required files for loading MathJax from MathJax NPM package
  256. new CopyPlugin(
  257. [
  258. { from: 'MathJax.js', to: 'js/libs/mathjax' },
  259. { from: 'config/**/*', to: 'js/libs/mathjax' },
  260. { from: 'extensions/**/*', to: 'js/libs/mathjax' },
  261. { from: 'localization/en/**/*', to: 'js/libs/mathjax' },
  262. { from: 'jax/output/HTML-CSS/fonts/TeX/**/*', to: 'js/libs/mathjax' },
  263. { from: 'jax/output/HTML-CSS/**/*.js', to: 'js/libs/mathjax' },
  264. { from: 'jax/element/**/*', to: 'js/libs/mathjax' },
  265. { from: 'jax/input/**/*', to: 'js/libs/mathjax' },
  266. { from: 'fonts/HTML-CSS/TeX/woff/*', to: 'js/libs/mathjax' },
  267. ],
  268. {
  269. context: 'node_modules/mathjax',
  270. }
  271. ),
  272. new CopyPlugin([
  273. {
  274. from: 'frontend/js/vendor/libs/sigma-master',
  275. to: 'js/libs/sigma-master',
  276. },
  277. {
  278. from: 'node_modules/ace-builds/src-min-noconflict',
  279. to: `js/ace-${PackageVersions.version.ace}/`,
  280. },
  281. // Copy CMap files from pdfjs-dist package to build output. These are used
  282. // to provide support for non-Latin characters
  283. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' },
  284. ]),
  285. ],
  286. }