logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Configuration
      dev
      assetPrefix
      beforeStartUrl
      client
      hmr
      host
      https
      lazyCompilation
      liveReload
      progressBar
      server
      setupMiddlewares
      startUrl
      watchFiles
      writeToDisk
      bff
      crossProject
      prefix
      html
      appIcon
      crossorigin
      favicon
      inject
      meta
      mountId
      outputStructure
      scriptLoading
      tags
      templateParameters
      template
      title
      tools
      autoprefixer
      babel
      bundlerChain
      cssExtract
      cssLoader
      devServer
      htmlPlugin
      less
      lightningcssLoader
      minifyCss
      postcss
      rspack
      sass
      styleLoader
      swc
      tsChecker
      source
      aliasStrategy
      alias
      configDir
      decorators
      define
      disableDefaultEntries
      enableAsyncEntry
      entriesDir
      entries
      exclude
      globalVars
      include
      mainEntryName
      preEntry
      transformImport
      resolve
      aliasStrategy
      alias
      conditionNames
      dedupe
      extensions
      server
      baseUrl
      port
      publicRoutes
      routes
      ssrByEntries
      ssr
      output
      assetPrefix
      assetsRetry
      charset
      cleanDistPath
      convertToRem
      copy
      cssModules
      dataUriLimit
      disableCssModuleExtension
      disableInlineRuntimeChunk
      disableSvgr
      disableTsChecker
      distPath
      enableAssetManifest
      enableCssModuleTSDeclaration
      disableInlineRouteManifests
      externals
      filenameHash
      filename
      injectStyles
      inlineScripts
      inlineStyles
      legalComments
      minify
      overrideBrowserslist
      polyfill
      sourceMap
      splitRouteChunks
      ssg
      ssgByEntries
      svgDefaultExport
      tempDir
      plugins
      security
      checkSyntax
      nonce
      sri
      runtime
      Introduce
      plugins
      router
      performance
      buildCache
      bundleAnalyze
      chunkSplit
      dnsPrefetch
      preconnect
      prefetch
      preload
      printFileSize
      profile
      removeConsole
      removeMomentLocale
      experiments
      sourceBuild
      builderPlugins
      📝 Edit this page
      Previous pagecssLoaderNext pagehtmlPlugin

      #tools.devServer

      • Type: Object
      • Default: {}

      The config of DevServer can be modified through tools.devServer.

      Tip

      Modern.js does not directly use webpack-dev-server or @rspack/dev-server, but implement DevServer based on webpack-dev-middleware.

      #Options

      #compress

      Warning

      Deprecated: This configuration is deprecated, please use dev.server.compress instead.

      • Type: boolean
      • Default: true

      Whether to enable gzip compression for served static assets.

      If you want to disable the gzip compression, you can set compress to false:

      export default {
        tools: {
          devServer: {
            compress: false,
          },
        },
      };

      #headers

      Warning

      Deprecated: This configuration is deprecated, please use dev.server.headers instead.

      • Type: Record<string, string>
      • Default: undefined

      Adds headers to all responses.

      export default {
        tools: {
          devServer: {
            headers: {
              'X-Custom-Foo': 'bar',
            },
          },
        },
      };

      #historyApiFallback

      Warning

      Deprecated: This configuration is deprecated, please use dev.server.historyApiFallback instead.

      • Type: boolean | ConnectHistoryApiFallbackOptions
      • Default: false

      The index.html page will likely have to be served in place of any 404 responses. Enable devServer.historyApiFallback by setting it to true:

      export default {
        tools: {
          devServer: {
            historyApiFallback: true,
          },
        },
      };

      For more options and information, see the connect-history-api-fallback documentation.

      #proxy

      Warning

      Deprecated: This configuration is deprecated, please use dev.server.proxy instead.

      • Type: Record<string, string> | Record<string, ProxyDetail>
      • Default: undefined

      Proxying some URLs.

      export default {
        tools: {
          devServer: {
            proxy: {
              '/api': 'http://localhost:3000',
            },
          },
        },
      };

      A request to /api/users will now proxy the request to http://localhost:3000/api/users.

      If you don't want /api to be passed along, we need to rewrite the path:

      export default {
        tools: {
          devServer: {
            proxy: {
              '/api': {
                target: 'http://localhost:3000',
                pathRewrite: { '^/api': '' },
              },
            },
          },
        },
      };

      The DevServer Proxy makes use of the http-proxy-middleware package. Check out its documentation for more advanced usages.

      The full type definition of DevServer Proxy is:

      import type { Options as HttpProxyOptions } from 'http-proxy-middleware';
      
      type Filter = string | string[] | ((pathname: string, req: Request) => boolean);
      
      type ProxyDetail = HttpProxyOptions & {
        bypass?: (
          req: IncomingMessage,
          res: ServerResponse,
          proxyOptions: ProxyOptions,
        ) => string | undefined | null | false;
        context?: Filter;
      };
      
      type ProxyOptions =
        | Record<string, string>
        | Record<string, ProxyDetail>
        | ProxyDetail[]
        | ProxyDetail;

      In addition to the http-proxy-middleware option, we also support the bypass and context configuration:

      • bypass: bypass the proxy based on the return value of a function.
        • Return null or undefined to continue processing the request with proxy.
        • Return false to produce a 404 error for the request.
        • Return a path to serve from, instead of continuing to proxy the request.
      • context: If you want to proxy multiple, specific paths to the same target, you can use an array of one or more objects with a context property.
      // custom bypass
      export default {
        tools: {
          devServer: {
            proxy: {
              '/api': {
                target: 'http://localhost:3000',
                bypass: function (req, res, proxyOptions) {
                  if (req.headers.accept.indexOf('html') !== -1) {
                    console.log('Skipping proxy for browser request.');
                    return '/index.html';
                  }
                },
              },
            },
          },
        },
      };
      // proxy multiple
      export default {
        tools: {
          devServer: {
            proxy: [
              {
                context: ['/auth', '/api'],
                target: 'http://localhost:3000',
              },
            ],
          },
        },
      };

      #watch

      Warning

      Deprecated: This configuration is deprecated, please use dev.server.watch instead.

      • Type: boolean
      • Default: true

      Whether to watch files change in directories such as mock/, server/, api/.