webpack.config.js 2.1 KB

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