webpack.config.js 4.3 KB
Newer Older
1 2
'use strict';

P
Phil Hughes 已提交
3
var fs = require('fs');
4 5 6
var path = require('path');
var webpack = require('webpack');
var StatsPlugin = require('stats-webpack-plugin');
P
Phil Hughes 已提交
7
var CompressionPlugin = require("compression-webpack-plugin");
8 9

var IS_PRODUCTION = process.env.NODE_ENV === 'production';
10
var IS_DEV_SERVER = process.argv[1].indexOf('webpack-dev-server') !== -1;
11 12 13
var ROOT_PATH = path.resolve(__dirname, '..');

// must match config.webpack.dev_server.port
P
Phil Hughes 已提交
14 15 16 17 18 19 20
var DEV_SERVER_PORT;

try {
  DEV_SERVER_PORT = parseInt(fs.readFileSync('../webpack_port'), 10);
} catch (e) {
  DEV_SERVER_PORT = 3808;
}
21 22

var config = {
23
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
24
  entry: {
25 26 27 28 29 30 31
    application:          './application.js',
    blob_edit:            './blob_edit/blob_edit_bundle.js',
    boards:               './boards/boards_bundle.js',
    boards_test:          './boards/test_utils/simulate_drag.js',
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
32
    filtered_search:      './filtered_search/filtered_search_bundle.js',
33
    graphs:               './graphs/graphs_bundle.js',
34
    issuable:             './issuable/issuable_bundle.js',
35 36 37 38 39 40 41 42 43
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
    merge_request_widget: './merge_request_widget/ci_bundle.js',
    network:              './network/network_bundle.js',
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
    snippet:              './snippet/snippet_bundle.js',
    terminal:             './terminal/terminal_bundle.js',
    users:                './users/users_bundle.js',
    lib_chart:            './lib/chart.js',
44
    lib_d3:               './lib/d3.js',
45
    lib_vue:              './lib/vue_resource.js',
M
Mike Greiling 已提交
46
    vue_pipelines:        './vue_pipelines_index/index.js',
47 48 49 50 51 52 53 54
  },

  output: {
    path: path.join(ROOT_PATH, 'public/assets/webpack'),
    publicPath: '/assets/webpack/',
    filename: IS_PRODUCTION ? '[name]-[chunkhash].js' : '[name].js'
  },

M
Mike Greiling 已提交
55
  devtool: 'inline-source-map',
56

M
Mike Greiling 已提交
57 58 59 60 61
  module: {
    loaders: [
      {
        test: /\.es6$/,
        exclude: /node_modules/,
62 63 64 65 66 67 68
        loader: 'babel-loader',
        query: {
          // "use strict" was broken in sprockets-es6 due to sprockets concatination method.
          // many es5 strict errors which were never caught ended up in our es6 assets as a result.
          // this hack is necessary until they can be fixed.
          blacklist: ["useStrict"]
        }
69 70 71 72
      },
      {
        test: /\.(js|es6)$/,
        loader: 'imports-loader',
73
        query: 'this=>window'
74 75 76 77
      },
      {
        test: /\.json$/,
        loader: 'json-loader'
M
Mike Greiling 已提交
78 79 80 81
      }
    ]
  },

82 83 84 85 86 87 88 89 90
  plugins: [
    // manifest filename must match config.webpack.manifest_filename
    // webpack-rails only needs assetsByChunkName to function properly
    new StatsPlugin('manifest.json', {
      chunkModules: false,
      source: false,
      chunks: false,
      modules: false,
      assets: true
P
Phil Hughes 已提交
91 92 93 94
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
    }),
M
Mike Greiling 已提交
95 96 97
  ],

  resolve: {
98 99
    extensions: ['', '.js', '.es6', '.js.es6'],
    alias: {
100
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
101 102 103 104 105
      'bootstrap/js':   'bootstrap-sass/assets/javascripts/bootstrap',
      'emoji-aliases$': path.join(ROOT_PATH, 'fixtures/emojis/aliases.json'),
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
      'vue$':           'vue/dist/vue.js',
      'vue-resource$':  'vue-resource/dist/vue-resource.js'
106
    }
M
Mike Greiling 已提交
107
  }
108 109
}

110
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
111
  config.devtool = 'source-map';
112 113 114 115 116 117 118 119 120 121 122
  config.plugins.push(
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: { warnings: false }
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
    }),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurrenceOrderPlugin()
  );
123 124 125
}

if (IS_DEV_SERVER) {
126 127 128 129 130 131 132 133
  config.devServer = {
    port: DEV_SERVER_PORT,
    headers: { 'Access-Control-Allow-Origin': '*' }
  };
  config.output.publicPath = '//localhost:' + DEV_SERVER_PORT + config.output.publicPath;
}

module.exports = config;