Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f13fa1d

Browse files
author
Josh Estus
committedJan 23, 2020
Get beginnings working. Add build-component command and file
1 parent 828a8f0 commit f13fa1d

File tree

3 files changed

+233
-1
lines changed

3 files changed

+233
-1
lines changed
 

‎packages/react-scripts/bin/react-scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const scriptIndex = args.findIndex(
2424
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
2525
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
2626

27-
if (['build', 'eject', 'start', 'test'].includes(script)) {
27+
if (['build', 'eject', 'start', 'test', 'build-component'].includes(script)) {
2828
const result = spawn.sync(
2929
'node',
3030
nodeArgs
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
// Do this as the first thing so that any code reading it knows the right env.
12+
process.env.BABEL_ENV = 'production';
13+
process.env.NODE_ENV = 'production';
14+
15+
// Makes the script crash on unhandled rejections instead of silently
16+
// ignoring them. In the future, promise rejections that are not handled will
17+
// terminate the Node.js process with a non-zero exit code.
18+
process.on('unhandledRejection', err => {
19+
throw err;
20+
});
21+
22+
// Ensure environment variables are read.
23+
require('../config/env');
24+
// @remove-on-eject-begin
25+
// Do the preflight checks (only happens before eject).
26+
const verifyPackageTree = require('./utils/verifyPackageTree');
27+
if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
28+
verifyPackageTree();
29+
}
30+
const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
31+
verifyTypeScriptSetup();
32+
// @remove-on-eject-end
33+
34+
const path = require('path');
35+
const chalk = require('react-dev-utils/chalk');
36+
const fs = require('fs-extra');
37+
const webpack = require('webpack');
38+
const configFactory = require('../config/webpack.config');
39+
const paths = require('../config/paths');
40+
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
41+
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
42+
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
43+
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
44+
const printBuildError = require('react-dev-utils/printBuildError');
45+
46+
const measureFileSizesBeforeBuild =
47+
FileSizeReporter.measureFileSizesBeforeBuild;
48+
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
49+
const useYarn = fs.existsSync(paths.yarnLockFile);
50+
51+
// These sizes are pretty large. We'll warn for bundles exceeding them.
52+
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
53+
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
54+
55+
const isInteractive = process.stdout.isTTY;
56+
57+
// Warn and crash if required files are missing
58+
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
59+
process.exit(1);
60+
}
61+
62+
// Generate configuration
63+
const config = configFactory('production');
64+
65+
// We require that you explicitly set browsers and do not fall back to
66+
// browserslist defaults.
67+
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
68+
checkBrowsers(paths.appPath, isInteractive)
69+
.then(() => {
70+
// First, read the current file sizes in build directory.
71+
// This lets us display how much they changed later.
72+
return measureFileSizesBeforeBuild(paths.appBuild);
73+
})
74+
.then(previousFileSizes => {
75+
// Remove all content but keep the directory so that
76+
// if you're in it, you don't end up in Trash
77+
fs.emptyDirSync(paths.appBuild);
78+
// Merge with the public folder
79+
copyPublicFolder();
80+
// Start the webpack build
81+
return build(previousFileSizes);
82+
})
83+
.then(
84+
({ stats, previousFileSizes, warnings }) => {
85+
if (warnings.length) {
86+
console.log(chalk.yellow('Compiled with warnings.\n'));
87+
console.log(warnings.join('\n\n'));
88+
console.log(
89+
'\nSearch for the ' +
90+
chalk.underline(chalk.yellow('keywords')) +
91+
' to learn more about each warning.'
92+
);
93+
console.log(
94+
'To ignore, add ' +
95+
chalk.cyan('// eslint-disable-next-line') +
96+
' to the line before.\n'
97+
);
98+
} else {
99+
console.log(chalk.green('Compiled successfully.\n'));
100+
}
101+
102+
console.log('File sizes after gzip:\n');
103+
printFileSizesAfterBuild(
104+
stats,
105+
previousFileSizes,
106+
paths.appBuild,
107+
WARN_AFTER_BUNDLE_GZIP_SIZE,
108+
WARN_AFTER_CHUNK_GZIP_SIZE
109+
);
110+
console.log();
111+
112+
const appPackage = require(paths.appPackageJson);
113+
const publicUrl = paths.publicUrl;
114+
const publicPath = config.output.publicPath;
115+
const buildFolder = path.relative(process.cwd(), paths.appBuild);
116+
printHostingInstructions(
117+
appPackage,
118+
publicUrl,
119+
publicPath,
120+
buildFolder,
121+
useYarn
122+
);
123+
},
124+
err => {
125+
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
126+
if (tscCompileOnError) {
127+
console.log(
128+
chalk.yellow(
129+
'Compiled with the following type errors (you may want to check these before deploying your app):\n'
130+
)
131+
);
132+
printBuildError(err);
133+
} else {
134+
console.log(chalk.red('Failed to compile.\n'));
135+
printBuildError(err);
136+
process.exit(1);
137+
}
138+
}
139+
)
140+
.catch(err => {
141+
if (err && err.message) {
142+
console.log(err.message);
143+
}
144+
process.exit(1);
145+
});
146+
147+
// Create the production build and print the deployment instructions.
148+
function build(previousFileSizes) {
149+
// We used to support resolving modules according to `NODE_PATH`.
150+
// This now has been deprecated in favor of jsconfig/tsconfig.json
151+
// This lets you use absolute paths in imports inside large monorepos:
152+
if (process.env.NODE_PATH) {
153+
console.log(
154+
chalk.yellow(
155+
'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.'
156+
)
157+
);
158+
console.log();
159+
}
160+
161+
console.log('Creating an optimized production component build...');
162+
163+
const compiler = webpack(config);
164+
return new Promise((resolve, reject) => {
165+
compiler.run((err, stats) => {
166+
let messages;
167+
if (err) {
168+
if (!err.message) {
169+
return reject(err);
170+
}
171+
172+
let errMessage = err.message;
173+
174+
// Add additional information for postcss errors
175+
if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) {
176+
errMessage +=
177+
'\nCompileError: Begins at CSS selector ' +
178+
err['postcssNode'].selector;
179+
}
180+
181+
messages = formatWebpackMessages({
182+
errors: [errMessage],
183+
warnings: [],
184+
});
185+
} else {
186+
messages = formatWebpackMessages(
187+
stats.toJson({
188+
all: false,
189+
warnings: true,
190+
errors: true,
191+
})
192+
);
193+
}
194+
if (messages.errors.length) {
195+
// Only keep the first error. Others are often indicative
196+
// of the same problem, but confuse the reader with noise.
197+
if (messages.errors.length > 1) {
198+
messages.errors.length = 1;
199+
}
200+
return reject(new Error(messages.errors.join('\n\n')));
201+
}
202+
if (
203+
process.env.CI &&
204+
(typeof process.env.CI !== 'string' ||
205+
process.env.CI.toLowerCase() !== 'false') &&
206+
messages.warnings.length
207+
) {
208+
console.log(
209+
chalk.yellow(
210+
'\nTreating warnings as errors because process.env.CI = true.\n' +
211+
'Most CI servers set it automatically.\n'
212+
)
213+
);
214+
return reject(new Error(messages.warnings.join('\n\n')));
215+
}
216+
217+
return resolve({
218+
stats,
219+
previousFileSizes,
220+
warnings: messages.warnings,
221+
});
222+
});
223+
});
224+
}
225+
226+
function copyPublicFolder() {
227+
fs.copySync(paths.appPublic, paths.appBuild, {
228+
dereference: true,
229+
filter: file => file !== paths.appHtml,
230+
});
231+
}

‎packages/react-scripts/scripts/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ module.exports = function(
127127
{
128128
start: 'react-scripts start',
129129
build: 'react-scripts build',
130+
'build-component': 'react-scripts build-component',
130131
test: 'react-scripts test',
131132
eject: 'react-scripts eject',
132133
},

0 commit comments

Comments
 (0)
This repository has been archived.