webpack.config.prod.js 3.8 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
26
27
var relativePath = isInNodeModules ? '../../..' : '..';
if (process.argv[2] === '--debug-template') {
  relativePath = '../template';
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
Dan Abramov's avatar
Dan Abramov committed
28
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
29
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
30
31
var homepagePath = require(path.resolve(__dirname, relativePath, 'package.json')).homepage;
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
32

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