UNPKG

10.7 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10// @remove-on-eject-end
11'use strict';
12
13var autoprefixer = require('autoprefixer');
14var webpack = require('webpack');
15var HtmlWebpackPlugin = require('html-webpack-plugin');
16var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
17var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
18var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
19var getClientEnvironment = require('./env');
20var paths = require('./paths');
21
22// @remove-on-eject-begin
23// `path` is not used after eject - see https://github.com/facebookincubator/create-react-app/issues/1174
24var path = require('path');
25// @remove-on-eject-end
26
27// Webpack uses `publicPath` to determine where the app is being served from.
28// In development, we always serve from the root. This makes config easier.
29var publicPath = '/';
30// `publicUrl` is just like `publicPath`, but we will provide it to our app
31// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
32// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
33var publicUrl = '';
34// Get environment variables to inject into our app.
35var env = getClientEnvironment(publicUrl);
36
37// This is the development configuration.
38// It is focused on developer experience and fast rebuilds.
39// The production configuration is different and lives in a separate file.
40module.exports = {
41 // You may want 'eval' instead if you prefer to see the compiled output in DevTools.
42 // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
43 devtool: 'cheap-module-source-map',
44 // These are the "entry points" to our application.
45 // This means they will be the "root" imports that are included in JS bundle.
46 // The first two entry points enable "hot" CSS and auto-refreshes for JS.
47 entry: [
48 // Include an alternative client for WebpackDevServer. A client's job is to
49 // connect to WebpackDevServer by a socket and get notified about changes.
50 // When you save a file, the client will either apply hot updates (in case
51 // of CSS changes), or refresh the page (in case of JS changes). When you
52 // make a syntax error, this client will display a syntax error overlay.
53 // Note: instead of the default WebpackDevServer client, we use a custom one
54 // to bring better experience for Create React App users. You can replace
55 // the line below with these two lines if you prefer the stock client:
56 // require.resolve('webpack-dev-server/client') + '?/',
57 // require.resolve('webpack/hot/dev-server'),
58 require.resolve('react-dev-utils/webpackHotDevClient'),
59 // We ship a few polyfills by default:
60 require.resolve('./polyfills'),
61 // Finally, this is your app's code:
62 paths.appIndexJs
63 // We include the app code last so that if there is a runtime error during
64 // initialization, it doesn't blow up the WebpackDevServer client, and
65 // changing JS code would still trigger a refresh.
66 ],
67 output: {
68 // Next line is not used in dev but WebpackDevServer crashes without it:
69 path: paths.appBuild,
70 // Add /* filename */ comments to generated require()s in the output.
71 pathinfo: true,
72 // This does not produce a real file. It's just the virtual path that is
73 // served by WebpackDevServer in development. This is the JS bundle
74 // containing code from all our entry points, and the Webpack runtime.
75 filename: 'static/js/bundle.js',
76 // This is the URL that app is served from. We use "/" in development.
77 publicPath: publicPath
78 },
79 resolve: {
80 // This allows you to set a fallback for where Webpack should look for modules.
81 // We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
82 // We use `fallback` instead of `root` because we want `node_modules` to "win"
83 // if there any conflicts. This matches Node resolution mechanism.
84 // https://github.com/facebookincubator/create-react-app/issues/253
85 // We also fallback to the app's node_modules to support hoisted modules in a
86 // linked package workflow.
87 fallback: [paths.appNodeModules].concat(paths.nodePaths),
88 // These are the reasonable defaults supported by the Node ecosystem.
89 // We also include JSX as a common component filename extension to support
90 // some tools, although we do not recommend using it, see:
91 // https://github.com/facebookincubator/create-react-app/issues/290
92 extensions: ['.js', '.json', '.jsx', ''],
93 alias: {
94 // Support React Native Web
95 // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
96 'react-native': 'react-native-web'
97 }
98 },
99 resolveLoader: {
100 // @remove-on-eject-begin
101 // Resolve loaders (webpack plugins for CSS, images, transpilation) from the
102 // directory of `react-scripts` itself rather than the project directory.
103 root: paths.ownNodeModules,
104 moduleTemplates: ['*-loader'],
105 // @remove-on-eject-end
106 // Fallback to any hoisted modules when dealing with linked libraries
107 fallback: paths.appNodeModules
108 },
109 module: {
110 // First, run the linter.
111 // It's important to do this before Babel processes the JS.
112 preLoaders: [
113 {
114 test: /\.(js|jsx)$/,
115 loader: 'eslint',
116 include: paths.appSrc,
117 }
118 ],
119 loaders: [
120 // ** ADDING/UPDATING LOADERS **
121 // The "url" loader handles all assets unless explicitly excluded.
122 // The `exclude` list *must* be updated with every change to loader extensions.
123 // When adding a new loader, you must add its `test`
124 // as a new entry in the `exclude` list for "url" loader.
125
126 // "url" loader embeds assets smaller than specified size as data URLs to avoid requests.
127 // Otherwise, it acts like the "file" loader.
128 {
129 exclude: [
130 /\.html$/,
131 // We have to write /\.(js|jsx)(\?.*)?$/ rather than just /\.(js|jsx)$/
132 // because you might change the hot reloading server from the custom one
133 // to Webpack's built-in webpack-dev-server/client?/, which would not
134 // get properly excluded by /\.(js|jsx)$/ because of the query string.
135 // Webpack 2 fixes this, but for now we include this hack.
136 // https://github.com/facebookincubator/create-react-app/issues/1713
137 /\.(js|jsx)(\?.*)?$/,
138 /\.css$/,
139 /\.json$/,
140 /\.less$/,
141 /\.svg$/
142 ],
143 loader: 'url',
144 query: {
145 limit: 10000,
146 name: 'static/media/[name].[hash:8].[ext]'
147 }
148 },
149 // Process JS with Babel.
150 {
151 test: /\.(js|jsx)$/,
152 include: paths.appSrc,
153 loader: 'babel',
154 query: {
155 // @remove-on-eject-begin
156 babelrc: false,
157 presets: [require.resolve('babel-preset-react-app')],
158 // @remove-on-eject-end
159 // This is a feature of `babel-loader` for webpack (not Babel itself).
160 // It enables caching results in ./node_modules/.cache/babel-loader/
161 // directory for faster rebuilds.
162 cacheDirectory: true
163 }
164 },
165 // "postcss" loader applies autoprefixer to our CSS.
166 // "css" loader resolves paths in CSS and adds assets as dependencies.
167 // "style" loader turns CSS into JS modules that inject <style> tags.
168 // In production, we use a plugin to extract that CSS to a file, but
169 // in development "style" loader enables hot editing of CSS.
170 {
171 test: /\.css$/,
172 loader: 'style!css?importLoaders=1!postcss'
173 },
174 // JSON is not enabled by default in Webpack but both Node and Browserify
175 // allow it implicitly so we also enable it.
176 {
177 test: /\.json$/,
178 loader: 'json'
179 },
180 // "file" loader for svg
181 {
182 test: /\.svg$/,
183 loader: 'file',
184 query: {
185 name: 'static/media/[name].[hash:8].[ext]'
186 }
187 },
188 {
189 test: /\.less$/,
190 loader: 'style!css?importLoaders=1!postcss!less'
191 }
192 // ** STOP ** Are you adding a new loader?
193 // Remember to add the new extension(s) to the "url" loader exclusion list.
194 ]
195 },
196 // @remove-on-eject-begin
197 // Point ESLint to our predefined config.
198 eslint: {
199 configFile: path.join(__dirname, '../eslintrc'),
200 useEslintrc: false,
201 },
202 // @remove-on-eject-end
203 // We use PostCSS for autoprefixing only.
204 postcss: function() {
205 return [
206 autoprefixer({
207 browsers: [
208 '>1%',
209 'last 4 versions',
210 'Firefox ESR',
211 'not ie < 9', // React doesn't support IE8 anyway
212 ]
213 }),
214 ];
215 },
216 plugins: [
217 // Makes some environment variables available in index.html.
218 // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
219 // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
220 // In development, this will be an empty string.
221 new InterpolateHtmlPlugin(env.raw),
222 // Generates an `index.html` file with the <script> injected.
223 new HtmlWebpackPlugin({
224 inject: true,
225 template: paths.appHtml,
226 }),
227 // Makes some environment variables available to the JS code, for example:
228 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
229 new webpack.DefinePlugin(env.stringified),
230 // This is necessary to emit hot updates (currently CSS only):
231 new webpack.HotModuleReplacementPlugin(),
232 // Watcher doesn't work well if you mistype casing in a path so we use
233 // a plugin that prints an error when you attempt to do this.
234 // See https://github.com/facebookincubator/create-react-app/issues/240
235 new CaseSensitivePathsPlugin(),
236 // If you require a missing module and then `npm install` it, you still have
237 // to restart the development server for Webpack to discover it. This plugin
238 // makes the discovery automatic so you don't have to restart.
239 // See https://github.com/facebookincubator/create-react-app/issues/186
240 new WatchMissingNodeModulesPlugin(paths.appNodeModules)
241 ],
242 // Some libraries import Node modules but don't use them in the browser.
243 // Tell Webpack to provide empty mocks for them so importing them works.
244 node: {
245 fs: 'empty',
246 net: 'empty',
247 tls: 'empty'
248 }
249};