@@ -16,22 +16,56 @@ var WatchMissingNodeModulesPlugin = require('../scripts/utils/WatchMissingNodeMo
1616var paths = require ( './paths' ) ;
1717var 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.
1922module . 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 : / \. j s $ / ,
@@ -58,22 +97,33 @@ module.exports = {
5897 }
5998 ] ,
6099 loaders : [
100+ // Process JS with Babel.
61101 {
62102 test : / \. j s $ / ,
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 : / \. c s s $ / ,
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 : / \. j s o n $ / ,
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 : / \. ( j p g | p n g | g i f | e o t | s v g | t t f | w o f f | w o f f 2 ) ( \? .* ) ? $ / ,
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 : / \. ( m p 4 | w e b m ) ( \? .* ) ? $ / ,
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