webpack.config.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. main: './frontend/js/main.js',
  12. ide: './frontend/js/ide.js',
  13. style: './frontend/stylesheets/style.less',
  14. 'ieee-style': './frontend/stylesheets/ieee-style.less',
  15. 'light-style': './frontend/stylesheets/light-style.less'
  16. }
  17. // Attempt to load frontend entry-points from modules, if they exist
  18. if (fs.existsSync(MODULES_PATH)) {
  19. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  20. const entryPath = path.join(MODULES_PATH, module, '/frontend/js/index.js')
  21. if (fs.existsSync(entryPath)) {
  22. acc[module] = entryPath
  23. }
  24. return acc
  25. }, entryPoints)
  26. }
  27. module.exports = {
  28. // Defines the "entry point(s)" for the application - i.e. the file which
  29. // bootstraps the application
  30. entry: entryPoints,
  31. // Define where and how the bundle will be output to disk
  32. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  33. // kept in memory for speed
  34. output: {
  35. path: path.join(__dirname, '/public'),
  36. // By default write into js directory
  37. filename: 'js/[name].js',
  38. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  39. // style code bundles
  40. libraryTarget: 'umd',
  41. // Name the exported variable from output bundle
  42. library: ['Frontend', '[name]']
  43. },
  44. // Define how file types are handled by webpack
  45. module: {
  46. rules: [
  47. {
  48. // Pass application JS files through babel-loader, compiling to ES5
  49. test: /\.js$/,
  50. // Only compile application files (npm and vendored dependencies are in
  51. // ES5 already)
  52. exclude: [
  53. /node_modules/,
  54. path.resolve(__dirname, 'frontend/js/vendor')
  55. ],
  56. use: [
  57. {
  58. loader: 'babel-loader',
  59. options: {
  60. // Configure babel-loader to cache compiled output so that
  61. // subsequent compile runs are much faster
  62. cacheDirectory: true
  63. }
  64. }
  65. ]
  66. },
  67. {
  68. // Wrap PDF.js worker in a Web Worker
  69. test: /pdf\.worker\.js$/,
  70. use: [
  71. {
  72. loader: 'worker-loader',
  73. options: {
  74. // Write into js directory (note: customising this is not possible
  75. // with pdfjs-dist/webpack auto loader)
  76. name: 'js/pdfjs-worker.[hash].js',
  77. // Override dynamically-set publicPath to explicitly use root.
  78. // This prevents a security problem where the Worker - normally
  79. // loaded from a CDN - has cross-origin issues, by forcing it to not
  80. // be loaded from the CDN
  81. publicPath: '/'
  82. }
  83. }
  84. ]
  85. },
  86. {
  87. // Pass Less files through less-loader/css-loader/mini-css-extract-
  88. // plugin (note: run in reverse order)
  89. test: /\.less$/,
  90. use: [
  91. // Allows the CSS to be extracted to a separate .css file
  92. { loader: MiniCssExtractPlugin.loader },
  93. // Resolves any CSS dependencies (e.g. url())
  94. { loader: 'css-loader' },
  95. {
  96. // Runs autoprefixer on CSS via postcss
  97. loader: 'postcss-loader',
  98. options: {
  99. // Uniquely identifies the postcss plugin (required by webpack)
  100. ident: 'postcss',
  101. plugins: [require('autoprefixer')]
  102. }
  103. },
  104. // Compiles the Less syntax to CSS
  105. { loader: 'less-loader' }
  106. ]
  107. },
  108. {
  109. // Load fonts
  110. test: /\.(woff|woff2)$/,
  111. use: [
  112. {
  113. loader: 'file-loader',
  114. options: {
  115. // Output to public/font
  116. outputPath: 'fonts',
  117. publicPath: '/fonts/',
  118. name: '[name].[ext]'
  119. }
  120. }
  121. ]
  122. },
  123. {
  124. // These options are necessary for handlebars to have access to helper
  125. // methods
  126. test: /\.handlebars$/,
  127. loader: 'handlebars-loader',
  128. options: {
  129. compat: true,
  130. knownHelpersOnly: false,
  131. runtimePath: 'handlebars/runtime'
  132. }
  133. },
  134. {
  135. // Load translations files with custom loader, to extract and apply
  136. // fallbacks
  137. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  138. use: [
  139. {
  140. loader: path.resolve('frontend/translations-loader.js')
  141. }
  142. ]
  143. },
  144. // Allow for injection of modules dependencies by reading contents of
  145. // modules directory and adding necessary dependencies
  146. {
  147. test: path.join(__dirname, 'modules/modules-main.js'),
  148. use: [
  149. {
  150. loader: 'val-loader'
  151. }
  152. ]
  153. },
  154. {
  155. test: path.join(__dirname, 'modules/modules-ide.js'),
  156. use: [
  157. {
  158. loader: 'val-loader'
  159. }
  160. ]
  161. },
  162. {
  163. // Expose jQuery and $ global variables
  164. test: require.resolve('jquery'),
  165. use: [
  166. {
  167. loader: 'expose-loader',
  168. options: 'jQuery'
  169. },
  170. {
  171. loader: 'expose-loader',
  172. options: '$'
  173. }
  174. ]
  175. },
  176. {
  177. // Expose angular global variable
  178. test: require.resolve('angular'),
  179. use: [
  180. {
  181. loader: 'expose-loader',
  182. options: 'angular'
  183. }
  184. ]
  185. }
  186. ]
  187. },
  188. resolve: {
  189. alias: {
  190. // Aliases for AMD modules
  191. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  192. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  193. // Enables ace/ace shortcut
  194. ace: path.join(
  195. __dirname,
  196. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  197. ),
  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. },
  206. // Split out vendored dependencies that are shared between 2 or more "real
  207. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  208. // that they are de-duplicated from the other bundles. This allows the
  209. // libraries bundle to be independently cached (as it likely will change less
  210. // than the other bundles)
  211. optimization: {
  212. splitChunks: {
  213. cacheGroups: {
  214. libraries: {
  215. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  216. name: 'libraries',
  217. chunks: 'initial',
  218. minChunks: 2
  219. }
  220. }
  221. }
  222. },
  223. plugins: [
  224. // Generate a manifest.json file which is used by the backend to map the
  225. // base filenames to the generated output filenames
  226. new ManifestPlugin({
  227. // Always write the manifest file to disk (even if in dev mode, where
  228. // files are held in memory). This is needed because the server will read
  229. // this file (from disk) when building the script's url
  230. writeToFileEmit: true
  231. }),
  232. // Silence react messages in the dev-tools console
  233. new webpack.DefinePlugin({
  234. __REACT_DEVTOOLS_GLOBAL_HOOK__: '({ isDisabled: true })'
  235. }),
  236. // Prevent moment from loading (very large) locale files that aren't used
  237. new webpack.IgnorePlugin({
  238. resourceRegExp: /^\.\/locale$/,
  239. contextRegExp: /moment$/
  240. }),
  241. new CopyPlugin([
  242. {
  243. from: 'frontend/js/vendor/libs/mathjax',
  244. to: 'js/libs/mathjax'
  245. },
  246. {
  247. from: 'frontend/js/vendor/libs/sigma-master',
  248. to: 'js/libs/sigma-master'
  249. },
  250. {
  251. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  252. to: `js/ace-${PackageVersions.version.ace}/`
  253. },
  254. // Copy CMap files from pdfjs-dist package to build output. These are used
  255. // to provide support for non-Latin characters
  256. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  257. ])
  258. ]
  259. }