webpack.config.js 6.0 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
    common_vue:           ['vue', './vue_shared/common_vue.js'],
22
    common_d3:            ['d3'],
23
    main:                 './main.js',
24
    blob:                 './blob_edit/blob_bundle.js',
25
    boards:               './boards/boards_bundle.js',
P
Phil Hughes 已提交
26
    simulate_drag:        './test_utils/simulate_drag.js',
27
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
28
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
29 30
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
31
    environments_folder:  './environments/folder/environments_folder_bundle.js',
32
    filtered_search:      './filtered_search/filtered_search_bundle.js',
33
    graphs:               './graphs/graphs_bundle.js',
F
Filipa Lacerda 已提交
34
    groups_list:          './groups_list.js',
35
    issuable:             './issuable/issuable_bundle.js',
36 37
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
    merge_request_widget: './merge_request_widget/ci_bundle.js',
38
    monitoring:           './monitoring/monitoring_bundle.js',
39
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
40
    notebook_viewer:      './blob/notebook_viewer.js',
41 42 43 44
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
    snippet:              './snippet/snippet_bundle.js',
    terminal:             './terminal/terminal_bundle.js',
M
Mike Greiling 已提交
45
    u2f:                  ['vendor/u2f'],
46
    users:                './users/users_bundle.js',
M
Mike Greiling 已提交
47
    vue_pipelines:        './vue_pipelines_index/index.js',
48
    raven:                './raven/index.js',
49 50 51 52 53
  },

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

M
Mike Greiling 已提交
57
  devtool: 'inline-source-map',
58

M
Mike Greiling 已提交
59
  module: {
M
Mike Greiling 已提交
60
    rules: [
M
Mike Greiling 已提交
61
      {
62
        test: /\.js$/,
63
        exclude: /(node_modules|vendor\/assets)/,
64
        loader: 'babel-loader'
F
Filipa Lacerda 已提交
65 66 67 68
      },
      {
        test: /\.svg$/,
        use: 'raw-loader'
M
Mike Greiling 已提交
69 70 71 72
      }
    ]
  },

73 74 75 76 77 78 79 80 81
  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 已提交
82
    }),
M
Mike Greiling 已提交
83 84

    // prevent pikaday from including moment.js
P
Phil Hughes 已提交
85
    new webpack.IgnorePlugin(/moment/, /pikaday/),
M
Mike Greiling 已提交
86

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

93 94 95 96
    // use deterministic module ids in all environments
    IS_PRODUCTION ?
      new webpack.HashedModuleIdsPlugin() :
      new webpack.NamedModulesPlugin(),
97

98 99 100 101 102 103 104 105 106 107 108 109
    // create cacheable common library bundle for all vue chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_vue',
      chunks: [
        'boards',
        'commit_pipelines',
        'cycle_analytics',
        'diff_notes',
        'environments',
        'environments_folder',
        'issuable',
        'merge_conflicts',
P
Phil Hughes 已提交
110
        'notebook_viewer',
111 112
        'vue_pipelines',
      ],
113 114 115
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
116 117
    }),

118 119 120
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
121 122 123 124 125
      chunks: [
        'graphs',
        'users',
        'monitoring',
      ],
126 127
    }),

128
    // create cacheable common library bundles
129
    new webpack.optimize.CommonsChunkPlugin({
130
      names: ['main', 'common', 'runtime'],
131
    }),
M
Mike Greiling 已提交
132 133 134
  ],

  resolve: {
135
    extensions: ['.js'],
136
    alias: {
137
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
138
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
139
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
140
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
141
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
142
      'vue$':           'vue/dist/vue.esm.js',
143
    }
M
Mike Greiling 已提交
144
  }
145 146
}

147
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
148
  config.devtool = 'source-map';
149
  config.plugins.push(
150
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
151 152 153 154
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
155
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
156
      sourceMap: true
157 158 159
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
160 161 162
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
M
Mike Greiling 已提交
163
    })
164
  );
165 166 167
}

if (IS_DEV_SERVER) {
168 169
  config.devServer = {
    port: DEV_SERVER_PORT,
170 171
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
172
    inline: DEV_SERVER_LIVERELOAD
173 174 175 176
  };
  config.output.publicPath = '//localhost:' + DEV_SERVER_PORT + config.output.publicPath;
}

177 178 179 180 181 182 183 184 185 186 187 188
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'),
    })
  );
}

189
module.exports = config;