webpack.config.js 5.2 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');
M
Mike Greiling 已提交
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',
P
Phil Hughes 已提交
24
    simulate_drag:        './test_utils/simulate_drag.js',
25
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
26
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
27 28
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
29
    environments_folder:  './environments/folder/environments_folder_bundle.js',
30
    filtered_search:      './filtered_search/filtered_search_bundle.js',
31
    graphs:               './graphs/graphs_bundle.js',
F
Filipa Lacerda 已提交
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',
M
Mike Greiling 已提交
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'
  },

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

M
Mike Greiling 已提交
56
  module: {
M
Mike Greiling 已提交
57
    rules: [
M
Mike Greiling 已提交
58
      {
59 60
        test: /\.(js|es6)$/,
        exclude: /(node_modules|vendor\/assets)/,
61
        loader: 'babel-loader',
M
Mike Greiling 已提交
62 63 64 65 66
        options: {
          presets: [
            ["es2015", {"modules": false}],
            'stage-2'
          ]
67
        }
F
Filipa Lacerda 已提交
68 69 70 71
      },
      {
        test: /\.svg$/,
        use: 'raw-loader'
M
Mike Greiling 已提交
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
P
Phil Hughes 已提交
85
    }),
M
Mike Greiling 已提交
86 87

    // prevent pikaday from including moment.js
P
Phil Hughes 已提交
88
    new webpack.IgnorePlugin(/moment/, /pikaday/),
M
Mike Greiling 已提交
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 102 103 104 105

    // create a common.js bundle to be loaded on every page
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common',
      minChunks: Infinity,
    }),
M
Mike Greiling 已提交
106 107 108
  ],

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

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

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

150 151 152 153 154 155 156 157 158 159 160 161
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'),
    })
  );
}

162
module.exports = config;