| 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 |
|
|---|
| 11 | const fs = require('fs');
|
|---|
| 12 | const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware');
|
|---|
| 13 | const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
|
|---|
| 14 | const ignoredFiles = require('react-dev-utils/ignoredFiles');
|
|---|
| 15 | const redirectServedPath = require('react-dev-utils/redirectServedPathMiddleware');
|
|---|
| 16 | const paths = require('./paths');
|
|---|
| 17 | const getHttpsConfig = require('./getHttpsConfig');
|
|---|
| 18 |
|
|---|
| 19 | const host = process.env.HOST || '0.0.0.0';
|
|---|
| 20 | const sockHost = process.env.WDS_SOCKET_HOST;
|
|---|
| 21 | const sockPath = process.env.WDS_SOCKET_PATH; // default: '/ws'
|
|---|
| 22 | const sockPort = process.env.WDS_SOCKET_PORT;
|
|---|
| 23 |
|
|---|
| 24 | module.exports = function (proxy, allowedHost) {
|
|---|
| 25 | const disableFirewall =
|
|---|
| 26 | !proxy || process.env.DANGEROUSLY_DISABLE_HOST_CHECK === 'true';
|
|---|
| 27 | return {
|
|---|
| 28 | // WebpackDevServer 2.4.3 introduced a security fix that prevents remote
|
|---|
| 29 | // websites from potentially accessing local content through DNS rebinding:
|
|---|
| 30 | // https://github.com/webpack/webpack-dev-server/issues/887
|
|---|
| 31 | // https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
|
|---|
| 32 | // However, it made several existing use cases such as development in cloud
|
|---|
| 33 | // environment or subdomains in development significantly more complicated:
|
|---|
| 34 | // https://github.com/facebook/create-react-app/issues/2271
|
|---|
| 35 | // https://github.com/facebook/create-react-app/issues/2233
|
|---|
| 36 | // While we're investigating better solutions, for now we will take a
|
|---|
| 37 | // compromise. Since our WDS configuration only serves files in the `public`
|
|---|
| 38 | // folder we won't consider accessing them a vulnerability. However, if you
|
|---|
| 39 | // use the `proxy` feature, it gets more dangerous because it can expose
|
|---|
| 40 | // remote code execution vulnerabilities in backends like Django and Rails.
|
|---|
| 41 | // So we will disable the host check normally, but enable it if you have
|
|---|
| 42 | // specified the `proxy` setting. Finally, we let you override it if you
|
|---|
| 43 | // really know what you're doing with a special environment variable.
|
|---|
| 44 | // Note: ["localhost", ".localhost"] will support subdomains - but we might
|
|---|
| 45 | // want to allow setting the allowedHosts manually for more complex setups
|
|---|
| 46 | allowedHosts: disableFirewall ? 'all' : [allowedHost],
|
|---|
| 47 | headers: {
|
|---|
| 48 | 'Access-Control-Allow-Origin': '*',
|
|---|
| 49 | 'Access-Control-Allow-Methods': '*',
|
|---|
| 50 | 'Access-Control-Allow-Headers': '*',
|
|---|
| 51 | },
|
|---|
| 52 | // Enable gzip compression of generated files.
|
|---|
| 53 | compress: true,
|
|---|
| 54 | static: {
|
|---|
| 55 | // By default WebpackDevServer serves physical files from current directory
|
|---|
| 56 | // in addition to all the virtual build products that it serves from memory.
|
|---|
| 57 | // This is confusing because those files won’t automatically be available in
|
|---|
| 58 | // production build folder unless we copy them. However, copying the whole
|
|---|
| 59 | // project directory is dangerous because we may expose sensitive files.
|
|---|
| 60 | // Instead, we establish a convention that only files in `public` directory
|
|---|
| 61 | // get served. Our build script will copy `public` into the `build` folder.
|
|---|
| 62 | // In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
|
|---|
| 63 | // <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
|
|---|
| 64 | // In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
|
|---|
| 65 | // Note that we only recommend to use `public` folder as an escape hatch
|
|---|
| 66 | // for files like `favicon.ico`, `manifest.json`, and libraries that are
|
|---|
| 67 | // for some reason broken when imported through webpack. If you just want to
|
|---|
| 68 | // use an image, put it in `src` and `import` it from JavaScript instead.
|
|---|
| 69 | directory: paths.appPublic,
|
|---|
| 70 | publicPath: [paths.publicUrlOrPath],
|
|---|
| 71 | // By default files from `contentBase` will not trigger a page reload.
|
|---|
| 72 | watch: {
|
|---|
| 73 | // Reportedly, this avoids CPU overload on some systems.
|
|---|
| 74 | // https://github.com/facebook/create-react-app/issues/293
|
|---|
| 75 | // src/node_modules is not ignored to support absolute imports
|
|---|
| 76 | // https://github.com/facebook/create-react-app/issues/1065
|
|---|
| 77 | ignored: ignoredFiles(paths.appSrc),
|
|---|
| 78 | },
|
|---|
| 79 | },
|
|---|
| 80 | client: {
|
|---|
| 81 | webSocketURL: {
|
|---|
| 82 | // Enable custom sockjs pathname for websocket connection to hot reloading server.
|
|---|
| 83 | // Enable custom sockjs hostname, pathname and port for websocket connection
|
|---|
| 84 | // to hot reloading server.
|
|---|
| 85 | hostname: sockHost,
|
|---|
| 86 | pathname: sockPath,
|
|---|
| 87 | port: sockPort,
|
|---|
| 88 | },
|
|---|
| 89 | overlay: {
|
|---|
| 90 | errors: true,
|
|---|
| 91 | warnings: false,
|
|---|
| 92 | },
|
|---|
| 93 | },
|
|---|
| 94 | devMiddleware: {
|
|---|
| 95 | // It is important to tell WebpackDevServer to use the same "publicPath" path as
|
|---|
| 96 | // we specified in the webpack config. When homepage is '.', default to serving
|
|---|
| 97 | // from the root.
|
|---|
| 98 | // remove last slash so user can land on `/test` instead of `/test/`
|
|---|
| 99 | publicPath: paths.publicUrlOrPath.slice(0, -1),
|
|---|
| 100 | },
|
|---|
| 101 |
|
|---|
| 102 | https: getHttpsConfig(),
|
|---|
| 103 | host,
|
|---|
| 104 | historyApiFallback: {
|
|---|
| 105 | // Paths with dots should still use the history fallback.
|
|---|
| 106 | // See https://github.com/facebook/create-react-app/issues/387.
|
|---|
| 107 | disableDotRule: true,
|
|---|
| 108 | index: paths.publicUrlOrPath,
|
|---|
| 109 | },
|
|---|
| 110 | // `proxy` is run between `before` and `after` `webpack-dev-server` hooks
|
|---|
| 111 | proxy,
|
|---|
| 112 | onBeforeSetupMiddleware(devServer) {
|
|---|
| 113 | // Keep `evalSourceMapMiddleware`
|
|---|
| 114 | // middlewares before `redirectServedPath` otherwise will not have any effect
|
|---|
| 115 | // This lets us fetch source contents from webpack for the error overlay
|
|---|
| 116 | devServer.app.use(evalSourceMapMiddleware(devServer));
|
|---|
| 117 |
|
|---|
| 118 | if (fs.existsSync(paths.proxySetup)) {
|
|---|
| 119 | // This registers user provided middleware for proxy reasons
|
|---|
| 120 | require(paths.proxySetup)(devServer.app);
|
|---|
| 121 | }
|
|---|
| 122 | },
|
|---|
| 123 | onAfterSetupMiddleware(devServer) {
|
|---|
| 124 | // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
|
|---|
| 125 | devServer.app.use(redirectServedPath(paths.publicUrlOrPath));
|
|---|
| 126 |
|
|---|
| 127 | // This service worker file is effectively a 'no-op' that will reset any
|
|---|
| 128 | // previous service worker registered for the same host:port combination.
|
|---|
| 129 | // We do this in development to avoid hitting the production cache if
|
|---|
| 130 | // it used the same host and port.
|
|---|
| 131 | // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
|
|---|
| 132 | devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
|
|---|
| 133 | },
|
|---|
| 134 | };
|
|---|
| 135 | };
|
|---|