UNPKG

5.95 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 errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
12const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware');
13const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
14const ignoredFiles = require('react-dev-utils/ignoredFiles');
15const config = require('./webpack.config.dev');
16const paths = require('./paths');
17const fs = require('fs');
18
19const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
20const host = process.env.HOST || '0.0.0.0';
21
22module.exports = function(proxy, allowedHost) {
23 return {
24 // WebpackDevServer 2.4.3 introduced a security fix that prevents remote
25 // websites from potentially accessing local content through DNS rebinding:
26 // https://github.com/webpack/webpack-dev-server/issues/887
27 // https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
28 // However, it made several existing use cases such as development in cloud
29 // environment or subdomains in development significantly more complicated:
30 // https://github.com/facebook/create-react-app/issues/2271
31 // https://github.com/facebook/create-react-app/issues/2233
32 // While we're investigating better solutions, for now we will take a
33 // compromise. Since our WDS configuration only serves files in the `public`
34 // folder we won't consider accessing them a vulnerability. However, if you
35 // use the `proxy` feature, it gets more dangerous because it can expose
36 // remote code execution vulnerabilities in backends like Django and Rails.
37 // So we will disable the host check normally, but enable it if you have
38 // specified the `proxy` setting. Finally, we let you override it if you
39 // really know what you're doing with a special environment variable.
40 disableHostCheck:
41 !proxy || process.env.DANGEROUSLY_DISABLE_HOST_CHECK === 'true',
42 // Enable gzip compression of generated files.
43 compress: true,
44 // Silence WebpackDevServer's own logs since they're generally not useful.
45 // It will still show compile warnings and errors with this setting.
46 clientLogLevel: 'none',
47 // By default WebpackDevServer serves physical files from current directory
48 // in addition to all the virtual build products that it serves from memory.
49 // This is confusing because those files won’t automatically be available in
50 // production build folder unless we copy them. However, copying the whole
51 // project directory is dangerous because we may expose sensitive files.
52 // Instead, we establish a convention that only files in `public` directory
53 // get served. Our build script will copy `public` into the `build` folder.
54 // In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
55 // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
56 // In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
57 // Note that we only recommend to use `public` folder as an escape hatch
58 // for files like `favicon.ico`, `manifest.json`, and libraries that are
59 // for some reason broken when imported through Webpack. If you just want to
60 // use an image, put it in `src` and `import` it from JavaScript instead.
61 contentBase: paths.appPublic,
62 // By default files from `contentBase` will not trigger a page reload.
63 watchContentBase: true,
64 // Enable hot reloading server. It will provide /sockjs-node/ endpoint
65 // for the WebpackDevServer client so it can learn when the files were
66 // updated. The WebpackDevServer client is included as an entry point
67 // in the Webpack development configuration. Note that only changes
68 // to CSS are currently hot reloaded. JS changes will refresh the browser.
69 hot: true,
70 // It is important to tell WebpackDevServer to use the same "root" path
71 // as we specified in the config. In development, we always serve from /.
72 publicPath: config.output.publicPath,
73 // WebpackDevServer is noisy by default so we emit custom message instead
74 // by listening to the compiler events with `compiler.hooks[...].tap` calls above.
75 quiet: true,
76 // Reportedly, this avoids CPU overload on some systems.
77 // https://github.com/facebook/create-react-app/issues/293
78 // src/node_modules is not ignored to support absolute imports
79 // https://github.com/facebook/create-react-app/issues/1065
80 watchOptions: {
81 ignored: ignoredFiles(paths.appSrc),
82 },
83 // Enable HTTPS if the HTTPS environment variable is set to 'true'
84 https: protocol === 'https',
85 host,
86 overlay: false,
87 historyApiFallback: {
88 // Paths with dots should still use the history fallback.
89 // See https://github.com/facebook/create-react-app/issues/387.
90 disableDotRule: true,
91 },
92 public: allowedHost,
93 proxy,
94 before(app, server) {
95 if (fs.existsSync(paths.proxySetup)) {
96 // This registers user provided middleware for proxy reasons
97 require(paths.proxySetup)(app);
98 }
99
100 // This lets us fetch source contents from webpack for the error overlay
101 app.use(evalSourceMapMiddleware(server));
102 // This lets us open files from the runtime error overlay.
103 app.use(errorOverlayMiddleware());
104
105 // This service worker file is effectively a 'no-op' that will reset any
106 // previous service worker registered for the same host:port combination.
107 // We do this in development to avoid hitting the production cache if
108 // it used the same host and port.
109 // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
110 app.use(noopServiceWorkerMiddleware());
111 },
112 };
113};