UNPKG

4.99 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10// Do this as the first thing so that any code reading it knows the right env.
11process.env.BABEL_ENV = 'production';
12process.env.NODE_ENV = 'production';
13
14process.on('unhandledRejection', err => {
15 throw err;
16});
17
18// Ensure environment variables are read.
19require('../config/env');
20
21const path = require('path');
22const chalk = require('chalk');
23const fs = require('fs-extra');
24const webpack = require('webpack');
25const bfj = require('bfj');
26const config = require('../config/webpack.prod.config');
27const paths = require('../config/paths');
28const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
29const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
30const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
31const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
32const printBuildError = require('react-dev-utils/printBuildError');
33const scriptVersion = require("../package").version;
34
35const measureFileSizesBeforeBuild =
36 FileSizeReporter.measureFileSizesBeforeBuild;
37const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
38
39// These sizes are pretty large. We'll warn for bundles exceeding them.
40const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
41const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
42
43// Warn and crash if required files are missing
44if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
45 process.exit(1);
46}
47
48// Process CLI arguments
49const argv = process.argv.slice(2);
50const writeStatsJson = argv.indexOf('--stats') !== -1;
51
52measureFileSizesBeforeBuild(paths.appBuild)
53 .then(previousFileSizes => {
54 // Remove all content but keep the directory so that
55 // if you're in it, you don't end up in Trash
56 fs.emptyDirSync(paths.appBuild);
57 // Merge with the public folder
58 copyPublicFolder();
59 // Start the webpack build
60 return build(previousFileSizes);
61 })
62 .then(
63 ({ stats, previousFileSizes, warnings }) => {
64 if (warnings.length) {
65 console.log(chalk.yellow('Compiled with warnings.\n'));
66 console.log(warnings.join('\n\n'));
67 console.log(
68 '\nSearch for the ' +
69 chalk.underline(chalk.yellow('keywords')) +
70 ' to learn more about each warning.'
71 );
72 console.log(
73 'To ignore, add ' +
74 chalk.cyan('// eslint-disable-next-line') +
75 ' to the line before.\n'
76 );
77 } else {
78 console.log(chalk.green('Compiled successfully.\n'));
79 }
80
81 console.log('File sizes after gzip:\n');
82 printFileSizesAfterBuild(
83 stats,
84 previousFileSizes,
85 paths.appBuild,
86 WARN_AFTER_BUNDLE_GZIP_SIZE,
87 WARN_AFTER_CHUNK_GZIP_SIZE
88 );
89 console.log();
90
91 console.log(chalk.cyan(`食行生鲜运营平台开发部出品 version:${ scriptVersion }`));
92
93 console.log()
94 },
95 err => {
96 console.log(chalk.red('Failed to compile.\n'));
97 printBuildError(err);
98 process.exit(1);
99 }
100 )
101 .catch(err => {
102 if (err && err.message) {
103 console.log(err.message);
104 }
105 process.exit(1);
106 });
107
108
109function build(previousFileSizes) {
110 console.log('Creating an optimized production build...');
111
112 let compiler = webpack(config);
113 return new Promise((resolve, reject) => {
114 compiler.run((err, stats) => {
115 if (err) {
116 return reject(err);
117 }
118 const messages = formatWebpackMessages(stats.toJson({}, true));
119 if (messages.errors.length) {
120 // Only keep the first error. Others are often indicative
121 // of the same problem, but confuse the reader with noise.
122 if (messages.errors.length > 1) {
123 messages.errors.length = 1;
124 }
125 return reject(new Error(messages.errors.join('\n\n')));
126 }
127 if (
128 process.env.CI &&
129 (typeof process.env.CI !== 'string' ||
130 process.env.CI.toLowerCase() !== 'false') &&
131 messages.warnings.length
132 ) {
133 console.log(
134 chalk.yellow(
135 '\nTreating warnings as errors because process.env.CI = true.\n' +
136 'Most CI servers set it automatically.\n'
137 )
138 );
139 return reject(new Error(messages.warnings.join('\n\n')));
140 }
141
142 const resolveArgs = {
143 stats,
144 previousFileSizes,
145 warnings: messages.warnings,
146 };
147 if (writeStatsJson) {
148 return bfj
149 .write(paths.appBuild + '/bundle-stats.json', stats.toJson())
150 .then(() => resolve(resolveArgs))
151 .catch(error => reject(new Error(error)));
152 }
153
154 return resolve(resolveArgs);
155 });
156 });
157}
158
159function copyPublicFolder() {
160 fs.copySync(paths.appPublic, paths.appBuild, {
161 dereference: true,
162 filter: file => file !== paths.appHtml,
163 });
164}