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

P
Phil Hughes 已提交
3
var fs = require('fs');
4 5
var path = require('path');
var webpack = require('webpack');
6
var StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin;
M
Mike Greiling 已提交
7
var CompressionPlugin = require('compression-webpack-plugin');
8
var NameAllModulesPlugin = require('name-all-modules-plugin');
9
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
10
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
11

12
var ROOT_PATH = path.resolve(__dirname, '..');
13
var IS_PRODUCTION = process.env.NODE_ENV === 'production';
14
var IS_DEV_SERVER = process.argv.join(' ').indexOf('webpack-dev-server') !== -1;
15
var DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost';
16
var DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808;
17
var DEV_SERVER_LIVERELOAD = process.env.DEV_SERVER_LIVERELOAD !== 'false';
18
var WEBPACK_REPORT = process.env.WEBPACK_REPORT;
M
Mike Greiling 已提交
19
var NO_COMPRESSION = process.env.NO_COMPRESSION;
20 21

var config = {
22 23 24 25
  // because sqljs requires fs.
  node: {
    fs: "empty"
  },
26
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
27
  entry: {
28
    balsamiq_viewer:      './blob/balsamiq_viewer.js',
29 30
    blob:                 './blob_edit/blob_bundle.js',
    boards:               './boards/boards_bundle.js',
31
    common:               './commons/index.js',
32
    common_vue:           ['vue', './vue_shared/common_vue.js'],
33
    common_d3:            ['d3'],
34
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
35
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
36
    deploy_keys:          './deploy_keys/index.js',
37 38
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
39
    environments_folder:  './environments/folder/environments_folder_bundle.js',
40
    filtered_search:      './filtered_search/filtered_search_bundle.js',
41
    graphs:               './graphs/graphs_bundle.js',
42 43
    graphs_charts:        './graphs/graphs_charts.js',
    graphs_show:          './graphs/graphs_show.js',
44
    group:                './group.js',
A
Alfredo Sumaran 已提交
45
    groups:               './groups/index.js',
F
Filipa Lacerda 已提交
46
    groups_list:          './groups_list.js',
T
Tim Zallmann 已提交
47
    how_to_merge:         './how_to_merge.js',
48
    issue_show:           './issue_show/index.js',
K
kushalpandya 已提交
49
    integrations:         './integrations',
50
    job_details:          './jobs/job_details_bundle.js',
P
Phil Hughes 已提交
51
    locale:               './locale/index.js',
52
    main:                 './main.js',
53
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
54
    monitoring:           './monitoring/monitoring_bundle.js',
55
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
56
    notebook_viewer:      './blob/notebook_viewer.js',
S
Sam Rose 已提交
57
    pdf_viewer:           './blob/pdf_viewer.js',
58
    pipelines:            './pipelines/pipelines_bundle.js',
T
Tim Zallmann 已提交
59
    pipelines_charts:     './pipelines/pipelines_charts.js',
60 61
    pipelines_details:    './pipelines/pipeline_details_bundle.js',
    pipelines_times:      './pipelines/pipelines_times.js',
62
    profile:              './profile/profile_bundle.js',
63
    project_new:          './projects/project_new.js',
K
kushalpandya 已提交
64
    prometheus_metrics:   './prometheus_metrics',
65
    protected_branches:   './protected_branches',
66
    protected_tags:       './protected_tags',
67
    sidebar:              './sidebar/sidebar_bundle.js',
68 69
    schedule_form:        './pipeline_schedules/pipeline_schedule_form_bundle.js',
    schedules_index:      './pipeline_schedules/pipeline_schedules_index_bundle.js',
70
    snippet:              './snippet/snippet_bundle.js',
71
    sketch_viewer:        './blob/sketch_viewer.js',
P
Phil Hughes 已提交
72
    stl_viewer:           './blob/stl_viewer.js',
73
    terminal:             './terminal/terminal_bundle.js',
M
Mike Greiling 已提交
74
    u2f:                  ['vendor/u2f'],
75 76
    ui_development_kit:   './ui_development_kit.js',
    users:                './users/index.js',
77
    raven:                './raven/index.js',
F
Fatih Acet 已提交
78
    vue_merge_request_widget: './vue_merge_request_widget/index.js',
79
    test:                 './test.js',
80
    two_factor_auth:      './two_factor_auth.js',
81
    performance_bar:      './performance_bar.js',
82
    webpack_runtime:      './webpack.js',
83 84 85 86 87
  },

  output: {
    path: path.join(ROOT_PATH, 'public/assets/webpack'),
    publicPath: '/assets/webpack/',
88 89
    filename: IS_PRODUCTION ? '[name].[chunkhash].bundle.js' : '[name].bundle.js',
    chunkFilename: IS_PRODUCTION ? '[name].[chunkhash].chunk.js' : '[name].chunk.js',
90 91
  },

M
Mike Greiling 已提交
92
  module: {
M
Mike Greiling 已提交
93
    rules: [
M
Mike Greiling 已提交
94
      {
95
        test: /\.js$/,
96
        exclude: /(node_modules|vendor\/assets)/,
M
Mike Greiling 已提交
97
        loader: 'babel-loader',
F
Filipa Lacerda 已提交
98
      },
99 100
      {
        test: /\.vue$/,
M
Mike Greiling 已提交
101
        loader: 'vue-loader',
102
      },
F
Filipa Lacerda 已提交
103 104
      {
        test: /\.svg$/,
M
Mike Greiling 已提交
105 106
        loader: 'raw-loader',
      },
S
Sam Rose 已提交
107
      {
108
        test: /\.(gif|png)$/,
S
Sam Rose 已提交
109
        loader: 'url-loader',
110
        options: { limit: 2048 },
S
Sam Rose 已提交
111
      },
M
Mike Greiling 已提交
112
      {
113
        test: /\.(worker(\.min)?\.js|pdf|bmpr)$/,
S
Sam Rose 已提交
114 115
        exclude: /node_modules/,
        loader: 'file-loader',
116 117 118
        options: {
          name: '[name].[hash].[ext]',
        }
S
Sam Rose 已提交
119
      },
120
      {
121
        test: /locale\/\w+\/(.*)\.js$/,
122 123
        loader: 'exports-loader?locales',
      },
M
Mike Greiling 已提交
124 125 126
    ]
  },

127 128 129
  plugins: [
    // manifest filename must match config.webpack.manifest_filename
    // webpack-rails only needs assetsByChunkName to function properly
130 131 132 133 134 135 136 137 138 139 140 141
    new StatsWriterPlugin({
      filename: 'manifest.json',
      transform: function(data, opts) {
        var stats = opts.compiler.getStats().toJson({
          chunkModules: false,
          source: false,
          chunks: false,
          modules: false,
          assets: true
        });
        return JSON.stringify(stats, null, 2);
      }
P
Phil Hughes 已提交
142
    }),
M
Mike Greiling 已提交
143 144

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

147 148 149 150 151 152
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

153
    // assign deterministic module ids
154
    new webpack.NamedModulesPlugin(),
155
    new NameAllModulesPlugin(),
156

157 158 159 160 161 162 163 164 165 166 167
    // assign deterministic chunk ids
    new webpack.NamedChunksPlugin((chunk) => {
      if (chunk.name) {
        return chunk.name;
      }
      return chunk.modules.map((m) => {
        var chunkPath = m.request.split('!').pop();
        return path.relative(m.context, chunkPath);
      }).join('_');
    }),

168 169 170 171 172 173 174
    // create cacheable common library bundle for all vue chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_vue',
      chunks: [
        'boards',
        'commit_pipelines',
        'cycle_analytics',
P
Phil Hughes 已提交
175
        'deploy_keys',
176 177 178
        'diff_notes',
        'environments',
        'environments_folder',
179
        'filtered_search',
A
Alfredo Sumaran 已提交
180
        'groups',
181
        'issue_show',
182
        'job_details',
183
        'merge_conflicts',
184
        'monitoring',
P
Phil Hughes 已提交
185
        'notebook_viewer',
S
Sam Rose 已提交
186
        'pdf_viewer',
187
        'pipelines',
188
        'pipelines_details',
189 190 191
        'schedule_form',
        'schedules_index',
        'sidebar',
192
        'vue_merge_request_widget',
193
      ],
194 195 196
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
197 198
    }),

