webpack.config.js 8.6 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 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[1].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 22 23 24 25 26 27 28 29
// optional dependency `node-zopfli` is unavailable on CentOS 6
var ZOPFLI_AVAILABLE;
try {
  require.resolve('node-zopfli');
  ZOPFLI_AVAILABLE = true;
} catch(err) {
  ZOPFLI_AVAILABLE = false;
}

30
var config = {
31 32 33 34
  // because sqljs requires fs.
  node: {
    fs: "empty"
  },
35
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
36
  entry: {
37
    balsamiq_viewer:      './blob/balsamiq_viewer.js',
38 39
    blob:                 './blob_edit/blob_bundle.js',
    boards:               './boards/boards_bundle.js',
40
    common:               './commons/index.js',
41
    common_vue:           ['vue', './vue_shared/common_vue.js'],
42
    common_d3:            ['d3'],
43
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
44
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
45
    deploy_keys:          './deploy_keys/index.js',
46 47
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
48
    environments_folder:  './environments/folder/environments_folder_bundle.js',
49
    filtered_search:      './filtered_search/filtered_search_bundle.js',
50
    graphs:               './graphs/graphs_bundle.js',
51
    group:                './group.js',
F
Filipa Lacerda 已提交
52
    groups_list:          './groups_list.js',
53
    issue_show:           './issue_show/index.js',
K
kushalpandya 已提交
54
    integrations:         './integrations',
P
Phil Hughes 已提交
55
    locale:               './locale/index.js',
56
    main:                 './main.js',
57
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
58
    monitoring:           './monitoring/monitoring_bundle.js',
59
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
60
    notebook_viewer:      './blob/notebook_viewer.js',
S
Sam Rose 已提交
61
    pdf_viewer:           './blob/pdf_viewer.js',
62
    pipelines:            './pipelines/index.js',
63
    pipelines_details:     './pipelines/pipeline_details_bundle.js',
64 65
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
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
    users:                './users/users_bundle.js',
76
    raven:                './raven/index.js',
F
Fatih Acet 已提交
77
    vue_merge_request_widget: './vue_merge_request_widget/index.js',
78
    test:                 './test.js',
79 80 81 82 83
  },

  output: {
    path: path.join(ROOT_PATH, 'public/assets/webpack'),
    publicPath: '/assets/webpack/',
84 85
    filename: IS_PRODUCTION ? '[name].[chunkhash].bundle.js' : '[name].bundle.js',
    chunkFilename: IS_PRODUCTION ? '[name].[chunkhash].chunk.js' : '[name].chunk.js',
86 87
  },

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

120 121 122 123 124 125 126 127 128
  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 已提交
129
    }),
M
Mike Greiling 已提交
130 131

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

134 135 136 137 138 139
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

140
    // assign deterministic module ids
141
    new webpack.NamedModulesPlugin(),
142
    new NameAllModulesPlugin(),
143

144 145 146 147 148 149 150 151 152 153 154
    // 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('_');
    }),

155 156 157 158 159 160 161
    // 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 已提交
162
        'deploy_keys',
163 164 165
        'diff_notes',
        'environments',
        'environments_folder',
166
        'filtered_search',
167
        'issue_show',
168
        'merge_conflicts',
P
Phil Hughes 已提交
169
        'notebook_viewer',
S
Sam Rose 已提交
170
        'pdf_viewer',
171
        'pipelines',
172
        'pipelines_details',
173 174 175
        'schedule_form',
        'schedules_index',
        'sidebar',
176
        'vue_merge_request_widget',
177
      ],
178 179 180
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
181 182
    }),

183 184 185
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
186 187 188 189 190
      chunks: [
        'graphs',
        'users',
        'monitoring',
      ],
191 192
    }),

193
    // create cacheable common library bundles
194
    new webpack.optimize.CommonsChunkPlugin({
195
      names: ['main', 'locale', 'common', 'runtime'],
196
    }),
M
Mike Greiling 已提交
197 198 199
  ],

  resolve: {
200
    extensions: ['.js'],
201
    alias: {
202
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
203
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
204
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
205
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
206
      'images':         path.join(ROOT_PATH, 'app/assets/images'),
207
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
208
      'vue$':           'vue/dist/vue.esm.js',
209
    }
M
Mike Greiling 已提交
210
  }
211 212
}

213
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
214
  config.devtool = 'source-map';
215
  config.plugins.push(
216
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
217 218 219 220
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
221
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
222
      sourceMap: true
223 224 225
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
M
Mike Greiling 已提交
226
    })
227
  );
M
Mike Greiling 已提交
228

229
  // zopfli requires a lot of compute time and is disabled in CI
M
Mike Greiling 已提交
230 231 232 233
  if (!NO_COMPRESSION) {
    config.plugins.push(
      new CompressionPlugin({
        asset: '[path].gz[query]',
234
        algorithm: ZOPFLI_AVAILABLE ? 'zopfli' : 'gzip',
M
Mike Greiling 已提交
235 236 237
      })
    );
  }
238 239 240
}

if (IS_DEV_SERVER) {
241
  config.devtool = 'cheap-module-eval-source-map';
242
  config.devServer = {
243
    host: DEV_SERVER_HOST,
244
    port: DEV_SERVER_PORT,
245 246
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
247
    inline: DEV_SERVER_LIVERELOAD
248
  };
249
  config.output.publicPath = '//' + DEV_SERVER_HOST + ':' + DEV_SERVER_PORT + config.output.publicPath;
250 251 252 253
  config.plugins.push(
    // watch node_modules for changes if we encounter a missing module compile error
    new WatchMissingNodeModulesPlugin(path.join(ROOT_PATH, 'node_modules'))
  );
254 255
}

256 257 258 259 260 261 262 263 264 265 266 267
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'),
    })
  );
}

268
module.exports = config;