webpack.config.js 6.9 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;
L
Luke "Jared" Bennett 已提交
18
var rewirePlugin = IS_PRODUCTION ? '?plugins=rewire' : '';
19 20

var config = {
21
  context: path.join(ROOT_PATH, 'app/assets/javascripts'),
22
  entry: {
23
    common:               './commons/index.js',
24
    common_vue:           ['vue', './vue_shared/common_vue.js'],
25
    common_d3:            ['d3'],
26
    main:                 './main.js',
27
    blob:                 './blob_edit/blob_bundle.js',
28 29
    boards:               './boards/boards_bundle.js',
    cycle_analytics:      './cycle_analytics/cycle_analytics_bundle.js',
F
Filipa Lacerda 已提交
30
    commit_pipelines:     './commit/pipelines/pipelines_bundle.js',
31 32
    diff_notes:           './diff_notes/diff_notes_bundle.js',
    environments:         './environments/environments_bundle.js',
F
Filipa Lacerda 已提交
33
    environments_folder:  './environments/folder/environments_folder_bundle.js',
34
    filtered_search:      './filtered_search/filtered_search_bundle.js',
35
    graphs:               './graphs/graphs_bundle.js',
F
Filipa Lacerda 已提交
36
    groups_list:          './groups_list.js',
37
    issuable:             './issuable/issuable_bundle.js',
38 39
    merge_conflicts:      './merge_conflicts/merge_conflicts_bundle.js',
    merge_request_widget: './merge_request_widget/ci_bundle.js',
40
    monitoring:           './monitoring/monitoring_bundle.js',
41
    network:              './network/network_bundle.js',
P
Phil Hughes 已提交
42
    notebook_viewer:      './blob/notebook_viewer.js',
J
Jacob Schatz 已提交
43
    sketch_viewer:        './blob/sketch_viewer.js',
S
Sam Rose 已提交
44
    pdf_viewer:           './blob/pdf_viewer.js',
45 46
    profile:              './profile/profile_bundle.js',
    protected_branches:   './protected_branches/protected_branches_bundle.js',
47
    protected_tags:       './protected_tags',
48
    snippet:              './snippet/snippet_bundle.js',
P
Phil Hughes 已提交
49
    stl_viewer:           './blob/stl_viewer.js',
50
    terminal:             './terminal/terminal_bundle.js',
M
Mike Greiling 已提交
51
    u2f:                  ['vendor/u2f'],
52
    users:                './users/users_bundle.js',
M
Mike Greiling 已提交
53
    vue_pipelines:        './vue_pipelines_index/index.js',
54
    raven:                './raven/index.js',
R
Regis Boudinot 已提交
55
    issue_show:           './issue_show/index.js',
L
Luke "Jared" Bennett 已提交
56
    group:                './group.js',
57 58 59 60 61
  },

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

65
  devtool: 'cheap-module-source-map',
66

M
Mike Greiling 已提交
67
  module: {
M
Mike Greiling 已提交
68
    rules: [
M
Mike Greiling 已提交
69
      {
70
        test: /\.js$/,
71
        exclude: /(node_modules|vendor\/assets)/,
L
Luke "Jared" Bennett 已提交
72
        loader: `babel-loader${rewirePlugin}`,
F
Filipa Lacerda 已提交
73
      },
74 75
      {
        test: /\.vue$/,
M
Mike Greiling 已提交
76
        loader: 'vue-loader',
F
Filipa Lacerda 已提交
77 78 79
      },
      {
        test: /\.svg$/,
M
Mike Greiling 已提交
80 81 82 83
        loader: 'raw-loader',
      },
      {
        test: /\.(worker\.js|pdf)$/,
S
Sam Rose 已提交
84 85 86
        exclude: /node_modules/,
        loader: 'file-loader',
      },
M
Mike Greiling 已提交
87 88 89
    ]
  },

90 91 92 93 94 95 96 97 98
  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 已提交
99
    }),
M
Mike Greiling 已提交
100 101

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

104 105 106 107 108 109
    // fix legacy jQuery plugins which depend on globals
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

110 111 112 113
    // use deterministic module ids in all environments
    IS_PRODUCTION ?
      new webpack.HashedModuleIdsPlugin() :
      new webpack.NamedModulesPlugin(),
114

115 116 117 118 119 120 121 122 123 124 125 126
    // create cacheable common library bundle for all vue chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_vue',
      chunks: [
        'boards',
        'commit_pipelines',
        'cycle_analytics',
        'diff_notes',
        'environments',
        'environments_folder',
        'issuable',
        'merge_conflicts',
P
Phil Hughes 已提交
127
        'notebook_viewer',
S
Sam Rose 已提交
128
        'pdf_viewer',
129 130
        'vue_pipelines',
      ],
131 132 133
      minChunks: function(module, count) {
        return module.resource && (/vue_shared/).test(module.resource);
      },
134 135
    }),

136 137 138
    // create cacheable common library bundle for all d3 chunks
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common_d3',
139 140 141 142 143
      chunks: [
        'graphs',
        'users',
        'monitoring',
      ],
144 145
    }),

146
    // create cacheable common library bundles
147
    new webpack.optimize.CommonsChunkPlugin({
148
      names: ['main', 'common', 'runtime'],
149
    }),
M
Mike Greiling 已提交
150 151 152
  ],

  resolve: {
153
    extensions: ['.js'],
154
    alias: {
155
      '~':              path.join(ROOT_PATH, 'app/assets/javascripts'),
156
      'emojis':         path.join(ROOT_PATH, 'fixtures/emojis'),
157
      'empty_states':   path.join(ROOT_PATH, 'app/views/shared/empty_states'),
158
      'icons':          path.join(ROOT_PATH, 'app/views/shared/icons'),
159
      'vendor':         path.join(ROOT_PATH, 'vendor/assets/javascripts'),
160
      'vue$':           'vue/dist/vue.esm.js',
161
    }
M
Mike Greiling 已提交
162
  }
163 164
}

165
if (IS_PRODUCTION) {
M
Mike Greiling 已提交
166
  config.devtool = 'source-map';
167
  config.plugins.push(
168
    new webpack.NoEmitOnErrorsPlugin(),
M
Mike Greiling 已提交
169 170 171 172
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
173
    new webpack.optimize.UglifyJsPlugin({
M
Mike Greiling 已提交
174
      sourceMap: true
175 176 177
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify('production') }
178 179 180
    }),
    new CompressionPlugin({
      asset: '[path].gz[query]',
M
Mike Greiling 已提交
181
    })
182
  );
183 184 185
}

if (IS_DEV_SERVER) {
186
  config.devtool = 'cheap-module-eval-source-map';
187
  config.devServer = {
188
    host: DEV_SERVER_HOST,
189
    port: DEV_SERVER_PORT,
190 191
    headers: { 'Access-Control-Allow-Origin': '*' },
    stats: 'errors-only',
192
    inline: DEV_SERVER_LIVERELOAD
193
  };
194
  config.output.publicPath = '//' + DEV_SERVER_HOST + ':' + DEV_SERVER_PORT + config.output.publicPath;
195 196 197 198
  config.plugins.push(
    // watch node_modules for changes if we encounter a missing module compile error
    new WatchMissingNodeModulesPlugin(path.join(ROOT_PATH, 'node_modules'))
  );
199 200
}

201 202 203 204 205 206 207 208 209 210 211 212
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'),
    })
  );
}

213
module.exports = config;