199 200 201
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
202 203 204 205
      chunks: [
        'graphs',
        'monitoring',
      ],
206 207
    }),

208
    // create cacheable common library bundles
209
    new webpack.optimize.CommonsChunkPlugin({
210
      names: ['main', 'locale', 'common', 'webpack_runtime'],
211
    }),
M
Mike Greiling 已提交
212 213 214
  ],

  resolve: {
215
    extensions: ['.js'],
216
    alias: {
217
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
218
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
219
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
220
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
221
      'images':         path.join(ROOT_PATH, 'app/assets/images'),
222
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
223
      'vue$':           'vue/dist/vue.esm.js',
224
    }
M
Mike Greiling 已提交
225
  }
226 227
}

228
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
229
  config.devtool = 'source-map';
230
  config.plugins.push(
231
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
232 233 234 235
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
236
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
237
      sourceMap: true
238 239 240
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
M
Mike Greiling 已提交
241
    })
242
  );
M
Mike Greiling 已提交
243

244
  // zopfli requires a lot of compute time and is disabled in CI
M
Mike Greiling 已提交
245
  if (!NO_COMPRESSION) {
M
Mike Greiling 已提交
246 247 248 249 250 251
    // gracefully fall back to gzip if `node-zopfli` is unavailable (e.g. in CentOS 6)
    try {
      config.plugins.push(new CompressionPlugin({ algorithm: 'zopfli' }));
    } catch(err) {
      config.plugins.push(new CompressionPlugin({ algorithm: 'gzip' }));
    }
M
Mike Greiling 已提交
252
  }
253 254 255
}

if (IS_DEV_SERVER) {
256
  config.devtool = 'cheap-module-eval-source-map';
257
  config.devServer = {
258
    host: DEV_SERVER_HOST,
259
    port: DEV_SERVER_PORT,
260 261
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
S
Simon Knox 已提交
262
    hot: DEV_SERVER_LIVERELOAD,
263
    inline: DEV_SERVER_LIVERELOAD
264
  };
265 266 267 268
  config.plugins.push(
    // watch node_modules for changes if we encounter a missing module compile error
    new WatchMissingNodeModulesPlugin(path.join(ROOT_PATH, 'node_modules'))
  );
S
Simon Knox 已提交
269 270 271
  if (DEV_SERVER_LIVERELOAD) {
    config.plugins.push(new webpack.HotModuleReplacementPlugin());
  }
272 273
}

274 275 276 277 278 279 280 281 282 283 284 285
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'),
    })
  );
}

286
module.exports = config;