webpackDevServer.config.js 7.0 KB

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