build.js 3.09 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
  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);
43
      var size = gzipSize(fileContents);
44
      return {
45
46
47
48
        folder: path.join('build', path.dirname(asset.name)),
        name: path.basename(asset.name),
        size: size,
        sizeLabel: filesize(size)
49
50
51
      };
    });
  assets.sort((a, b) => b.size - a.size);
52
53
54
55

  var longestSizeLabelLength = Math.max.apply(null,
    assets.map(a => a.sizeLabel.length)
  );
56
  assets.forEach(asset => {
57
58
59
60
61
    var sizeLabel = asset.sizeLabel;
    if (sizeLabel.length < longestSizeLabelLength) {
      var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLabel.length);
      sizeLabel += rightPadding;
    }
62
    console.log(
63
64
      '  ' + chalk.green(sizeLabel) +
      '  ' + chalk.dim(asset.folder + path.sep) + chalk.cyan(asset.name)
65
66
67
68
    );
  });
  console.log();

Dan Abramov's avatar
Dan Abramov committed
69
  var openCommand = process.platform === 'win32' ? 'start' : 'open';
70
  var homepagePath = require(paths.appPackageJson).homepage;
71
  if (homepagePath) {
72
    console.log('You can now publish them at ' + homepagePath + '.');
73
74
    console.log('For example, if you use GitHub Pages:');
    console.log();
75
    console.log('  git commit -am "Save local changes"');
76
77
78
    console.log('  git checkout -B gh-pages');
    console.log('  git add -f build');
    console.log('  git commit -am "Rebuild website"');
79
80
    console.log('  git filter-branch -f --prune-empty --subdirectory-filter build');
    console.log('  git push -f origin gh-pages');
81
82
    console.log('  git checkout -');
    console.log();
83
  } else {
84
    console.log('You can now serve them with any static server.');
85
86
    console.log('For example:');
    console.log();
87
88
89
    console.log('  npm install -g pushstate-server');
    console.log('  pushstate-server build');
    console.log('  ' + openCommand + ' http://localhost:9000');
90
    console.log();
91
  }
92
  console.log();
93
});