build.js 7.04 KB
Newer Older
1
// @remove-on-eject-begin
Christopher Chedeau's avatar
Christopher Chedeau committed
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
// @remove-on-eject-end
Christopher Chedeau's avatar
Christopher Chedeau committed
11

12
// Do this as the first thing so that any code reading it knows the right env.
13
14
process.env.NODE_ENV = 'production';

Brian Ng's avatar
Brian Ng committed
15
// Load environment variables from .env file. Suppress warnings using silent
16
17
18
19
20
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({silent: true});

21
var chalk = require('chalk');
22
var fs = require('fs-extra');
23
var path = require('path');
24
var url = require('url');
25
var webpack = require('webpack');
26
var config = require('../config/webpack.config.prod');
27
var paths = require('../config/paths');
28
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
29
30
31
var FileSizeReporter = require('react-dev-utils/FileSizeReporter');
var measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild;
var printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
32

33
var useYarn = fs.existsSync(paths.yarnLockFile);
Ville Immonen's avatar
Ville Immonen committed
34

35
36
37
38
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
  process.exit(1);
}
39

Dan Abramov's avatar
Dan Abramov committed
40
41
// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
42
measureFileSizesBeforeBuild(paths.appBuild).then(previousFileSizes => {
Elijah Manor's avatar
Elijah Manor committed
43
44
  // Remove all content but keep the directory so that
  // if you're in it, you don't end up in Trash
45
  fs.emptyDirSync(paths.appBuild);
Elijah Manor's avatar
Elijah Manor committed
46

Dan Abramov's avatar
Dan Abramov committed
47
  // Start the webpack build
48
  build(previousFileSizes);
49
50
51

  // Merge with the public folder
  copyPublicFolder();
Elijah Manor's avatar
Elijah Manor committed
52
});
53

54
55
56
57
58
59
60
61
62
63
// Print out errors
function printErrors(summary, errors) {
  console.log(chalk.red(summary));
  console.log();
  errors.forEach(err => {
    console.log(err.message || err);
    console.log();
  });
}

