config/webpackDevServer.config.js   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 114
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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