webpack.config.js 8.5 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

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
    group:                './group.js',
F
Filipa Lacerda 已提交
43
    groups_list:          './groups_list.js',
44
    issue_show:           './issue_show/index.js',
P
Phil Hughes 已提交
45
    locale:               './locale/index.js',
46
    main:                 './main.js',
47
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
48
    monitoring:           './monitoring/monitoring_bundle.js',
49
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
50
    notebook_viewer:      './blob/notebook_viewer.js',
S
Sam Rose 已提交
51
    pdf_viewer:           './blob/pdf_viewer.js',
52
    pipelines:            './pipelines/index.js',
53
    pipelines_details:     './pipelines/pipeline_details_bundle.js',
54 55
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
56
    protected_tags:       './protected_tags',
57
    sidebar:              './sidebar/sidebar_bundle.js',
58 59
    schedule_form:        './pipeline_schedules/pipeline_schedule_form_bundle.js',
    schedules_index:      './pipeline_schedules/pipeline_schedules_index_bundle.js',
60
    snippet:              './snippet/snippet_bundle.js',
61
    sketch_viewer:        './blob/sketch_viewer.js',
P
Phil Hughes 已提交
62
    stl_viewer:           './blob/stl_viewer.js',
63
    terminal:             './terminal/terminal_bundle.js',
M
Mike Greiling 已提交
64
    u2f:                  ['vendor/u2f'],
65
    users:                './users/users_bundle.js',
66
    raven:                './raven/index.js',
F
Fatih Acet 已提交
67
    vue_merge_request_widget: './vue_merge_request_widget/index.js',
68
    test:                 './test.js',
69 70 71 72 73
  },

  output: {
    path: path.join(ROOT_PATH, 'public/assets/webpack'),
    publicPath: '/assets/webpack/',
74 75
    filename: IS_PRODUCTION ? '[name].[chunkhash].bundle.js' : '[name].bundle.js',
    chunkFilename: IS_PRODUCTION ? '[name].[chunkhash].chunk.js' : '[name].chunk.js',
76 77
  },

78
  devtool: 'cheap-module-source-map',
79

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

112 113 114 115 116 117 118 119 120
  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 已提交
121
    }),
M
Mike Greiling 已提交
122 123

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

126 127 128 129 130 131
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

132
    // assign deterministic module ids
133
    new webpack.NamedModulesPlugin(),
134
    new NameAllModulesPlugin(),
135

136 137 138 139 140 141 142 143 144 145 146
    // 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('_');
    }),

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

175 176 177
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
178 179 180 181 182
      chunks: [
        'graphs',
        'users',
        'monitoring',
      ],
183 184
    }),

185
    // create cacheable common library bundles
186
    new webpack.optimize.CommonsChunkPlugin({
187
      names: ['main', 'common', 'runtime'],
188
    }),
189 190 191 192 193 194 195 196

    // locale common library
    new webpack.optimize.CommonsChunkPlugin({
      name: 'locale',
      chunks: [
        'cycle_analytics',
      ],
    }),
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 230 231 232 233 234 235

  if (!NO_COMPRESSION) {
    config.plugins.push(
      new CompressionPlugin({
        asset: '[path].gz[query]',
      })
    );
  }
236 237 238
}

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

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

266
module.exports = config;