webpack.config.prod.js 4.19 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');
Dan Abramov's avatar
Dan Abramov committed
17

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

Dan Abramov's avatar
Dan Abramov committed
25
module.exports = {
Dan Abramov's avatar
Dan Abramov committed
26
  bail: true,
Dan Abramov's avatar
Dan Abramov committed
27
  devtool: 'source-map',
28
29
30
31
  entry: [
    require.resolve('./polyfills'),
    path.join(paths.appSrc, 'index')
  ],
Dan Abramov's avatar
Dan Abramov committed
32
  output: {
33
    path: paths.appBuild,
34
35
    filename: '[name].[chunkhash:8].js',
    chunkFilename: '[name].[chunkhash:8].chunk.js',
36
    publicPath: publicPath
Dan Abramov's avatar
Dan Abramov committed
37
  },
38
  resolve: {
Dan Abramov's avatar
Dan Abramov committed
39
    extensions: ['', '.js', '.json'],
40
41
42
43
44
45
46
47
48
49
50
    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')
    }
51
  },
52
  resolveLoader: {
53
    root: paths.ownNodeModules,
54
55
    moduleTemplates: ['*-loader']
  },
Dan Abramov's avatar
Dan Abramov committed
56
  module: {
Dan Abramov's avatar
Dan Abramov committed
57
58
59
    preLoaders: [
      {
        test: /\.js$/,
60
        loader: 'eslint',
61
        include: paths.appSrc
Dan Abramov's avatar
Dan Abramov committed
62
63
      }
    ],
Dan Abramov's avatar
Dan Abramov committed
64
    loaders: [
Dan Abramov's avatar
Dan Abramov committed
65
66
      {
        test: /\.js$/,
67
        include: paths.appSrc,
68
        loader: 'babel',
69
        query: require('./babel.prod')
Dan Abramov's avatar
Dan Abramov committed
70
      },
Dan Abramov's avatar
Dan Abramov committed
71
72
      {
        test: /\.css$/,
73
        include: [paths.appSrc, paths.appNodeModules],
74
75
76
77
        // 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
78
79
      },
      {
Dan Abramov's avatar
Dan Abramov committed
80
        test: /\.json$/,
81
        include: [paths.appSrc, paths.appNodeModules],
Dan Abramov's avatar
Dan Abramov committed
82
83
84
85
        loader: 'json'
      },
      {
        test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/,
86
        include: [paths.appSrc, paths.appNodeModules],
Dan Abramov's avatar
Dan Abramov committed
87
        loader: 'file',
88
89
90
        query: {
          name: '[name].[hash:8].[ext]'
        }
Dan Abramov's avatar
Dan Abramov committed
91
92
93
      },
      {
        test: /\.(mp4|webm)$/,
94
        include: [paths.appSrc, paths.appNodeModules],
Dan Abramov's avatar
Dan Abramov committed
95
        loader: 'url?limit=10000'
Dan Abramov's avatar
Dan Abramov committed
96
97
98
      }
    ]
  },
99
  eslint: {
100
101
102
103
    // 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
104
105
106
  },
  postcss: function() {
    return [autoprefixer];
Dan Abramov's avatar
Dan Abramov committed
107
108
  },
  plugins: [
Max's avatar
Max committed
109
110
    new HtmlWebpackPlugin({
      inject: true,
111
112
      template: paths.appHtml,
      favicon: paths.appFavicon,
Dan Abramov's avatar
Dan Abramov committed
113
114
115
116
117
118
119
120
121
122
123
124
      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
125
    }),
Dan Abramov's avatar
Dan Abramov committed
126
127
    new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }),
    new webpack.optimize.OccurrenceOrderPlugin(),
128
    new webpack.optimize.DedupePlugin(),
129
130
131
132
133
134
135
136
137
138
139
140
    new webpack.optimize.UglifyJsPlugin({
      compressor: {
        screw_ie8: true,
        warnings: false
      },
      mangle: {
        screw_ie8: true
      },
      output: {
        comments: false,
        screw_ie8: true
      }
141
    }),
142
    new ExtractTextPlugin('[name].[contenthash:8].css')
Dan Abramov's avatar
Dan Abramov committed
143
144
  ]
};