webpack.config.js 10.8 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;
7
var CopyWebpackPlugin = require('copy-webpack-plugin');
M
Mike Greiling 已提交
8
var CompressionPlugin = require('compression-webpack-plugin');
9
var NameAllModulesPlugin = require('name-all-modules-plugin');
10
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
11
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
12

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

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

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

M
Mike Greiling 已提交
95
  module: {
M
Mike Greiling 已提交
96
    rules: [
M
Mike Greiling 已提交
97
      {
98
        test: /\.js$/,
99
        exclude: /(node_modules|vendor\/assets)/,
M
Mike Greiling 已提交
100
        loader: 'babel-loader',
F
Filipa Lacerda 已提交
101
      },
102 103
      {
        test: /\.vue$/,
M
Mike Greiling 已提交
104
        loader: 'vue-loader',
105
      },
F
Filipa Lacerda 已提交
106 107
      {
        test: /\.svg$/,
M
Mike Greiling 已提交
108 109
        loader: 'raw-loader',
      },
S
Sam Rose 已提交
110
      {
111
        test: /\.(gif|png)$/,
S
Sam Rose 已提交
112
        loader: 'url-loader',
113
        options: { limit: 2048 },
S
Sam Rose 已提交
114
      },
M
Mike Greiling 已提交
115
      {
116
        test: /\.(worker(\.min)?\.js|pdf|bmpr)$/,
S
Sam Rose 已提交
117 118
        exclude: /node_modules/,
        loader: 'file-loader',
119 120 121
        options: {
          name: '[name].[hash].[ext]',
        }
S
Sam Rose 已提交
122
      },
123
      {
124
        test: /locale\/\w+\/(.*)\.js$/,
125 126
        loader: 'exports-loader?locales',
      },
127 128 129 130 131 132 133 134 135 136
      {
        test: /monaco-editor\/\w+\/vs\/loader\.js$/,
        use: [
          { loader: 'exports-loader', options: 'l.global' },
          { loader: 'imports-loader', options: 'l=>{},this=>l,AMDLoader=>this,module=>undefined' },
        ],
      }
    ],

    noParse: [/monaco-editor\/\w+\/vs\//],
M
Mike Greiling 已提交
137 138
  },

139 140 141
  plugins: [
    // manifest filename must match config.webpack.manifest_filename
    // webpack-rails only needs assetsByChunkName to function properly
142 143 144 145 146 147 148 149 150 151 152 153
    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 已提交
154
    }),
M
Mike Greiling 已提交
155 156

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

159 160 161 162 163 164
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

165
    // assign deterministic module ids
166
    new webpack.NamedModulesPlugin(),
167
    new NameAllModulesPlugin(),
168

169 170 171 172 173 174 175 176 177 178
    // 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('_');
    }),
179

180 181 182 183 184 185 186
    // 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 已提交
187
        'deploy_keys',
188 189 190
        'diff_notes',
        'environments',
        'environments_folder',
191
        'filtered_search',
A
Alfredo Sumaran 已提交
192
        'groups',
193
        'issue_show',
194
        'job_details',
195
        'merge_conflicts',
196
        'monitoring',
P
Phil Hughes 已提交
197
        'notebook_viewer',
S
Sam Rose 已提交
198
        'pdf_viewer',
199
        'pipelines',
200
        'pipelines_details',
M
Mike Greiling 已提交
201
        'repo',
202 203 204
        'schedule_form',
        'schedules_index',
        'sidebar',
205
        'vue_merge_request_widget',
206
      ],
207 208 209
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
210 211
    }),

212 213 214
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
215 216 217 218
      chunks: [
        'graphs',
        'monitoring',
      ],
219 220
    }),

221
    // create cacheable common library bundles
222
    new webpack.optimize.CommonsChunkPlugin({
223
      names: ['main', 'locale', 'common', 'webpack_runtime'],
224
    }),
225

M
Mike Greiling 已提交
226 227 228
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),

229
    // copy pre-compiled vendor libraries verbatim
230 231
    new CopyWebpackPlugin([
      {
232 233 234
        from: path.join(ROOT_PATH, `node_modules/monaco-editor/${IS_PRODUCTION ? 'min' : 'dev'}/vs`),
        to: 'monaco-editor/vs',
        transform: function(content, path) {
235
          if (/\.js$/.test(path) && !/worker/i.test(path)) {
236 237 238
            return (
              '(function(){\n' +
              'var define = this.define, require = this.require;\n' +
239
              'window.define = define; window.require = require;\n' +
240 241 242 243 244 245
              content +
              '\n}.call(window.__monaco_context__ || (window.__monaco_context__ = {})));'
            );
          }
          return content;
        }
246 247
      }
    ]),
M
Mike Greiling 已提交
248 249 250
  ],

  resolve: {
251
    extensions: ['.js'],
252
    alias: {
253
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
254
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
255
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
256
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
257
      'images':         path.join(ROOT_PATH, 'app/assets/images'),
258
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
259
      'vue$':           'vue/dist/vue.esm.js',
260
    }
M
Mike Greiling 已提交
261
  }
262 263
}

264
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
265
  config.devtool = 'source-map';
266
  config.plugins.push(
267
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
268 269 270 271
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
272
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
273
      sourceMap: true
274 275 276
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
M
Mike Greiling 已提交
277
    })
278
  );
M
Mike Greiling 已提交
279

280
  // zopfli requires a lot of compute time and is disabled in CI
M
Mike Greiling 已提交
281
  if (!NO_COMPRESSION) {
M
Mike Greiling 已提交
282 283 284 285 286 287
    // 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 已提交
288
  }
289 290 291
}

if (IS_DEV_SERVER) {
292
  config.devtool = 'cheap-module-eval-source-map';
293
  config.devServer = {
294
    host: DEV_SERVER_HOST,
295
    port: DEV_SERVER_PORT,
296
    disableHostCheck: true,
297 298
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
S
Simon Knox 已提交
299
    hot: DEV_SERVER_LIVERELOAD,
300
    inline: DEV_SERVER_LIVERELOAD
301
  };
302 303 304 305
  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 已提交
306 307 308
  if (DEV_SERVER_LIVERELOAD) {
    config.plugins.push(new webpack.HotModuleReplacementPlugin());
  }
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322
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'),
    })
  );
}

323
module.exports = config;