UNPKG

15.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', 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 paths.appSrc
87 ),
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 // `web` extension prefixes have been added for better support
93 // for React Native Web.
94 extensions: [
95 '.re',
96 '.ml',
97 '.web.js',
98 '.mjs',
99 '.js',
100 '.json',
101 '.web.jsx',
102 '.jsx',
103 ],
104 alias: {
105 // @remove-on-eject-begin
106 // Resolve Babel runtime relative to react-scripts.
107 // It usually still works on npm 3 without this but it would be
108 // unfortunate to rely on, as react-scripts could be symlinked,
109 // and thus babel-runtime might not be resolvable from the source.
110 'babel-runtime': path.dirname(
111 require.resolve('babel-runtime/package.json')
112 ),
113 // @remove-on-eject-end
114 // Support React Native Web
115 // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
116 'react-native': 'react-native-web',
117 },
118 plugins: [
119 // Prevents users from importing files from outside of src/ (or node_modules/).
120 // This often causes confusion because we only process files within src/ with babel.
121 // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
122 // please link the files into your node_modules/ and let module-resolution kick in.
123 // Make sure your source files are compiled, as they will not be processed in any way.
124 new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
125 ],
126 },
127 module: {
128 strictExportPresence: true,
129 rules: [
130 // TODO: Disable require.ensure as it's not a standard language feature.
131 // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
132 // { parser: { requireEnsure: false } },
133
134 // First, run the linter.
135 // It's important to do this before Babel processes the JS.
136 {
137 test: /\.(js|jsx|mjs)$/,
138 enforce: 'pre',
139 use: [
140 {
141 options: {
142 formatter: eslintFormatter,
143 eslintPath: require.resolve('eslint'),
144 // @remove-on-eject-begin
145 baseConfig: {
146 extends: [require.resolve('eslint-config-react-app')],
147 },
148 ignore: false,
149 useEslintrc: false,
150 // @remove-on-eject-end
151 },
152 loader: require.resolve('eslint-loader'),
153 },
154 ],
155 include: paths.appSrc,
156 },
157 {
158 // "oneOf" will traverse all following loaders until one will
159 // match the requirements. When no loader matches it will fall
160 // back to the "file" loader at the end of the loader list.
161 oneOf: [
162 // "url" loader works like "file" loader except that it embeds assets
163 // smaller than specified limit in bytes as data URLs to avoid requests.
164 // A missing `test` is equivalent to a match.
165 {
166 test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
167 loader: require.resolve('url-loader'),
168 options: {
169 limit: 10000,
170 name: 'static/media/[name].[hash:8].[ext]',
171 },
172 },
173 // Process JS with Babel.
174 {
175 test: /\.(js|jsx|mjs)$/,
176 include: paths.appSrc,
177 loader: require.resolve('babel-loader'),
178 options: {
179 // @remove-on-eject-begin
180 babelrc: false,
181 presets: [require.resolve('babel-preset-react-app')],
182 // @remove-on-eject-end
183 // This is a feature of `babel-loader` for webpack (not Babel itself).
184 // It enables caching results in ./node_modules/.cache/babel-loader/
185 // directory for faster rebuilds.
186 plugins: [require.resolve('react-hot-loader/babel')],
187 cacheDirectory: true,
188 },
189 },
190 // "postcss" loader applies autoprefixer to our CSS.
191 // "css" loader resolves paths in CSS and adds assets as dependencies.
192 // "style" loader turns CSS into JS modules that inject <style> tags.
193 // In production, we use a plugin to extract that CSS to a file, but
194 // in development "style" loader enables hot editing of CSS.
195 {
196 test: /\.css$/,
197 oneOf: [
198 {
199 exclude: paths.appSrc,
200 use: [
201 require.resolve('style-loader'),
202 {
203 loader: require.resolve('css-loader'),
204 options: {
205 importLoaders: 1,
206 },
207 },
208 {
209 loader: require.resolve('postcss-loader'),
210 options: {
211 // Necessary for external CSS imports to work
212 // https://github.com/facebookincubator/create-react-app/issues/2677
213 ident: 'postcss',
214 plugins: () => [
215 require('postcss-flexbugs-fixes'),
216 autoprefixer({
217 browsers: [
218 '>1%',
219 'last 4 versions',
220 'Firefox ESR',
221 'not ie < 9', // React doesn't support IE8 anyway
222 ],
223 flexbox: 'no-2009',
224 }),
225 ],
226 },
227 },
228 ],
229 },
230 {
231 include: paths.appSrc,
232 use: [
233 require.resolve('style-loader'),
234 {
235 loader: require.resolve('css-loader'),
236 options: {
237 importLoaders: 1,
238 modules: true,
239 localIdentName:
240 '[folder]_[name]__[local]--[hash:base64:5]',
241 },
242 },
243 {
244 loader: require.resolve('postcss-loader'),
245 options: {
246 // Necessary for external CSS imports to work
247 // https://github.com/facebookincubator/create-react-app/issues/2677
248 ident: 'postcss',
249 plugins: () => [
250 require('postcss-flexbugs-fixes'),
251 autoprefixer({
252 browsers: [
253 '>1%',
254 'last 4 versions',
255 'Firefox ESR',
256 'not ie < 9', // React doesn't support IE8 anyway
257 ],
258 flexbox: 'no-2009',
259 }),
260 require('postcss-nested'),
261 require('postcss-simple-vars'),
262 ],
263 },
264 },
265 ],
266 },
267 ],
268 },
269 {
270 test: /\.(re|rei|ml|mli)$/,
271 use: [
272 {
273 loader: require.resolve('bs-loader'),
274 },
275 ],
276 },
277 // "file" loader makes sure those assets get served by WebpackDevServer.
278 // When you `import` an asset, you get its (virtual) filename.
279 // In production, they would get copied to the `build` folder.
280 // This loader doesn't use a "test" so it will catch all modules
281 // that fall through the other loaders.
282 {
283 // Exclude `js` files to keep "css" loader working as it injects
284 // it's runtime that would otherwise processed through "file" loader.
285 // Also exclude `html` and `json` extensions so they get processed
286 // by webpacks internal loaders.
287 exclude: [/\.js$/, /\.html$/, /\.json$/],
288 loader: require.resolve('file-loader'),
289 options: {
290 name: 'static/media/[name].[hash:8].[ext]',
291 },
292 },
293 ],
294 },
295 // ** STOP ** Are you adding a new loader?
296 // Make sure to add the new loader(s) before the "file" loader.
297 ],
298 },
299 plugins: [
300 // Makes some environment variables available in index.html.
301 // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
302 // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
303 // In development, this will be an empty string.
304 new InterpolateHtmlPlugin(env.raw),
305 // Generates an `index.html` file with the <script> injected.
306 new HtmlWebpackPlugin({
307 inject: true,
308 template: paths.appHtml,
309 }),
310 // Add module names to factory functions so they appear in browser profiler.
311 new webpack.NamedModulesPlugin(),
312 // Makes some environment variables available to the JS code, for example:
313 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
314 new webpack.DefinePlugin(env.stringified),
315 // This is necessary to emit hot updates (currently CSS only):
316 new webpack.HotModuleReplacementPlugin(),
317 // Watcher doesn't work well if you mistype casing in a path so we use
318 // a plugin that prints an error when you attempt to do this.
319 // See https://github.com/facebookincubator/create-react-app/issues/240
320 new CaseSensitivePathsPlugin(),
321 // If you require a missing module and then `npm install` it, you still have
322 // to restart the development server for Webpack to discover it. This plugin
323 // makes the discovery automatic so you don't have to restart.
324 // See https://github.com/facebookincubator/create-react-app/issues/186
325 new WatchMissingNodeModulesPlugin(paths.appNodeModules),
326 // Moment.js is an extremely popular library that bundles large locale files
327 // by default due to how Webpack interprets its code. This is a practical
328 // solution that requires the user to opt into importing specific locales.
329 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
330 // You can remove this if you don't use Moment.js:
331 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
332 ],
333 // Some libraries import Node modules but don't use them in the browser.
334 // Tell Webpack to provide empty mocks for them so importing them works.
335 node: {
336 dgram: 'empty',
337 fs: 'empty',
338 net: 'empty',
339 tls: 'empty',
340 child_process: 'empty',
341 },
342 // Turn off performance hints during development because we don't do any
343 // splitting or minification in interest of speed. These warnings become
344 // cumbersome.
345 performance: {
346 hints: false,
347 },
348};