UNPKG

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