webpack.config.js 9.2 KB

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