UNPKG

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