webpack.config.js 7.8 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
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
10

11
var ROOT_PATH = path.resolve(__dirname, '..');
12
var IS_PRODUCTION = process.env.NODE_ENV === 'production';
13
var IS_DEV_SERVER = process.argv[1].indexOf('webpack-dev-server') !== -1;
14
var DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost';
15
var DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808;
16
var DEV_SERVER_LIVERELOAD = process.env.DEV_SERVER_LIVERELOAD !== 'false';
17
var WEBPACK_REPORT = process.env.WEBPACK_REPORT;
18 19

var config = {
20 21 22 23
  // because sqljs requires fs.
  node: {
    fs: "empty"
  },
24
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
25
  entry: {
26 27
    blob:                 './blob_edit/blob_bundle.js',
    boards:               './boards/boards_bundle.js',
28
    common:               './commons/index.js',
29
    common_vue:           ['vue', './vue_shared/common_vue.js'],
30
    common_d3:            ['d3'],
31
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
32
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
33
    deploy_keys:          './deploy_keys/index.js',
34 35
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
36
    environments_folder:  './environments/folder/environments_folder_bundle.js',
37
    filtered_search:      './filtered_search/filtered_search_bundle.js',
38
    graphs:               './graphs/graphs_bundle.js',
39
    group:                './group.js',
F
Filipa Lacerda 已提交
40
    groups_list:          './groups_list.js',
41
    issue_show:           './issue_show/index.js',
P
Phil Hughes 已提交
42
    locale:               './locale/index.js',
43
    main:                 './main.js',
44
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
45
    monitoring:           './monitoring/monitoring_bundle.js',
46
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
47
    notebook_viewer:      './blob/notebook_viewer.js',
S
Sam Rose 已提交
48
    pdf_viewer:           './blob/pdf_viewer.js',
49
    pipelines:            './pipelines/index.js',
J
Jacob Schatz 已提交
50
    balsamiq_viewer:      './blob/balsamiq_viewer.js',
51
    pipelines_graph:      './pipelines/graph_bundle.js',
52 53
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
54
    protected_tags:       './protected_tags',
55
    sidebar:              './sidebar/sidebar_bundle.js',
56 57
    schedule_form:        './pipeline_schedules/pipeline_schedule_form_bundle.js',
    schedules_index:      './pipeline_schedules/pipeline_schedules_index_bundle.js',
58
    snippet:              './snippet/snippet_bundle.js',
59
    sketch_viewer:        './blob/sketch_viewer.js',
P
Phil Hughes 已提交
60
    stl_viewer:           './blob/stl_viewer.js',
61
    terminal:             './terminal/terminal_bundle.js',
M
Mike Greiling 已提交
62
    u2f:                  ['vendor/u2f'],
63
    users:                './users/users_bundle.js',
64
    raven:                './raven/index.js',
F
Fatih Acet 已提交
65
    vue_merge_request_widget: './vue_merge_request_widget/index.js',
66 67 68 69 70
  },

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

74
  devtool: 'cheap-module-source-map',
75

M
Mike Greiling 已提交
76
  module: {
M
Mike Greiling 已提交
77
    rules: [
M
Mike Greiling 已提交
78
      {
79
        test: /\.js$/,
80
        exclude: /(node_modules|vendor\/assets)/,
M
Mike Greiling 已提交
81
        loader: 'babel-loader',
F
Filipa Lacerda 已提交
82
      },
83 84
      {
        test: /\.vue$/,
M
Mike Greiling 已提交
85
        loader: 'vue-loader',
86
      },
F
Filipa Lacerda 已提交
87 88
      {
        test: /\.svg$/,
M
Mike Greiling 已提交
89 90
        loader: 'raw-loader',
      },
S
Sam Rose 已提交
91 92 93 94 95
      {
        test: /\.gif$/,
        loader: 'url-loader',
        query: { mimetype: 'image/gif' },
      },
M
Mike Greiling 已提交
96
      {
97
        test: /\.(worker\.js|pdf|bmpr)$/,
S
Sam Rose 已提交
98 99 100
        exclude: /node_modules/,
        loader: 'file-loader',
      },
101 102 103 104
      {
        test: /locale\/[a-z]+\/(.*)\.js$/,
        loader: 'exports-loader?locales',
      },
M
Mike Greiling 已提交
105 106 107
    ]
  },

108 109 110 111 112 113 114 115 116
  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 已提交
117
    }),
M
Mike Greiling 已提交
118 119

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

122 123 124 125 126 127
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

128 129 130 131
    // use deterministic module ids in all environments
    IS_PRODUCTION ?
      new webpack.HashedModuleIdsPlugin() :
      new webpack.NamedModulesPlugin(),
132

133 134 135 136 137 138 139
    // 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 已提交
140
        'deploy_keys',
141 142 143
        'diff_notes',
        'environments',
        'environments_folder',
144
        'filtered_search',
145
        'issue_show',
146
        'merge_conflicts',
P
Phil Hughes 已提交
147
        'notebook_viewer',
S
Sam Rose 已提交
148
        'pdf_viewer',
149
        'pipelines',
150
        'pipelines_graph',
151 152 153
        'schedule_form',
        'schedules_index',
        'sidebar',
154
      ],
155 156 157
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
158 159
    }),

160 161 162
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
163 164 165 166 167
      chunks: [
        'graphs',
        'users',
        'monitoring',
      ],
168 169
    }),

170
    // create cacheable common library bundles
171
    new webpack.optimize.CommonsChunkPlugin({
172
      names: ['main', 'common', 'runtime'],
173
    }),
174 175 176 177 178 179 180 181

    // locale common library
    new webpack.optimize.CommonsChunkPlugin({
      name: 'locale',
      chunks: [
        'cycle_analytics',
      ],
    }),
M
Mike Greiling 已提交
182 183 184
  ],

  resolve: {
185
    extensions: ['.js'],
186
    alias: {
187
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
188
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
189
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
190
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
191
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
192
      'vue$':           'vue/dist/vue.esm.js',
193
    }
M
Mike Greiling 已提交
194
  }
195 196
}

197
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
198
  config.devtool = 'source-map';
199
  config.plugins.push(
200
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
201 202 203 204
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
205
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
206
      sourceMap: true
207 208 209
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
210 211 212
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
M
Mike Greiling 已提交
213
    })
214
  );
215 216 217
}

if (IS_DEV_SERVER) {
218
  config.devtool = 'cheap-module-eval-source-map';
219
  config.devServer = {
220
    host: DEV_SERVER_HOST,
221
    port: DEV_SERVER_PORT,
222 223
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
224
    inline: DEV_SERVER_LIVERELOAD
225
  };
226
  config.output.publicPath = '//' + DEV_SERVER_HOST + ':' + DEV_SERVER_PORT + config.output.publicPath;
227 228 229 230
  config.plugins.push(
    // watch node_modules for changes if we encounter a missing module compile error
    new WatchMissingNodeModulesPlugin(path.join(ROOT_PATH, 'node_modules'))
  );
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244
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'),
    })
  );
}

245
module.exports = config;