webpack.config.prod.js 4.32 KB
Newer Older
Christopher Chedeau's avatar
Christopher Chedeau committed
1
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

Dan Abramov's avatar
Dan Abramov committed
10
11
12
13
var path = require('path');
var autoprefixer = require('autoprefixer');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
14
var ExtractTextPlugin = require('extract-text-webpack-plugin');
15
var url = require('url');
16
var paths = require('./paths');
17
var env = require('./env');
Dan Abramov's avatar
Dan Abramov committed
18

19
var homepagePath = require(paths.appPackageJson).homepage;
20
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
21
22
23
24
if (!publicPath.endsWith('/')) {
  // Prevents incorrect paths in file-loader
  publicPath += '/';
}
25

Dan Abramov's avatar
Dan Abramov committed
26
module.exports = {
Dan Abramov's avatar
Dan Abramov committed
27
  bail: true,
Dan Abramov's avatar
Dan Abramov committed
28
  devtool: 'source-map',
29
30
31
32
  entry: [
    require.resolve('./polyfills'),
    path.join(paths.appSrc, 'index')
  ],
Dan Abramov's avatar
Dan Abramov committed
33
  output: {
34
    path: paths.appBuild,
35
36
    filename: 'static/js/[name].[chunkhash:8].js',
    chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
37
    publicPath: publicPath
Dan Abramov's avatar
Dan Abramov committed
38
  },
39
  resolve: {
Dan Abramov's avatar
Dan Abramov committed
40
    extensions: ['', '.js', '.json'],
41
42
43
44
45
46
47
48
49
50
51
    alias: {
      // This `alias` section can be safely removed after ejection.
      // We do this because `babel-runtime` may be inside `react-scripts`,
      // so when `babel-plugin-transform-runtime` imports it, it will not be
      // available to the app directly. This is a temporary solution that lets
      // us ship support for generators. However it is far from ideal, and
      // if we don't have a good solution, we should just make `babel-runtime`
      // a dependency in generated projects.
      // See https://github.com/facebookincubator/create-react-app/issues/255
      'babel-runtime/regenerator': require.resolve('babel-runtime/regenerator')
    }
52
  },
53
  resolveLoader: {
54
    root: paths.ownNodeModules,
55
56
    moduleTemplates: ['*-loader']
  },
Dan Abramov's avatar
Dan Abramov committed
57
  module: {
Dan Abramov's avatar
Dan Abramov committed
58
59
60
    preLoaders: [
      {
        test: /\.js$/,
61
        loader: 'eslint',
62
        include: paths.appSrc
Dan Abramov's avatar
Dan Abramov committed
63
64
      }
    ],
Dan Abramov's avatar
Dan Abramov committed
65
    loaders: [
Dan Abramov's avatar
Dan Abramov committed
66
67
      {
        test: /\.js$/,
68
        include: paths.appSrc,
69
        loader: 'babel',
70
        query: require('./babel.prod')
Dan Abramov's avatar
Dan Abramov committed
71
      },
Dan Abramov's avatar
Dan Abramov committed
72
73
      {
        test: /\.css$/,
74
        include: [paths.appSrc, paths.appNodeModules],
75
76
77
78
        // Disable autoprefixer in css-loader itself:
        // https://github.com/webpack/css-loader/issues/281
        // We already have it thanks to postcss.
        loader: ExtractTextPlugin.extract('style', 'css?-autoprefixer!postcss')
Dan Abramov's avatar
Dan Abramov committed
79
80
      },
      {
Dan Abramov's avatar
Dan Abramov committed
81
        test: /\.json$/,
82
        include: [paths.appSrc, paths.appNodeModules],
Dan Abramov's avatar
Dan Abramov committed
83
84
85
        loader: 'json'
      },
      {
86
        test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
87
        include: [paths.appSrc, paths.appNodeModules],
Dan Abramov's avatar
Dan Abramov committed
88
        loader: 'file',
89
        query: {
90
          name: 'static/media/[name].[hash:8].[ext]'
91
        }
Dan Abramov's avatar
Dan Abramov committed
92
93
      },
      {
94
        test: /\.(mp4|webm)(\?.*)?$/,
95
        include: [paths.appSrc, paths.appNodeModules],
96
97
98
99
100
        loader: 'url',
        query: {
          limit: 10000,
          name: 'static/media/[name].[hash:8].[ext]'
        }
Dan Abramov's avatar
Dan Abramov committed
101
102
103
      }
    ]
  },
104
  eslint: {
105
106
107
108
    // TODO: consider separate config for production,
    // e.g. to enable no-console and no-debugger only in prod.
    configFile: path.join(__dirname, 'eslint.js'),
    useEslintrc: false
109
110
111
  },
  postcss: function() {
    return [autoprefixer];
Dan Abramov's avatar
Dan Abramov committed
112
113
  },
  plugins: [
Max's avatar
Max committed
114
115
    new HtmlWebpackPlugin({
      inject: true,
116
117
      template: paths.appHtml,
      favicon: paths.appFavicon,
Dan Abramov's avatar
Dan Abramov committed
118
119
120
121
122
123
124
125
126
127
128
129
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
      }
Max's avatar
Max committed
130
    }),
131
    new webpack.DefinePlugin(env),
Dan Abramov's avatar
Dan Abramov committed
132
    new webpack.optimize.OccurrenceOrderPlugin(),
133
    new webpack.optimize.DedupePlugin(),
134
    new webpack.optimize.UglifyJsPlugin({
135
      compress: {
136
137
138
139
140
141
142
143
144
145
        screw_ie8: true,
        warnings: false
      },
      mangle: {
        screw_ie8: true
      },
      output: {
        comments: false,
        screw_ie8: true
      }
146
    }),
147
    new ExtractTextPlugin('static/css/[name].[contenthash:8].css')
Dan Abramov's avatar
Dan Abramov committed
148
149
  ]
};