webpack.config.js 6.9 KB

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