webpack.config.js 4.16 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 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
    'dependencies.js': ['es5-shim/es5-shim', 'es5-shim/es5-sham', 'jquery', 'bootstrap', 'angular', 'angular-resource', 'angular-ui-router']
  };
  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 79 80 81 82 83 84 85
      jolokia: path.resolve(ROOT, 'modules/applications-jmx/third-party/jolokia.js')
    }
  },
  module: {
    preLoaders: [{
      test: /\.js$/,
      loader: 'eslint',
      exclude: [/node_modules/, /third-party/],
        }],
    loaders: [{
      test: /\.js$/,
      exclude: [/node_modules/, /third-party/],
      loader: 'ng-annotate'
        }, {
86
      test: /\.tpl\.html$/,
87
      loader: 'raw'
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        }, {
      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)/(.+)$'
      }
      }]
108 109
  },
  plugins: [
110 111 112
    new CleanWebpackPlugin([DIST]),
    new ExtractTextPlugin('[name].css'),
    new Webpack.ProvidePlugin({
113 114 115 116
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }),
117
    new NgAnnotatePlugin({
118 119
      add: true
    }),
120
    new Webpack.optimize.CommonsChunkPlugin({
121 122 123
      name: 'dependencies.js',
      filename: 'dependencies.js'
    }),
124 125 126 127 128 129 130 131 132 133
    new CopyWebpackPlugin([{
      from: '**/*.html',
      context: 'core/'
      }, {
      from: '**/*.html',
      context: 'modules/'
      }], {
      ignore: ['*.tpl.html']
    })
    ].concat(!isDevServer ? [] : new ModuleConcatPlugin([{
134 135 136
      filename: 'all-modules.js',
      test: /module\.js/,
      delimiter: ';\n'
137 138 139
    }, {
      filename: 'all-modules.css',
      test: /module\.css/,
140
      delimiter: '\n'
141
      }
142 143 144 145 146 147 148 149 150 151 152 153 154
    ])),
  devServer: {
    proxy: {
      '/api*': {
        target: 'http://localhost:8080',
        secure: false
      }
    }
  },
  node: {
    fs: 'empty'
  }
};