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

17
18
// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
Dan Abramov's avatar
Dan Abramov committed
19
var isInNodeModules = 'node_modules' ===
20
  path.basename(path.resolve(path.join(__dirname, '..', '..')));
21
22
23
24
25
var relativePath = isInNodeModules ? '../../..' : '..';
if (process.argv[2] === '--debug-template') {
  relativePath = '../template';
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
26
var rootNodeModulesPath = path.resolve(__dirname, relativePath, 'node_modules');
27
28
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
Dan Abramov's avatar
Dan Abramov committed
29
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
30
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
31
32
var homepagePath = require(path.resolve(__dirname, relativePath, 'package.json')).homepage;
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
33
34
35
36
if (!publicPath.endsWith('/')) {
  // Prevents incorrect paths in file-loader
  publicPath += '/';
}
37

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