logo
  • 指南
  • 配置
  • 插件
  • API
  • 示例
  • 社区
  • Modern.js 2.x 文档
  • 简体中文
    • 简体中文
    • English
    • 配置使用
      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
      介绍
      plugins
      router
      performance
      buildCache
      bundleAnalyze
      chunkSplit
      dnsPrefetch
      preconnect
      prefetch
      preload
      printFileSize
      profile
      removeConsole
      removeMomentLocale
      experiments
      sourceBuild
      builderPlugins
      📝 编辑此页面
      上一页cssLoader下一页htmlPlugin

      #tools.devServer

      • 类型: Object
      • 默认值: {}

      通过 tools.devServer 可以修改开发环境服务器的配置。

      Tip

      Modern.js 中并没有直接使用 webpack-dev-server 或 @rspack/dev-server, 而是基于 webpack-dev-middleware 实现 DevServer。

      #选项

      #compress

      Warning

      Deprecated:该配置已废弃,请使用 dev.server.compress 代替。

      • 类型: boolean
      • 默认值: true

      是否对静态资源启用 gzip 压缩。

      如果你需要禁用 gzip 压缩,可以将 compress 设置为 false:

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

      #headers

      Warning

      Deprecated:该配置已废弃,请使用 dev.server.headers 代替。

      • 类型: Record<string, string>
      • 默认值: undefined

      设置自定义响应头。

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

      #historyApiFallback

      Warning

      Deprecated:该配置已废弃,请使用 dev.server.historyApiFallback 代替。

      • 类型: boolean | ConnectHistoryApiFallbackOptions
      • 默认值: false

      在需要对一些 404 响应或其他请求提供替代页面的场景,可通过 devServer.historyApiFallback 进行设置:

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

      更多选项和详细信息可参考 connect-history-api-fallback 文档。

      #proxy

      Warning

      Deprecated:该配置已废弃,请使用 dev.server.proxy 代替。

      • 类型: Record<string, string> | Record<string, ProxyDetail>
      • 默认值: undefined

      代理请求到指定的服务上。

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

      此时,/api/users 请求将会代理到 http://localhost:3000/api/users。

      如果你不想传递 /api,可以通过 pathRewrite 重写请求路径:

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

      DevServer Proxy 基于 http-proxy-middleware 实现。你可以使用 http-proxy-middleware 的所有配置项,具体可以查看文档。

      DevServer Proxy 完整类型定义为:

      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;

      除了 http-proxy-middleware 的选项外,还支持 bypass 和 context 两个配置项:

      • bypass:根据函数的返回值绕过代理。
        • 返回 null 或 undefined 会继续用代理处理请求。
        • 返回 false 会返回 404 错误。
        • 返回一个具体的服务路径,将会使用此路径替代原请求路径。
      • context:如果你想代理多个特定的路径到同一个目标,你可以使用 context 配置项。
      // 自定义 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';
                  }
                },
              },
            },
          },
        },
      };
      // 代理多个路径到同一个目标
      export default {
        tools: {
          devServer: {
            proxy: [
              {
                context: ['/auth', '/api'],
                target: 'http://localhost:3000',
              },
            ],
          },
        },
      };

      #watch

      Warning

      Deprecated:该配置已废弃,请使用 dev.server.watch 代替。

      • 类型: boolean
      • 默认值: true

      是否监听 mock/、server/、api/ 等目录的文件变化。