UNPKG

13.4 kBJavaScriptView Raw
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
11const autoprefixer = require('autoprefixer');
12const path = require('path');
13const webpack = require('webpack');
14const HtmlWebpackPlugin = require('html-webpack-plugin');
15const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
16const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
17const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
18const eslintFormatter = require('react-dev-utils/eslintFormatter');
19const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
20const getClientEnvironment = require('./env');
21const paths = require('./paths');
22
23// Webpack uses `publicPath` to determine where the app is being served from.
24// In development, we always serve from the root. This makes config easier.
25const publicPath = '/';
26// `publicUrl` is just like `publicPath`, but we will provide it to our app
27// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
28// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
29const publicUrl = '';
30// Get environment variables to inject into our app.
31const env = getClientEnvironment(publicUrl);
32
33// This is the development configuration.
34// It is focused on developer experience and fast rebuilds.
35// The production configuration is different and lives in a separate file.
36module.exports = {
37 // You may want 'eval' instead if you prefer to see the compiled output in DevTools.
38 // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
39 devtool: 'cheap-module-source-map',
40 // These are the "entry points" to our application.
41 // This means they will be the "root" imports that are included in JS bundle.
42 // The first two entry points enable "hot" CSS and auto-refreshes for JS.
43 entry: [
44 // We ship a few polyfills by default:
45 require.resolve('./polyfills'),
46 // Include an alternative client for WebpackDevServer. A client's job is to
47 // connect to WebpackDevServer by a socket and get notified about changes.
48 // When you save a file, the client will either apply hot updates (in case
49 // of CSS changes), or refresh the page (in case of JS changes). When you
50 // make a syntax error, this client will display a syntax error overlay.
51 // Note: instead of the default WebpackDevServer client, we use a custom one
52 // to bring better experience for Create React App users. You can replace
53 // the line below with these two lines if you prefer the stock client:
54 // require.resolve('webpack-dev-server/client') + '?/',
55 // require.resolve('webpack/hot/dev-server'),
56 require.resolve('react-dev-utils/webpackHotDevClient'),
57 // Finally, this is your app's code:
58 paths.appIndexJs,
59 // We include the app code last so that if there is a runtime error during
60 // initialization, it doesn't blow up the WebpackDevServer client, and
61 // changing JS code would still trigger a refresh.
62 ],
63 output: {
64 // Add /* filename */ comments to generated require()s in the output.
65 pathinfo: true,
66 // This does not produce a real file. It's just the virtual path that is
67 // served by WebpackDevServer in development. This is the JS bundle
68 // containing code from all our entry points, and the Webpack runtime.
69 filename: 'static/js/bundle.js',
70 // There are also additional JS chunk files if you use code splitting.
71 chunkFilename: 'static/js/[name].chunk.js',
72 // This is the URL that app is served from. We use "/" in development.
73 publicPath: publicPath,
74 // Point sourcemap entries to original disk location (format as URL on Windows)
75 devtoolModuleFilenameTemplate: info =>
76 path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
77 },
78 resolve: {
79 // This allows you to set a fallback for where Webpack should look for modules.
80 // We placed these paths second because we want `node_modules` to "win"
81 // if there are any conflicts. This matches Node resolution mechanism.
82 // https://github.com/facebookincubator/create-react-app/issues/253
83 modules: ['node_modules', 'src', paths.appNodeModules].concat(
84 // It is guaranteed to exist because we tweak it in `env.js`
85 process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
86 ),
87 // These are the reasonable defaults supported by the Node ecosystem.
88 // We also include JSX as a common component filename extension to support
89 // some tools, although we do not recommend using it, see:
90 // https://github.com/facebookincubator/create-react-app/issues/290
91 // `web` extension prefixes have been added for better support
92 // for React Native Web.
93 extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
94 alias: {
95 // @remove-on-eject-begin
96 // Resolve Babel runtime relative to react-scripts.
97 // It usually still works on npm 3 without this but it would be
98 // unfortunate to rely on, as react-scripts could be symlinked,
99 // and thus babel-runtime might not be resolvable from the source.
100 'babel-runtime': path.dirname(
101 require.resolve('babel-runtime/package.json')
102 ),
103 // @remove-on-eject-end
104 // Support React Native Web
105 // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
106 'react-native': 'react-native-web',
107 },
108 plugins: [
109 // Prevents users from importing files from outside of src/ (or node_modules/).
110 // This often causes confusion because we only process files within src/ with babel.
111 // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
112 // please link the files into your node_modules/ and let module-resolution kick in.
113 // Make sure your source files are compiled, as they will not be processed in any way.
114 new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
115 ],
116 },
117 module: {
118 strictExportPresence: true,
119 rules: [
120 // TODO: Disable require.ensure as it's not a standard language feature.
121 // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
122 // { parser: { requireEnsure: false } },
123
124 // First, run the linter.
125 // It's important to do this before Babel processes the JS.
126 {
127 test: /\.(js|jsx|mjs)$/,
128 enforce: 'pre',
129 use: [
130 {
131 options: {
132 formatter: eslintFormatter,
133 eslintPath: require.resolve('eslint'),
134 // @remove-on-eject-begin
135 baseConfig: {
136 extends: [require.resolve('eslint-config-react-app')],
137 },
138 ignore: false,
139 useEslintrc: false,
140 // @remove-on-eject-end
141 },
142 loader: require.resolve('eslint-loader'),
143 },
144 ],
145 include: paths.appSrc,
146 },
147 {
148 // "oneOf" will traverse all following loaders until one will
149 // match the requirements. When no loader matches it will fall
150 // back to the "file" loader at the end of the loader list.
151 oneOf: [
152 // "url" loader works like "file" loader except that it embeds assets
153 // smaller than specified limit in bytes as data URLs to avoid requests.
154 // A missing `test` is equivalent to a match.
155 {
156 test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
157 loader: require.resolve('url-loader'),
158 options: {
159 limit: 10000,
160 name: 'static/media/[name].[hash:8].[ext]',
161 },
162 },
163 // Process JS with Babel.
164 {
165 test: /\.(js|jsx|mjs)$/,
166 include: paths.appSrc,
167 loader: require.resolve('babel-loader'),
168 options: {
169 // @remove-on-eject-begin
170 babelrc: false,
171 presets: [require.resolve('babel-preset-react-app')],
172 // @remove-on-eject-end
173 // This is a feature of `babel-loader` for webpack (not Babel itself).
174 // It enables caching results in ./node_modules/.cache/babel-loader/
175 // directory for faster rebuilds.
176 cacheDirectory: true,
177 },
178 },
179 // "postcss" loader applies autoprefixer to our CSS.
180 // "css" loader resolves paths in CSS and adds assets as dependencies.
181 // "style" loader turns CSS into JS modules that inject <style> tags.
182 // In production, we use a plugin to extract that CSS to a file, but
183 // in development "style" loader enables hot editing of CSS.
184 {
185 test: /\.css$/,
186 use: [
187 require.resolve('style-loader'),
188 {
189 loader: require.resolve('css-loader'),
190 options: {
191 importLoaders: 1,
192 },
193 },
194 {
195 loader: require.resolve('postcss-loader'),
196 options: {
197 // Necessary for external CSS imports to work
198 // https://github.com/facebookincubator/create-react-app/issues/2677
199 ident: 'postcss',
200 plugins: () => [
201 require('postcss-flexbugs-fixes'),
202 autoprefixer({
203 browsers: [
204 '>1%',
205 'last 4 versions',
206 'Firefox ESR',
207 'not ie < 9', // React doesn't support IE8 anyway
208 ],
209 flexbox: 'no-2009',
210 }),
211 ],
212 },
213 },
214 ],
215 },
216 // "file" loader makes sure those assets get served by WebpackDevServer.
217 // When you `import` an asset, you get its (virtual) filename.
218 // In production, they would get copied to the `build` folder.
219 // This loader doesn't use a "test" so it will catch all modules
220 // that fall through the other loaders.
221 {
222 // Exclude `js` files to keep "css" loader working as it injects
223 // its runtime that would otherwise processed through "file" loader.
224 // Also exclude `html` and `json` extensions so they get processed
225 // by webpacks internal loaders.
226 exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
227 loader: require.resolve('file-loader'),
228 options: {
229 name: 'static/media/[name].[hash:8].[ext]',
230 },
231 },
232 ],
233 },
234 // ** STOP ** Are you adding a new loader?
235 // Make sure to add the new loader(s) before the "file" loader.
236 ],
237 },
238 plugins: [
239 // Makes some environment variables available in index.html.
240 // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
241 // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
242 // In development, this will be an empty string.
243 new InterpolateHtmlPlugin(env.raw),
244 // Generates an `index.html` file with the <script> injected.
245 new HtmlWebpackPlugin({
246 inject: true,
247 template: paths.appHtml,
248 }),
249 // Add module names to factory functions so they appear in browser profiler.
250 new webpack.NamedModulesPlugin(),
251 // Makes some environment variables available to the JS code, for example:
252 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
253 new webpack.DefinePlugin(env.stringified),
254 // This is necessary to emit hot updates (currently CSS only):
255 new webpack.HotModuleReplacementPlugin(),
256 // Watcher doesn't work well if you mistype casing in a path so we use
257 // a plugin that prints an error when you attempt to do this.
258 // See https://github.com/facebookincubator/create-react-app/issues/240
259 new CaseSensitivePathsPlugin(),
260 // If you require a missing module and then `npm install` it, you still have
261 // to restart the development server for Webpack to discover it. This plugin
262 // makes the discovery automatic so you don't have to restart.
263 // See https://github.com/facebookincubator/create-react-app/issues/186
264 new WatchMissingNodeModulesPlugin(paths.appNodeModules),
265 // Moment.js is an extremely popular library that bundles large locale files
266 // by default due to how Webpack interprets its code. This is a practical
267 // solution that requires the user to opt into importing specific locales.
268 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
269 // You can remove this if you don't use Moment.js:
270 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
271 ],
272 // Some libraries import Node modules but don't use them in the browser.
273 // Tell Webpack to provide empty mocks for them so importing them works.
274 node: {
275 dgram: 'empty',
276 fs: 'empty',
277 net: 'empty',
278 tls: 'empty',
279 child_process: 'empty',
280 },
281 // Turn off performance hints during development because we don't do any
282 // splitting or minification in interest of speed. These warnings become
283 // cumbersome.
284 performance: {
285 hints: false,
286 },
287};