Skip to content

Commit 03eee39

Browse files
committed
Document configuration and build process
1 parent c13f7f4 commit 03eee39

File tree

8 files changed

+203
-21
lines changed

8 files changed

+203
-21
lines changed

config/babel.dev.js

+12
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@
88
*/
99

1010
module.exports = {
11+
// Don't try to find .babelrc because we want to force this configuration.
1112
babelrc: false,
13+
// This is a feature of `babel-loader` for webpack (not Babel itself).
14+
// It enables caching results in OS temporary directory for faster rebuilds.
1215
cacheDirectory: true,
1316
presets: [
17+
// let, const, destructuring, classes, modules
1418
require.resolve('babel-preset-es2015'),
19+
// exponentiation
1520
require.resolve('babel-preset-es2016'),
21+
// JSX, Flow
1622
require.resolve('babel-preset-react')
1723
],
1824
plugins: [
25+
// function x(a, b, c,) { }
1926
require.resolve('babel-plugin-syntax-trailing-function-commas'),
27+
// await fetch()
2028
require.resolve('babel-plugin-syntax-async-functions'),
29+
// class { handleClick = () => { } }
2130
require.resolve('babel-plugin-transform-class-properties'),
31+
// { ...todo, completed: true }
2232
require.resolve('babel-plugin-transform-object-rest-spread'),
33+
// function* () { yield 42; yield 43; }
2334
require.resolve('babel-plugin-transform-regenerator'),
35+
// Polyfills the runtime needed for async/await and generators
2436
[require.resolve('babel-plugin-transform-runtime'), {
2537
helpers: false,
2638
polyfill: false,

config/babel.prod.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,34 @@
88
*/
99

1010
module.exports = {
11+
// Don't try to find .babelrc because we want to force this configuration.
1112
babelrc: false,
1213
presets: [
14+
// let, const, destructuring, classes, modules
1315
require.resolve('babel-preset-es2015'),
16+
// exponentiation
1417
require.resolve('babel-preset-es2016'),
18+
// JSX, Flow
1519
require.resolve('babel-preset-react')
1620
],
1721
plugins: [
22+
// function x(a, b, c,) { }
1823
require.resolve('babel-plugin-syntax-trailing-function-commas'),
24+
// await fetch()
1925
require.resolve('babel-plugin-syntax-async-functions'),
26+
// class { handleClick = () => { } }
2027
require.resolve('babel-plugin-transform-class-properties'),
28+
// { ...todo, completed: true }
2129
require.resolve('babel-plugin-transform-object-rest-spread'),
22-
require.resolve('babel-plugin-transform-react-constant-elements'),
30+
// function* () { yield 42; yield 43; }
2331
require.resolve('babel-plugin-transform-regenerator'),
32+
// Polyfills the runtime needed for async/await and generators
2433
[require.resolve('babel-plugin-transform-runtime'), {
2534
helpers: false,
2635
polyfill: false,
2736
regenerator: true
28-
}]
37+
}],
38+
// Optimization: hoist JSX that never changes out of render()
39+
require.resolve('babel-plugin-transform-react-constant-elements')
2940
]
3041
};

config/env.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
11+
// injected into the application via DefinePlugin in Webpack configuration.
12+
1013
var REACT_APP = /^REACT_APP_/i;
1114
var NODE_ENV = JSON.stringify(process.env.NODE_ENV || 'development');
1215

config/polyfills.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ if (typeof Promise === 'undefined') {
66
window.Promise = require('promise/lib/es6-extensions.js');
77
}
88

9+
// fetch() polyfill for making API calls.
910
require('whatwg-fetch');

config/webpack.config.dev.js

+66-2
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,56 @@ var WatchMissingNodeModulesPlugin = require('../scripts/utils/WatchMissingNodeMo
1616
var paths = require('./paths');
1717
var env = require('./env');
1818

19+
// This is the development configuration.
20+
// It is focused on developer experience and fast rebuilds.
21+
// The production configuration is different and lives in a separate file.
1922
module.exports = {
23+
// This makes the bundle appear split into separate modules in the devtools.
24+
// We don't use source maps here because they can be confusing:
25+
// https://github.com/facebookincubator/create-react-app/issues/343#issuecomment-237241875
26+
// You may want 'cheap-module-source-map' instead if you prefer source maps.
2027
devtool: 'eval',
28+
// These are the "entry points" to our application.
29+
// This means they will be the "root" imports that are included in JS bundle.
30+
// The first two entry points enable "hot" CSS and auto-refreshes for JS.
2131
entry: [
32+
// Include WebpackDevServer client. It connects to WebpackDevServer via
33+
// sockets and waits for recompile notifications. When WebpackDevServer
34+
// recompiles, it sends a message to the client by socket. If only CSS
35+
// was changed, the app reload just the CSS. Otherwise, it will refresh.
36+
// The "?/" bit at the end tells the client to look for the socket at
37+
// the root path, i.e. /sockjs-node/. Otherwise visiting a client-side
38+
// route like /todos/42 would make it wrongly request /todos/42/sockjs-node.
39+
// The socket server is a part of WebpackDevServer which we are using.
40+
// The /sockjs-node/ path I'm referring to is hardcoded in WebpackDevServer.
2241
require.resolve('webpack-dev-server/client') + '?/',
42+
// Include Webpack hot module replacement runtime. Webpack is pretty
43+
// low-level so we need to put all the pieces together. The runtime listens
44+
// to the events received by the client above, and applies updates (such as
45+
// new CSS) to the running application.
2346
require.resolve('webpack/hot/dev-server'),
47+
// We ship a few polyfills by default.
2448
require.resolve('./polyfills'),
49+
// Finally, this is your app's code:
2550
path.join(paths.appSrc, 'index')
51+
// We include the app code last so that if there is a runtime error during
52+
// initialization, it doesn't blow up the WebpackDevServer client, and
53+
// changing JS code would still trigger a refresh.
2654
],
2755
output: {
2856
// Next line is not used in dev but WebpackDevServer crashes without it:
2957
path: paths.appBuild,
58+
// Add /* filename */ comments to generated require()s in the output.
3059
pathinfo: true,
60+
// This does not produce a real file. It's just the virtual path that is
61+
// served by WebpackDevServer in development. This is the JS bundle
62+
// containing code from all our entry points, and the Webpack runtime.
3163
filename: 'static/js/bundle.js',
64+
// In development, we always serve from the root. This makes config easier.
3265
publicPath: '/'
3366
},
3467
resolve: {
68+
// These are the reasonable defaults supported by the Node ecosystem.
3569
extensions: ['.js', '.json', ''],
3670
alias: {
3771
// This `alias` section can be safely removed after ejection.
@@ -45,11 +79,16 @@ module.exports = {
4579
'babel-runtime/regenerator': require.resolve('babel-runtime/regenerator')
4680
}
4781
},
82+
// Resolve loaders (webpack plugins for CSS, images, transpilation) from the
83+
// directory of `react-scripts` itself rather than the project directory.
84+
// You can remove this after ejecting.
4885
resolveLoader: {
4986
root: paths.ownNodeModules,
5087
moduleTemplates: ['*-loader']
5188
},
5289
module: {
90+
// First, run the linter.
91+
// It's important to do this before Babel processes the JS.
5392
preLoaders: [
5493
{
5594
test: /\.js$/,
@@ -58,22 +97,33 @@ module.exports = {
5897
}
5998
],
6099
loaders: [
100+
// Process JS with Babel.
61101
{
62102
test: /\.js$/,
63103
include: paths.appSrc,
64104
loader: 'babel',
65105
query: require('./babel.dev')
66106
},
107+
// "postcss" loader applies autoprefixer to our CSS.
108+
// "css" loader resolves paths in CSS and adds assets as dependencies.
109+
// "style" loader turns CSS into JS modules that inject <style> tags.
110+
// In production, we use a plugin to extract that CSS to a file, but
111+
// in development "style" loader enables hot editing of CSS.
67112
{
68113
test: /\.css$/,
69114
include: [paths.appSrc, paths.appNodeModules],
70115
loader: 'style!css!postcss'
71116
},
117+
// JSON is not enabled by default in Webpack but both Node and Browserify
118+
// allow it implicitly so we also enable it.
72119
{
73120
test: /\.json$/,
74121
include: [paths.appSrc, paths.appNodeModules],
75122
loader: 'json'
76123
},
124+
// "file" loader makes sure those assets get served by WebpackDevServer.
125+
// When you `import` an asset, you get its (virtual) filename.
126+
// In production, they would get copied to the `build` folder.
77127
{
78128
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)(\?.*)?$/,
79129
include: [paths.appSrc, paths.appNodeModules],
@@ -82,6 +132,8 @@ module.exports = {
82132
name: 'static/media/[name].[ext]'
83133
}
84134
},
135+
// "url" loader works just like "file" loader but it also embeds
136+
// assets smaller than specified size as data URLs to avoid requests.
85137
{
86138
test: /\.(mp4|webm)(\?.*)?$/,
87139
include: [paths.appSrc, paths.appNodeModules],
@@ -93,32 +145,44 @@ module.exports = {
93145
}
94146
]
95147
},
148+
// Point ESLint to our predefined config.
96149
eslint: {
97150
configFile: path.join(__dirname, 'eslint.js'),
98151
useEslintrc: false
99152
},
153+
// We use PostCSS for autoprefixing only.
100154
postcss: function() {
101155
return [
102156
autoprefixer({
103157
browsers: [
104158
'>1%',
105159
'last 4 versions',
106160
'Firefox ESR',
107-
'not ie < 9',
161+
'not ie < 9', // React doesn't support IE8 anyway
108162
]
109163
}),
110164
];
111165
},
112166
plugins: [
167+
// Generates an `index.html` file with the <script> injected.
113168
new HtmlWebpackPlugin({
114169
inject: true,
115170
template: paths.appHtml,
116171
favicon: paths.appFavicon,
117172
}),
173+
// Makes some environment variables available to the JS code, for example:
174+
// if (process.env.NODE_ENV === 'development') { ... }. See `env.js`.
118175
new webpack.DefinePlugin(env),
119-
// Note: only CSS is currently hot reloaded
176+
// This is necessary to emit hot updates (currently CSS only):
120177
new webpack.HotModuleReplacementPlugin(),
178+
// Watcher doesn't work well if you mistype casing in a path so we use
179+
// a plugin that prints an error when you attempt to do this.
180+
// See https://github.com/facebookincubator/create-react-app/issues/240
121181
new CaseSensitivePathsPlugin(),
182+
// If you require a missing module and then `npm install` it, you still have
183+
// to restart the development server for Webpack to discover it. This plugin
184+
// makes the discovery automatic so you don't have to restart.
185+
// See https://github.com/facebookincubator/create-react-app/issues/186
122186
new WatchMissingNodeModulesPlugin(paths.appNodeModules)
123187
]
124188
};

0 commit comments

Comments
 (0)