1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 |
|
11 | process.env.BABEL_ENV = 'production';
|
12 | process.env.NODE_ENV = 'production';
|
13 |
|
14 | process.on('unhandledRejection', err => {
|
15 | throw err;
|
16 | });
|
17 |
|
18 |
|
19 | require('../config/env');
|
20 |
|
21 | const path = require('path');
|
22 | const chalk = require('chalk');
|
23 | const fs = require('fs-extra');
|
24 | const webpack = require('webpack');
|
25 | const bfj = require('bfj');
|
26 | const config = require('../config/webpack.prod.config');
|
27 | const paths = require('../config/paths');
|
28 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
29 | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
|
30 | const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
|
31 | const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
|
32 | const printBuildError = require('react-dev-utils/printBuildError');
|
33 | const scriptVersion = require("../package").version;
|
34 |
|
35 | const measureFileSizesBeforeBuild =
|
36 | FileSizeReporter.measureFileSizesBeforeBuild;
|
37 | const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
|
38 |
|
39 |
|
40 | const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
41 | const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
42 |
|
43 |
|
44 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
45 | process.exit(1);
|
46 | }
|
47 |
|
48 |
|
49 | const argv = process.argv.slice(2);
|
50 | const writeStatsJson = argv.indexOf('--stats') !== -1;
|
51 |
|
52 | measureFileSizesBeforeBuild(paths.appBuild)
|
53 | .then(previousFileSizes => {
|
54 |
|
55 |
|
56 | fs.emptyDirSync(paths.appBuild);
|
57 |
|
58 | copyPublicFolder();
|
59 |
|
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 |
|
109 | function 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 |
|
121 |
|
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 |
|
159 | function copyPublicFolder() {
|
160 | fs.copySync(paths.appPublic, paths.appBuild, {
|
161 | dereference: true,
|
162 | filter: file => file !== paths.appHtml,
|
163 | });
|
164 | }
|