webpack.config.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 PackageVersions = require('./app/src/infrastructure/PackageVersions')
  7. const MODULES_PATH = path.join(__dirname, '/modules')
  8. // Generate a hash of entry points, including modules
  9. const entryPoints = {
  10. main: './frontend/js/main.js',
  11. ide: './frontend/js/ide.js'
  12. }
  13. // Attempt to load frontend entry-points from modules, if they exist
  14. if (fs.existsSync(MODULES_PATH)) {
  15. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  16. const entryPath = path.join(MODULES_PATH, module, '/frontend/js/index.js')
  17. if (fs.existsSync(entryPath)) {
  18. acc[module] = entryPath
  19. }
  20. return acc
  21. }, entryPoints)
  22. }
  23. module.exports = {
  24. // Defines the "entry point(s)" for the application - i.e. the file which
  25. // bootstraps the application
  26. entry: entryPoints,
  27. // Define where and how the bundle will be output to disk
  28. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  29. // kept in memory for speed
  30. output: {
  31. path: path.join(__dirname, '/public/js'),
  32. // Serve from /js
  33. publicPath: '/js/',
  34. filename: '[name].js',
  35. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  36. // style code bundles
  37. libraryTarget: 'umd',
  38. // Name the exported variable from output bundle
  39. library: ['Frontend', '[name]']
  40. },
  41. // Define how file types are handled by webpack
  42. module: {
  43. rules: [
  44. {
  45. // Pass application JS files through babel-loader, compiling to ES5
  46. test: /\.js$/,
  47. // Only compile application files (dependencies are in ES5 already)
  48. exclude: /node_modules/,
  49. use: [
  50. {
  51. loader: 'babel-loader',
  52. options: {
  53. // Configure babel-loader to cache compiled output so that
  54. // subsequent compile runs are much faster
  55. cacheDirectory: true
  56. }
  57. }
  58. ]
  59. },
  60. {
  61. // These options are necessary for handlebars to have access to helper
  62. // methods
  63. test: /\.handlebars$/,
  64. loader: 'handlebars-loader',
  65. options: {
  66. compat: true,
  67. knownHelpersOnly: false,
  68. runtimePath: 'handlebars/runtime'
  69. }
  70. },
  71. // Allow for injection of modules dependencies by reading contents of
  72. // modules directory and adding necessary dependencies
  73. {
  74. test: path.join(__dirname, 'modules/modules-main.js'),
  75. use: [
  76. {
  77. loader: 'val-loader'
  78. }
  79. ]
  80. },
  81. {
  82. test: path.join(__dirname, 'modules/modules-ide.js'),
  83. use: [
  84. {
  85. loader: 'val-loader'
  86. }
  87. ]
  88. },
  89. {
  90. // Expose underscore global variable
  91. test: path.join(
  92. __dirname,
  93. `frontend/js/vendor/libs/${PackageVersions.lib('underscore')}.js`
  94. ),
  95. use: [
  96. {
  97. loader: 'expose-loader',
  98. options: '_'
  99. }
  100. ]
  101. },
  102. {
  103. // Expose Algolia global variable
  104. test: path.join(
  105. __dirname,
  106. `frontend/js/vendor/libs/${PackageVersions.lib('algolia')}.js`
  107. ),
  108. use: [
  109. {
  110. loader: 'expose-loader',
  111. options: 'AlgoliaSearch'
  112. }
  113. ]
  114. }
  115. ]
  116. },
  117. resolve: {
  118. alias: {
  119. // Aliases for AMD modules
  120. // Vendored dependencies in public/src/vendor/libs (e.g. angular)
  121. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  122. // Use vendored moment (with correct version)
  123. moment: path.join(
  124. __dirname,
  125. `frontend/js/vendor/libs/${PackageVersions.lib('moment')}`
  126. ),
  127. // Enables ace/ace shortcut
  128. ace: path.join(
  129. __dirname,
  130. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  131. ),
  132. // fineupload vendored dependency (which we're aliasing to fineuploadER
  133. // for some reason)
  134. fineuploader: path.join(
  135. __dirname,
  136. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  137. )
  138. },
  139. // Define what can be imported with out an absolute or relative path. This
  140. // is because we need to override the default (which is just node_modules)
  141. // to get AMD modules in public/src to work as they do not use relative/
  142. // absolute paths for dependencies
  143. modules: ['frontend/js', 'node_modules']
  144. },
  145. // Split out vendored dependencies that are shared between 2 or more "real
  146. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  147. // that they are de-duplicated from the other bundles. This allows the
  148. // libraries bundle to be independently cached (as it likely will change less
  149. // than the other bundles)
  150. optimization: {
  151. splitChunks: {
  152. cacheGroups: {
  153. libraries: {
  154. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  155. name: 'libraries',
  156. chunks: 'initial',
  157. minChunks: 2
  158. }
  159. }
  160. }
  161. },
  162. plugins: [
  163. // Generate a manifest.json file which is used by the backend to map the
  164. // base filenames to the generated output filenames
  165. new ManifestPlugin({
  166. // Always write the manifest file to disk (even if in dev mode, where
  167. // files are held in memory). This is needed because the server will read
  168. // this file (from disk) when building the script's url
  169. writeToFileEmit: true
  170. }),
  171. // Silence warning when loading moment from vendored dependencies as it
  172. // attempts to load locales.js file which does not exist (but this is fine
  173. // as we don't want to load the large amount of locale data from moment)
  174. new webpack.IgnorePlugin(/^\.\/locale$/, /frontend\/js\/vendor\/libs/),
  175. new CopyPlugin([
  176. {
  177. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js',
  178. to: 'libs/angular-1.6.4.min.js'
  179. },
  180. {
  181. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js.map',
  182. to: 'libs/angular-1.6.4.min.js.map'
  183. },
  184. {
  185. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js',
  186. to: 'libs/jquery-1.11.1.min.js'
  187. },
  188. {
  189. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js.map',
  190. to: 'libs/jquery-1.11.1.min.js.map'
  191. },
  192. {
  193. from: 'frontend/js/vendor/libs/mathjax',
  194. to: 'libs/mathjax'
  195. },
  196. {
  197. from: 'frontend/js/vendor/libs/sigma-master',
  198. to: 'libs/sigma-master'
  199. },
  200. {
  201. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  202. to: `ace-${PackageVersions.version.ace}/`
  203. },
  204. // Copy CMap files from pdfjs-dist package to build output. These are used
  205. // to provide support for non-Latin characters
  206. { from: 'node_modules/pdfjs-dist/cmaps', to: 'cmaps' }
  207. ])
  208. ],
  209. // If jquery or underscore is required by another dependency *don't* include
  210. // in the bundle and use the relevant global variable instead
  211. externals: {
  212. jquery: '$',
  213. underscore: '_'
  214. }
  215. }