webpack.config.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 (dependencies are in ES5 already)
  51. exclude: /node_modules/,
  52. use: [
  53. {
  54. loader: 'babel-loader',
  55. options: {
  56. // Configure babel-loader to cache compiled output so that
  57. // subsequent compile runs are much faster
  58. cacheDirectory: true
  59. }
  60. }
  61. ]
  62. },
  63. {
  64. // Wrap PDF.js worker in a Web Worker
  65. test: /pdf\.worker\.js$/,
  66. use: [
  67. {
  68. loader: 'worker-loader',
  69. options: {
  70. // Write into js directory (note: customising this is not possible
  71. // with pdfjs-dist/webpack auto loader)
  72. name: 'js/pdfjs-worker.[hash].js',
  73. // Override dynamically-set publicPath to explicitly use root.
  74. // This prevents a security problem where the Worker - normally
  75. // loaded from a CDN - has cross-origin issues, by forcing it to not
  76. // be loaded from the CDN
  77. publicPath: '/'
  78. }
  79. }
  80. ]
  81. },
  82. {
  83. // Pass Less files through less-loader/css-loader/mini-css-extract-
  84. // plugin (note: run in reverse order)
  85. test: /\.less$/,
  86. use: [
  87. // Allows the CSS to be extracted to a separate .css file
  88. { loader: MiniCssExtractPlugin.loader },
  89. // Resolves any CSS dependencies (e.g. url())
  90. { loader: 'css-loader' },
  91. {
  92. // Runs autoprefixer on CSS via postcss
  93. loader: 'postcss-loader',
  94. options: {
  95. // Uniquely identifies the postcss plugin (required by webpack)
  96. ident: 'postcss',
  97. plugins: [
  98. require('autoprefixer')({ env: 'last 2 versions, ie >= 10' })
  99. ]
  100. }
  101. },
  102. // Compiles the Less syntax to CSS
  103. { loader: 'less-loader' }
  104. ]
  105. },
  106. {
  107. // Load fonts
  108. test: /\.(woff|woff2)$/,
  109. use: [
  110. {
  111. loader: 'file-loader',
  112. options: {
  113. // Output to public/font
  114. outputPath: 'fonts',
  115. publicPath: '/fonts/',
  116. name: '[name].[ext]'
  117. }
  118. }
  119. ]
  120. },
  121. {
  122. // These options are necessary for handlebars to have access to helper
  123. // methods
  124. test: /\.handlebars$/,
  125. loader: 'handlebars-loader',
  126. options: {
  127. compat: true,
  128. knownHelpersOnly: false,
  129. runtimePath: 'handlebars/runtime'
  130. }
  131. },
  132. // Allow for injection of modules dependencies by reading contents of
  133. // modules directory and adding necessary dependencies
  134. {
  135. test: path.join(__dirname, 'modules/modules-main.js'),
  136. use: [
  137. {
  138. loader: 'val-loader'
  139. }
  140. ]
  141. },
  142. {
  143. test: path.join(__dirname, 'modules/modules-ide.js'),
  144. use: [
  145. {
  146. loader: 'val-loader'
  147. }
  148. ]
  149. },
  150. {
  151. // Expose jQuery and $ global variables
  152. test: require.resolve('jquery'),
  153. use: [
  154. {
  155. loader: 'expose-loader',
  156. options: 'jQuery'
  157. },
  158. {
  159. loader: 'expose-loader',
  160. options: '$'
  161. }
  162. ]
  163. },
  164. {
  165. // Expose angular global variable
  166. test: require.resolve('angular'),
  167. use: [
  168. {
  169. loader: 'expose-loader',
  170. options: 'angular'
  171. }
  172. ]
  173. },
  174. {
  175. // Expose lodash global variable
  176. test: require.resolve('lodash'),
  177. use: [
  178. {
  179. loader: 'expose-loader',
  180. options: '_'
  181. }
  182. ]
  183. }
  184. ]
  185. },
  186. resolve: {
  187. alias: {
  188. // Aliases for AMD modules
  189. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  190. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  191. // Enables ace/ace shortcut
  192. ace: path.join(
  193. __dirname,
  194. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  195. ),
  196. // fineupload vendored dependency (which we're aliasing to fineuploadER
  197. // for some reason)
  198. fineuploader: path.join(
  199. __dirname,
  200. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  201. )
  202. }
  203. },
  204. // Split out vendored dependencies that are shared between 2 or more "real
  205. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  206. // that they are de-duplicated from the other bundles. This allows the
  207. // libraries bundle to be independently cached (as it likely will change less
  208. // than the other bundles)
  209. optimization: {
  210. splitChunks: {
  211. cacheGroups: {
  212. libraries: {
  213. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  214. name: 'libraries',
  215. chunks: 'initial',
  216. minChunks: 2
  217. }
  218. }
  219. }
  220. },
  221. plugins: [
  222. // Generate a manifest.json file which is used by the backend to map the
  223. // base filenames to the generated output filenames
  224. new ManifestPlugin({
  225. // Always write the manifest file to disk (even if in dev mode, where
  226. // files are held in memory). This is needed because the server will read
  227. // this file (from disk) when building the script's url
  228. writeToFileEmit: true
  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. new CopyPlugin([
  236. {
  237. from: 'frontend/js/vendor/libs/mathjax',
  238. to: 'js/libs/mathjax'
  239. },
  240. {
  241. from: 'frontend/js/vendor/libs/sigma-master',
  242. to: 'js/libs/sigma-master'
  243. },
  244. {
  245. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  246. to: `js/ace-${PackageVersions.version.ace}/`
  247. },
  248. // Copy CMap files from pdfjs-dist package to build output. These are used
  249. // to provide support for non-Latin characters
  250. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  251. ])
  252. ]
  253. }