webpack.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // Allow for injection of modules dependencies by reading contents of
  135. // modules directory and adding necessary dependencies
  136. {
  137. test: path.join(__dirname, 'modules/modules-main.js'),
  138. use: [
  139. {
  140. loader: 'val-loader'
  141. }
  142. ]
  143. },
  144. {
  145. test: path.join(__dirname, 'modules/modules-ide.js'),
  146. use: [
  147. {
  148. loader: 'val-loader'
  149. }
  150. ]
  151. },
  152. {
  153. // Expose jQuery and $ global variables
  154. test: require.resolve('jquery'),
  155. use: [
  156. {
  157. loader: 'expose-loader',
  158. options: 'jQuery'
  159. },
  160. {
  161. loader: 'expose-loader',
  162. options: '$'
  163. }
  164. ]
  165. },
  166. {
  167. // Expose angular global variable
  168. test: require.resolve('angular'),
  169. use: [
  170. {
  171. loader: 'expose-loader',
  172. options: 'angular'
  173. }
  174. ]
  175. }
  176. ]
  177. },
  178. resolve: {
  179. alias: {
  180. // Aliases for AMD modules
  181. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  182. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  183. // Enables ace/ace shortcut
  184. ace: path.join(
  185. __dirname,
  186. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  187. ),
  188. // fineupload vendored dependency (which we're aliasing to fineuploadER
  189. // for some reason)
  190. fineuploader: path.join(
  191. __dirname,
  192. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  193. )
  194. }
  195. },
  196. // Split out vendored dependencies that are shared between 2 or more "real
  197. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  198. // that they are de-duplicated from the other bundles. This allows the
  199. // libraries bundle to be independently cached (as it likely will change less
  200. // than the other bundles)
  201. optimization: {
  202. splitChunks: {
  203. cacheGroups: {
  204. libraries: {
  205. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  206. name: 'libraries',
  207. chunks: 'initial',
  208. minChunks: 2
  209. }
  210. }
  211. }
  212. },
  213. plugins: [
  214. // Generate a manifest.json file which is used by the backend to map the
  215. // base filenames to the generated output filenames
  216. new ManifestPlugin({
  217. // Always write the manifest file to disk (even if in dev mode, where
  218. // files are held in memory). This is needed because the server will read
  219. // this file (from disk) when building the script's url
  220. writeToFileEmit: true
  221. }),
  222. // Prevent moment from loading (very large) locale files that aren't used
  223. new webpack.IgnorePlugin({
  224. resourceRegExp: /^\.\/locale$/,
  225. contextRegExp: /moment$/
  226. }),
  227. new CopyPlugin([
  228. {
  229. from: 'frontend/js/vendor/libs/mathjax',
  230. to: 'js/libs/mathjax'
  231. },
  232. {
  233. from: 'frontend/js/vendor/libs/sigma-master',
  234. to: 'js/libs/sigma-master'
  235. },
  236. {
  237. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  238. to: `js/ace-${PackageVersions.version.ace}/`
  239. },
  240. // Copy CMap files from pdfjs-dist package to build output. These are used
  241. // to provide support for non-Latin characters
  242. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  243. ])
  244. ]
  245. }