webpack.config.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fs = require('fs')
  2. const path = require('path')
  3. const MODULES_PATH = path.join(__dirname, '/modules')
  4. // Generate a hash of entry points, including modules
  5. const entryPoints = {}
  6. if (fs.existsSync(MODULES_PATH)) {
  7. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  8. const entryPath = path.join(MODULES_PATH, module, '/public/es/index.js')
  9. if (fs.existsSync(entryPath)) {
  10. acc[module] = entryPath
  11. }
  12. return acc
  13. }, entryPoints)
  14. }
  15. // If no entry points are found, silently exit
  16. if (!Object.keys(entryPoints).length) {
  17. console.warn('No entry points found, exiting')
  18. process.exit(0)
  19. }
  20. module.exports = {
  21. // Defines the "entry point(s)" for the application - i.e. the file which
  22. // bootstraps the application
  23. entry: entryPoints,
  24. // Define where and how the bundle will be output to disk
  25. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  26. // kept in memory for speed
  27. output: {
  28. path: path.join(__dirname, '/public/js/es'),
  29. filename: '[name].js',
  30. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  31. // style code bundles
  32. libraryTarget: 'umd',
  33. // Name the exported variable from output bundle
  34. library: ['Frontend', '[name]']
  35. },
  36. // Define how file types are handled by webpack
  37. module: {
  38. rules: [{
  39. // Pass application JS files through babel-loader, compiling to ES5
  40. test: /\.js$/,
  41. // Only compile application files (dependencies are in ES5 already)
  42. exclude: /node_modules/,
  43. use: [{
  44. loader: 'babel-loader',
  45. options: {
  46. presets: [
  47. 'react',
  48. ['env', { modules: false }]
  49. ],
  50. // Configure babel-loader to cache compiled output so that subsequent
  51. // compile runs are much faster
  52. cacheDirectory: true
  53. }
  54. }]
  55. },
  56. {
  57. // These options are necesary for handlebars to have access to helper
  58. // methods
  59. test: /\.handlebars$/,
  60. loader: "handlebars-loader",
  61. options: {
  62. compat: true,
  63. knownHelpersOnly: false,
  64. runtimePath: 'handlebars/runtime',
  65. }
  66. }]
  67. },
  68. resolve: {
  69. alias: {
  70. // makes handlerbars globally accessible to backbone
  71. handlebars: 'handlebars/dist/handlebars.min.js',
  72. jquery: path.join(__dirname, 'node_modules/jquery/dist/jquery'),
  73. }
  74. },
  75. // TODO
  76. // plugins: {}
  77. }