webpack.config.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. {
  40. // Pass application JS files through babel-loader, compiling to ES5
  41. test: /\.js$/,
  42. // Only compile application files (dependencies are in ES5 already)
  43. exclude: /node_modules/,
  44. use: [
  45. {
  46. loader: 'babel-loader',
  47. options: {
  48. // Configure babel-loader to cache compiled output so that
  49. // subsequent compile runs are much faster
  50. cacheDirectory: true
  51. }
  52. }
  53. ]
  54. },
  55. {
  56. // These options are neccesary for handlebars to have access to helper
  57. // methods
  58. test: /\.handlebars$/,
  59. loader: 'handlebars-loader',
  60. options: {
  61. compat: true,
  62. knownHelpersOnly: false,
  63. runtimePath: 'handlebars/runtime'
  64. }
  65. }
  66. ]
  67. },
  68. resolve: {
  69. alias: {
  70. // makes handlebars globally accessible to backbone
  71. handlebars: 'handlebars/dist/handlebars.min.js',
  72. jquery: path.join(__dirname, 'node_modules/jquery/dist/jquery')
  73. }
  74. }
  75. }