build.js 2.67 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
var chalk = require('chalk');
13
var fs = require('fs');
14
var path = require('path');
15
var filesize = require('filesize');
16
var gzipSize = require('gzip-size').sync;
17
var rimrafSync = require('rimraf').sync;
18
var webpack = require('webpack');
19
var config = require('../config/webpack.config.prod');
20
var paths = require('../config/paths');
21

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

26
console.log('Creating an optimized production build...');
27
28
webpack(config).run(function(err, stats) {
  if (err) {
Dan Abramov's avatar
Dan Abramov committed
29
    console.error('Failed to create a production build. Reason:');
30
    console.error(err.message || err);
31
32
33
    process.exit(1);
  }

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  console.log(chalk.green('Compiled successfully.'));
  console.log();

  console.log('File sizes after gzip:');
  console.log();
  var assets = stats.toJson().assets
    .filter(asset => /\.(js|css)$/.test(asset.name))
    .map(asset => {
      var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name);
      return {
        name: asset.name,
        size: gzipSize(fileContents)
      };
    });
  assets.sort((a, b) => b.size - a.size);
  assets.forEach(asset => {
    console.log(
      '  ' + chalk.dim('build' + path.sep) + chalk.cyan(asset.name) + ': ' +
      chalk.green(filesize(asset.size))
    );
  });
  console.log();

Dan Abramov's avatar
Dan Abramov committed
57
  var openCommand = process.platform === 'win32' ? 'start' : 'open';
58
  var homepagePath = require(paths.appPackageJson).homepage;
59
  if (homepagePath) {
60
    console.log('You can now publish them at ' + homepagePath + '.');
61
62
    console.log('For example, if you use GitHub Pages:');
    console.log();
63
    console.log('  git commit -am "Save local changes"');
64
65
66
    console.log('  git checkout -B gh-pages');
    console.log('  git add -f build');
    console.log('  git commit -am "Rebuild website"');
67
68
    console.log('  git filter-branch -f --prune-empty --subdirectory-filter build');
    console.log('  git push -f origin gh-pages');
69
70
    console.log('  git checkout -');
    console.log();
71
  } else {
72
    console.log('You can now serve them with any static server.');
73
74
    console.log('For example:');
    console.log();
75
76
77
    console.log('  npm install -g pushstate-server');
    console.log('  pushstate-server build');
    console.log('  ' + openCommand + ' http://localhost:9000');
78
    console.log();
79
  }
80
  console.log();
81
});