webpack.config.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const fs = require('fs')
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const MODULES_PATH = path.join(__dirname, '/modules')
  5. const webpackENV = process.env.WEBPACK_ENV || 'development'
  6. // Generate a hash of entry points, including modules
  7. const entryPoints = {}
  8. if (fs.existsSync(MODULES_PATH)) {
  9. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  10. const entryPath = path.join(MODULES_PATH, module, '/public/es/index.js')
  11. if (fs.existsSync(entryPath)) {
  12. acc[module] = entryPath
  13. }
  14. return acc
  15. }, entryPoints)
  16. }
  17. // If no entry points are found, silently exit
  18. if (!Object.keys(entryPoints).length) {
  19. console.warn('No entry points found, exiting')
  20. process.exit(0)
  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/es'),
  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. // Pass application JS files through babel-loader, compiling to ES5
  42. test: /\.js$/,
  43. // Only compile application files (dependencies are in ES5 already)
  44. exclude: /node_modules/,
  45. use: [{
  46. loader: 'babel-loader',
  47. options: {
  48. // Configure babel-loader to cache compiled output so that subsequent
  49. // compile runs are much faster
  50. cacheDirectory: true
  51. }
  52. }]
  53. },
  54. {
  55. // These options are necesary for handlebars to have access to helper
  56. // methods
  57. test: /\.handlebars$/,
  58. loader: "handlebars-loader",
  59. options: {
  60. compat: true,
  61. knownHelpersOnly: false,
  62. runtimePath: 'handlebars/runtime',
  63. }
  64. }]
  65. },
  66. resolve: {
  67. alias: {
  68. // makes handlerbars globally accessible to backbone
  69. handlebars: 'handlebars/dist/handlebars.min.js',
  70. jquery: path.join(__dirname, 'node_modules/jquery/dist/jquery'),
  71. }
  72. },
  73. plugins: [
  74. new webpack.DefinePlugin({
  75. // Swaps out checks for NODE_ENV with the env. This is used by various
  76. // libs to enable dev-only features. These checks then become something
  77. // like `if ('production' == 'production')`. Minification will then strip
  78. // the dev-only code from the bundle
  79. 'process.env': {
  80. NODE_ENV: JSON.stringify(webpackENV)
  81. },
  82. })
  83. ]
  84. }