webpack.config.js 6.9 KB

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