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

3
var fs = require('fs');
4 5 6
var path = require('path');
var webpack = require('webpack');
var StatsPlugin = require('stats-webpack-plugin');
7
var CompressionPlugin = require('compression-webpack-plugin');
8
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
9

10
var ROOT_PATH = path.resolve(__dirname, '..');
11
var IS_PRODUCTION = process.env.NODE_ENV === 'production';
12
var IS_DEV_SERVER = process.argv[1].indexOf('webpack-dev-server') !== -1;
13
var DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808;
14
var DEV_SERVER_LIVERELOAD = process.env.DEV_SERVER_LIVERELOAD !== 'false';
15
var WEBPACK_REPORT = process.env.WEBPACK_REPORT;
16 17

var config = {
18
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
19
  entry: {
20
    common:               './commons/index.js',
21 22 23
    application:          './application.js',
    blob_edit:            './blob_edit/blob_edit_bundle.js',
    boards:               './boards/boards_bundle.js',
Phil Hughes's avatar
Phil Hughes committed
24
    simulate_drag:        './test_utils/simulate_drag.js',
25
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
26
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
27 28
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
Filipa Lacerda's avatar
Filipa Lacerda committed
29
    environments_folder:  './environments/folder/environments_folder_bundle.js',
30
    filtered_search:      './filtered_search/filtered_search_bundle.js',
31
    graphs:               './graphs/graphs_bundle.js',
32
    groups_list:          './groups_list.js',
33
    issuable:             './issuable/issuable_bundle.js',
34 35 36 37 38 39 40 41 42
    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',
43
    lib_d3:               './lib/d3.js',
44
    lib_vue:              './lib/vue_resource.js',
Mike Greiling's avatar
Mike Greiling committed
45
    vue_pipelines:        './vue_pipelines_index/index.js',
46 47 48 49 50 51 52 53
  },

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

54
  devtool: 'inline-source-map',
55

56
  module: {
Mike Greiling's avatar
Mike Greiling committed
57
    rules: [
58
      {
59 60
        test: /\.(js|es6)$/,
        exclude: /(node_modules|vendor\/assets)/,
61
        loader: 'babel-loader',
Mike Greiling's avatar
Mike Greiling committed
62 63 64 65 66
        options: {
          presets: [
            ["es2015", {"modules": false}],
            'stage-2'
          ]
67
        }
Filipa Lacerda's avatar
Filipa Lacerda committed
68 69 70 71
      },
      {
        test: /\.svg$/,
        use: 'raw-loader'
72 73 74 75
      }
    ]
  },

76 77 78 79 80 81 82 83 84
  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
Phil Hughes's avatar
Phil Hughes committed
85
    }),
Mike Greiling's avatar
Mike Greiling committed
86 87

    // prevent pikaday from including moment.js
Phil Hughes's avatar
Phil Hughes committed
88
    new webpack.IgnorePlugin(/moment/, /pikaday/),
Mike Greiling's avatar
Mike Greiling committed
89

90 91 92 93 94 95
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

96 97 98 99
    // use deterministic module ids in all environments
    IS_PRODUCTION ?
      new webpack.HashedModuleIdsPlugin() :
      new webpack.NamedModulesPlugin(),
100

101
    // create cacheable common library bundles
102
    new webpack.optimize.CommonsChunkPlugin({
103
      names: ['application', 'common', 'manifest'],
104
    }),
105 106 107
  ],

  resolve: {
Mike Greiling's avatar
Mike Greiling committed
108
    extensions: ['.js', '.es6', '.js.es6'],
109
    alias: {
110
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
111
      'emoji-aliases$': path.join(ROOT_PATH, 'fixtures/emojis/aliases.json'),
112
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
113
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
114
      'vue$':           'vue/dist/vue.common.js',
115
    }
116
  }
117 118
}

119
if (IS_PRODUCTION) {
120
  config.devtool = 'source-map';
121
  config.plugins.push(
122
    new webpack.NoEmitOnErrorsPlugin(),
Mike Greiling's avatar
Mike Greiling committed
123 124 125 126
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
127
    new webpack.optimize.UglifyJsPlugin({
Mike Greiling's avatar
Mike Greiling committed
128
      sourceMap: true
129 130 131
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
132 133 134
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
Mike Greiling's avatar
Mike Greiling committed
135
    })
136
  );
137 138 139
}

if (IS_DEV_SERVER) {
140 141
  config.devServer = {
    port: DEV_SERVER_PORT,
142 143
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
144
    inline: DEV_SERVER_LIVERELOAD
145 146 147 148
  };
  config.output.publicPath = '//localhost:' + DEV_SERVER_PORT + config.output.publicPath;
}

149 150 151 152 153 154 155 156 157 158 159 160
if (WEBPACK_REPORT) {
  config.plugins.push(
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      generateStatsFile: true,
      openAnalyzer: false,
      reportFilename: path.join(ROOT_PATH, 'webpack-report/index.html'),
      statsFilename: path.join(ROOT_PATH, 'webpack-report/stats.json'),
    })
  );
}

161
module.exports = config;