build.js 7.06 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
11
'use strict';
Christopher Chedeau's avatar
Christopher Chedeau committed
12

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

Brian Ng's avatar
Brian Ng committed
16
// Load environment variables from .env file. Suppress warnings using silent
17
18
19
20
21
// 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});

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

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

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

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

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

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

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

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

  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
78
    if (err) {
79
80
81
82
83
84
      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
85
      process.exit(1);
86
    }
87

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

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

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

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

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