webpack.config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/js/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/js/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/js/libs (e.g. angular)
  118. libs: path.join(__dirname, 'public/js/libs'),
  119. // Use vendored moment (with correct version)
  120. moment: path.join(
  121. __dirname,
  122. `public/js/libs/${PackageVersions.lib('moment')}`
  123. ),
  124. // Enables ace/ace shortcut
  125. ace: path.join(__dirname, `public/js/${PackageVersions.lib('ace')}`),
  126. // fineupload vendored dependency (which we're aliasing to fineuploadER
  127. // for some reason)
  128. fineuploader: path.join(
  129. __dirname,
  130. `public/js/libs/${PackageVersions.lib('fineuploader')}`
  131. )
  132. },
  133. // Define what can be imported with out an absolute or relative path. This
  134. // is because we need to override the default (which is just node_modules)
  135. // to get AMD modules in public/src to work as they do not use relative/
  136. // absolute paths for dependencies
  137. modules: [path.join(__dirname, 'public/src'), 'node_modules']
  138. },
  139. // Split out vendored dependencies that are shared between 2 or more "real
  140. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  141. // that they are de-duplicated from the other bundles. This allows the
  142. // libraries bundle to be independently cached (as it likely will change less
  143. // than the other bundles)
  144. optimization: {
  145. splitChunks: {
  146. cacheGroups: {
  147. libraries: {
  148. test: /[\\/]node_modules[\\/]|[\\/]public[\\/]js[\\/]libs[\\/]/,
  149. name: 'libraries',
  150. chunks: 'initial',
  151. minChunks: 2
  152. }
  153. }
  154. }
  155. },
  156. plugins: [
  157. // Generate a manifest.json file which is used by the backend to map the
  158. // base filenames to the generated output filenames
  159. new ManifestPlugin({
  160. // Always write the manifest file to disk (even if in dev mode, where
  161. // files are held in memory). This is needed because the server will read
  162. // this file (from disk) when building the script's url
  163. writeToFileEmit: true
  164. }),
  165. // Silence warning when loading moment from vendored dependencies as it
  166. // attempts to load locales.js file which does not exist (but this is fine
  167. // as we don't want to load the large amount of locale data from moment)
  168. new webpack.IgnorePlugin(/^\.\/locale$/, /public\/js\/libs/),
  169. // Copy CMap files from pdfjs-dist package to build output. These are used
  170. // to provide support for non-Latin characters
  171. new CopyPlugin([{ from: 'node_modules/pdfjs-dist/cmaps', to: 'cmaps' }])
  172. ],
  173. // If jquery or underscore is required by another dependency *don't* include
  174. // in the bundle and use the relevant global variable instead
  175. externals: {
  176. jquery: '$',
  177. underscore: '_'
  178. }
  179. }