webpack.config.js 4.17 KB
Newer Older
1 2 3 4 5 6
'use strict';

var Webpack = require('webpack'),
  NgAnnotatePlugin = require('ng-annotate-webpack-plugin'),
  CopyWebpackPlugin = require('copy-webpack-plugin'),
  CleanWebpackPlugin = require('clean-webpack-plugin'),
7
  ExtractTextPlugin = require('extract-text-webpack-plugin'),
8 9 10 11 12 13 14 15 16 17 18
  path = require('path'),
  glob = require('glob');

var DIST = path.resolve(__dirname, 'target/dist');
var ROOT = __dirname;
var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';

var allModules = glob.sync(ROOT + '/modules/*/module.js').map(function (file) {
  var name = /modules\/([^\/]+)\/module\.js/.exec(file)[1];
  return {
    name: name,
19 20 21
    bundle: name + '/module',
    entry: './' + path.relative(ROOT, file),
    outputPath: name + '/'
22 23 24 25 26
  };
});

var getEntries = function (modules) {
  var entries = {
27
    'core': './core/core.js',
28
    'dependencies.js': ['jquery', 'bootstrap', 'angular', 'angular-resource', 'angular-ui-router']
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  };
  modules.forEach(function (module) {
    entries[module.bundle] = module.entry;
  });
  return entries;
};

var ConcatSource = require('webpack-sources').ConcatSource;
var ModuleConcatPlugin = function (files) {
  this.files = files;
};
ModuleConcatPlugin.prototype.apply = function (compiler) {
  var self = this;
  compiler.plugin('emit', function (compilation, done) {
    self.files.forEach(function (file) {
      var newSource = new ConcatSource();
      Object.keys(compilation.assets).forEach(function (asset) {
        if (file.test.test(asset)) {
          newSource.add(compilation.assets[asset]);
          newSource.add(file.delimiter);
        }
      });
      if (newSource.children.length > 0) {
        compilation.assets[file.filename] = newSource;
      }
    });
    done();
  });
};

module.exports = {
  context: ROOT,
  entry: getEntries(allModules),
  output: {
    path: DIST,
64
    filename: '[name].js'
65 66 67
  },
  resolve: {
    alias: {
68 69 70
      bootstrap: path.resolve(ROOT, 'third-party/bootstrap/js/bootstrap.js'),
      'bootstrap.css': path.resolve(ROOT, 'third-party/bootstrap/css/bootstrap.css'),
      'bootstrap-responsive.css': path.resolve(ROOT, 'third-party/bootstrap/css/bootstrap-responsive.css'),
71
      'googlefonts.css': path.resolve(ROOT, 'third-party/googlefonts/googlefonts.css'),
72 73 74 75 76 77 78
      jolokia: path.resolve(ROOT, 'modules/applications-jmx/third-party/jolokia.js')
    }
  },
  module: {
    preLoaders: [{
      test: /\.js$/,
      loader: 'eslint',
Johannes Edmeier committed
79
      exclude: [/node_modules/, /third-party/]
80 81 82 83
    }],
    loaders: [
      {
        test: /^jolokia$/,
Johannes Edmeier committed
84
        loader: 'imports?$=jquery'
85
      }, {
86 87 88
        test: /\.js$/,
        exclude: [/node_modules/, /third-party/],
        loader: 'ng-annotate'
89
      }, {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        test: /\.tpl\.html$/,
        loader: 'raw'
      }, {
        test: /\.css(\?.*)?$/,
        loader: ExtractTextPlugin.extract('style', 'css?-minimize')
      }, {
        test: /\.(jpg|png|gif|eot|svg|ttf|woff(2)?)(\?.*)?$/,
        include: /\/(third-party|node_modules)\//,
        loader: 'file',
        query: {
          name: 'third-party/[2]',
          regExp: '(third-party|node_modules)/(.+)'
        }
      }, {
        test: /\.(jpg|png|gif)$/,
        include: /\/(core|modules)\//,
        loader: 'file',
        query: {
          name: '[2]',
          regExp: '(core|modules)/(.+)$'
        }
111
      }]
112 113
  },
  plugins: [
114 115 116
    new CleanWebpackPlugin([DIST]),
    new ExtractTextPlugin('[name].css'),
    new Webpack.ProvidePlugin({
117 118
      'window.jQuery': 'jquery'
    }),
119
    new NgAnnotatePlugin({
120 121
      add: true
    }),
122
    new Webpack.optimize.CommonsChunkPlugin({
123 124 125
      name: 'dependencies.js',
      filename: 'dependencies.js'
    }),
126 127 128
    new CopyWebpackPlugin([{
      from: '**/*.html',
      context: 'core/'
129
    }, {
Johannes Edmeier committed
130 131 132 133
      from: '**/*.html',
      context: 'modules/'
    }],
      { ignore: ['*.tpl.html'] })
134 135
  ].concat(!isDevServer ? [] : new ModuleConcatPlugin([
    {
136 137 138
      filename: 'all-modules.js',
      test: /module\.js/,
      delimiter: ';\n'
139 140 141
    }, {
      filename: 'all-modules.css',
      test: /module\.css/,
142
      delimiter: '\n'
143 144
    }
  ])),
145
  devServer: {
146
    proxy: [{
Johannes Edmeier committed
147 148 149
      context: '/api',
      target: 'http://localhost:8080',
      secure: false
150
    }]
151 152 153 154 155
  },
  node: {
    fs: 'empty'
  }
};