64
// Create the production build and print the deployment instructions.
65
function build(previousFileSizes) {
Elijah Manor's avatar
Elijah Manor committed
66
  console.log('Creating an optimized production build...');
67
68
69
70
71
72
73
74
75
76

  var compiler;
  try {
    compiler = webpack(config);
  } catch (err) {
    printErrors('Failed to compile.', [err]);
    process.exit(1);
  }

  compiler.run((err, stats) => {
Elijah Manor's avatar
Elijah Manor committed
77
    if (err) {
78
79
80
81
82
83
      printErrors('Failed to compile.', [err]);
      process.exit(1);
    }

    if (stats.compilation.errors.length) {
      printErrors('Failed to compile.', stats.compilation.errors);
Elijah Manor's avatar
Elijah Manor committed
84
      process.exit(1);
85
    }
86

87
    if (process.env.CI && stats.compilation.warnings.length) {
88
     printErrors('Failed to compile. When process.env.CI = true, warnings are treated as failures. Most CI servers set this automatically.', stats.compilation.warnings);
89
90
91
     process.exit(1);
   }

Elijah Manor's avatar
Elijah Manor committed
92
    console.log(chalk.green('Compiled successfully.'));
93
    console.log();
Elijah Manor's avatar
Elijah Manor committed
94
95

    console.log('File sizes after gzip:');
96
    console.log();
97
    printFileSizesAfterBuild(stats, previousFileSizes);
98
    console.log();
Elijah Manor's avatar
Elijah Manor committed
99
100

    var openCommand = process.platform === 'win32' ? 'start' : 'open';
101
    var appPackage  = require(paths.appPackageJson);
102
    var publicUrl = paths.publicUrl;
Dan Abramov's avatar
Dan Abramov committed
103
    var publicPath = config.output.publicPath;
104
105
    var publicPathname = url.parse(publicPath).pathname;
    if (publicUrl && publicUrl.indexOf('.github.io/') !== -1) {
Dan Abramov's avatar
Dan Abramov committed
106
      // "homepage": "http://user.github.io/project"
107
      console.log('The project was built assuming it is hosted at ' + chalk.green(publicPathname) + '.');
Dan Abramov's avatar
Dan Abramov committed
108
109
110
      console.log('You can control this with the ' + chalk.green('homepage') + ' field in your '  + chalk.cyan('package.json') + '.');
      console.log();
      console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
111
      console.log('To publish it at ' + chalk.green(publicUrl) + ', run:');
112
113
114
115
116
117
118
119
120
121
122
123
124
125
      // If script deploy has been added to package.json, skip the instructions
      if (typeof appPackage.scripts.deploy === 'undefined') {
        console.log();
        if (useYarn) {
          console.log('  ' + chalk.cyan('yarn') +  ' add --dev gh-pages');
        } else {
          console.log('  ' + chalk.cyan('npm') +  ' install --save-dev gh-pages');
        }
        console.log();
        console.log('Add the following script in your ' + chalk.cyan('package.json') + '.');
        console.log();
        console.log('    ' + chalk.dim('// ...'));
        console.log('    ' + chalk.yellow('"scripts"') + ': {');
        console.log('      ' + chalk.dim('// ...'));
126
127
        console.log('      ' + chalk.yellow('"predeploy"') + ': ' + chalk.yellow('"npm run build",'));
        console.log('      ' + chalk.yellow('"deploy"') + ': ' + chalk.yellow('"gh-pages -d build"'));
128
129
130
        console.log('    }');
        console.log();
        console.log('Then run:');
Ville Immonen's avatar
Ville Immonen committed
131
      }
132
      console.log();
Ville Immonen's avatar
Ville Immonen committed
133
      console.log('  ' + chalk.cyan(useYarn ? 'yarn' : 'npm') +  ' run deploy');
Elijah Manor's avatar
Elijah Manor committed
134
      console.log();
Dan Abramov's avatar
Dan Abramov committed
135
136
137
138
    } else if (publicPath !== '/') {
      // "homepage": "http://mywebsite.com/project"
      console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
      console.log('You can control this with the ' + chalk.green('homepage') + ' field in your '  + chalk.cyan('package.json') + '.');
Elijah Manor's avatar
Elijah Manor committed
139
      console.log();
Dan Abramov's avatar
Dan Abramov committed
140
      console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
Elijah Manor's avatar
Elijah Manor committed
141
      console.log();
Dan Abramov's avatar
Dan Abramov committed
142
    } else {
143
      if (publicUrl) {
Dan Abramov's avatar
Dan Abramov committed
144
        // "homepage": "http://mywebsite.com"
145
        console.log('The project was built assuming it is hosted at ' + chalk.green(publicUrl) +  '.');
Dan Abramov's avatar
Dan Abramov committed
146
        console.log('You can control this with the ' + chalk.green('homepage') + ' field in your '  + chalk.cyan('package.json') + '.');
Dan Abramov's avatar
Dan Abramov committed
147
        console.log();
Dan Abramov's avatar
Dan Abramov committed
148
149
      } else {
        // no homepage
150
        console.log('The project was built assuming it is hosted at the server root.');
Dan Abramov's avatar
Dan Abramov committed
151
152
153
154
        console.log('To override this, specify the ' + chalk.green('homepage') + ' in your '  + chalk.cyan('package.json') + '.');
        console.log('For example, add this to build it for GitHub Pages:')
        console.log();
        console.log('  ' + chalk.green('"homepage"') + chalk.cyan(': ') + chalk.green('"http://myname.github.io/myapp"') + chalk.cyan(','));
Dan Abramov's avatar
Dan Abramov committed
155
        console.log();
Dan Abramov's avatar
Dan Abramov committed
156
      }
157
158
      var build = path.relative(process.cwd(), paths.appBuild);
      console.log('The ' + chalk.cyan(build) + ' folder is ready to be deployed.');
Dan Abramov's avatar
Dan Abramov committed
159
160
      console.log('You may also serve it locally with a static server:')
      console.log();
Ville Immonen's avatar
Ville Immonen committed
161
162
163
164
165
      if (useYarn) {
        console.log('  ' + chalk.cyan('yarn') +  ' global add pushstate-server');
      } else {
        console.log('  ' + chalk.cyan('npm') +  ' install -g pushstate-server');
      }
166
      console.log('  ' + chalk.cyan('pushstate-server') + ' ' + build);
Dan Abramov's avatar
Dan Abramov committed
167
      console.log('  ' + chalk.cyan(openCommand) + ' http://localhost:' + (process.env.PORT || 9000));
Dan Abramov's avatar
Dan Abramov committed
168
      console.log();
Elijah Manor's avatar
Elijah Manor committed
169
170
171
    }
  });
}
172
173
174
175
176
177
178

function copyPublicFolder() {
  fs.copySync(paths.appPublic, paths.appBuild, {
    dereference: true,
    filter: file => file !== paths.appHtml
  });
}