build.js 2.5 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.
 */

10
11
process.env.NODE_ENV = 'production';

12
13
14
var fs = require('fs');
var filesize = require('filesize');
var gzipSize = require('gzip-size');
15
var rimrafSync = require('rimraf').sync;
16
var webpack = require('webpack');
17
var config = require('../config/webpack.config.prod');
18
var paths = require('../config/paths');
19

20
21
22
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
rimrafSync(paths.appBuild + '/*');
23

24
25
26
27
28
29
30
31
32
33
function logBuildSize(assets, extension) {
  for (var i = 0; i < assets.length; i++) {
    var asset = assets[i];
    if (asset.name.endsWith('.' + extension)) {
      var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name);
      console.log('Size (gzipped) of ' + asset.name + ': ' + filesize(gzipSize.sync(fileContents)));
    }
  }
}

34
35
webpack(config).run(function(err, stats) {
  if (err) {
Dan Abramov's avatar
Dan Abramov committed
36
    console.error('Failed to create a production build. Reason:');
37
    console.error(err.message || err);
38
39
40
    process.exit(1);
  }

Dan Abramov's avatar
Dan Abramov committed
41
  var openCommand = process.platform === 'win32' ? 'start' : 'open';
42
  var homepagePath = require(paths.appPackageJson).homepage;
Dan Abramov's avatar
Dan Abramov committed
43
  console.log('Successfully generated a bundle in the build folder!');
44
  if (homepagePath) {
45
46
47
    console.log('You can now deploy it to ' + homepagePath + '.');
    console.log('For example, if you use GitHub Pages:');
    console.log();
48
    console.log('  git commit -am "Save local changes"');
49
50
51
    console.log('  git checkout -B gh-pages');
    console.log('  git add -f build');
    console.log('  git commit -am "Rebuild website"');
52
53
    console.log('  git filter-branch -f --prune-empty --subdirectory-filter build');
    console.log('  git push -f origin gh-pages');
54
55
    console.log('  git checkout -');
    console.log();
56
  } else {
57
58
59
    console.log('You can now serve it with any static server.');
    console.log('For example:');
    console.log();
60
61
62
    console.log('  npm install -g pushstate-server');
    console.log('  pushstate-server build');
    console.log('  ' + openCommand + ' http://localhost:9000');
63
    console.log();
64
65
66
    var assets = stats.toJson()['assets'];
    logBuildSize(assets, 'js');
    logBuildSize(assets, 'css');
67
  }
Dan Abramov's avatar
Dan Abramov committed
68
  console.log('The bundle is optimized and ready to be deployed to production.');
69
});