close
logologo
指南
配置
插件
API
社区
版本
更新日志
Rsbuild 0.x 文档
English
简体中文
指南
配置
插件
API
社区
更新日志
Rsbuild 0.x 文档
English
简体中文
logologo
Overview
root
mode
plugins
logLevel
environments

dev

dev.assetPrefix
dev.browserLogs
dev.cliShortcuts
dev.client
dev.hmr
dev.lazyCompilation
dev.liveReload
dev.progressBar
dev.setupMiddlewares
dev.watchFiles
dev.writeToDisk

resolve

resolve.aliasStrategy
resolve.alias
resolve.conditionNames
resolve.dedupe
resolve.extensions
resolve.mainFields

source

source.assetsInclude
source.decorators
source.define
source.entry
source.exclude
source.include
source.preEntry
source.transformImport
source.tsconfigPath

output

output.assetPrefix
output.charset
output.cleanDistPath
output.copy
output.cssModules
output.dataUriLimit
output.distPath
output.emitAssets
output.emitCss
output.externals
output.filenameHash
output.filename
output.injectStyles
output.inlineScripts
output.inlineStyles
output.legalComments
output.manifest
output.minify
output.module
output.overrideBrowserslist
output.polyfill
output.sourceMap
output.target

html

html.appIcon
html.crossorigin
html.favicon
html.inject
html.meta
html.mountId
html.outputStructure
html.scriptLoading
html.tags
html.templateParameters
html.template
html.title

server

server.base
server.compress
server.cors
server.headers
server.historyApiFallback
server.host
server.htmlFallback
server.https
server.middlewareMode
server.open
server.port
server.printUrls
server.proxy
server.publicDir
server.strictPort

security

security.nonce
security.sri

tools

tools.bundlerChain
tools.cssExtract
tools.cssLoader
tools.htmlPlugin
tools.lightningcssLoader
tools.postcss
tools.rspack
tools.styleLoader
tools.swc

performance

performance.buildCache
performance.bundleAnalyze
performance.chunkSplit
performance.dnsPrefetch
performance.preconnect
performance.prefetch
performance.preload
performance.printFileSize
performance.profile
performance.removeConsole
performance.removeMomentLocale

moduleFederation

moduleFederation.options
📝 在 GitHub 上编辑此页
上一页server.compress
下一页server.headers

#server.cors

  • 类型: boolean | import('cors').CorsOptions
  • 默认值:
const defaultAllowedOrigins =
  /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/;

const defaultOptions = {
  // 默认允许:
  // - localhost
  // - 127.0.0.1
  // - [::1]
  origin: defaultAllowedOrigins,
};
  • 版本: >= 1.1.11

为开发服务器和预览服务器配置 CORS 选项,基于 cors 中间件实现。

  • object:启用 CORS 并使用指定的选项。
  • true:启用 CORS 并使用默认选项(允许所有 origin,不推荐)。
  • false:禁用 CORS。
WARNING

使用 cors: true 或 cors.origin: '*' 会使你的开发服务器对所有 origin 开放,这可能会危及源代码的安全性,建议使用 origin 选项指定一个受信任 origins 的白名单。

#示例

  • 为特定 origin 启用 CORS:
rsbuild.config.ts
export default {
  server: {
    cors: {
      // 配置 `Access-Control-Allow-Origin` CORS 响应头
      origin: 'https://example.com',
    },
  },
};
  • 保留 Rsbuild 默认的 origin 配置,并添加额外的 origin:
rsbuild.config.ts
// `defaultAllowedOrigins` 为 Rsbuild 默认的 origin 值
import { defaultAllowedOrigins } from '@rsbuild/core';

export default {
  server: {
    cors: {
      origin: [defaultAllowedOrigins, 'https://example.com'],
    },
  },
};
  • 仅为开发服务器启用 CORS:
rsbuild.config.ts
const isDev = process.env.NODE_ENV === 'development';

export default {
  server: {
    cors: isDev ? { origin: 'https://example.com' } : false,
  },
};
  • 禁用 CORS:
rsbuild.config.ts
export default {
  server: {
    cors: false,
  },
};
  • 为所有 origin 启用 CORS(不推荐):
rsbuild.config.ts
export default {
  server: {
    // 等价于 `{ origin: '*' }`
    cors: true,
  },
};

#选项

cors 选项可以是一个对象,与 cors 中间件的选项相同。

默认配置等同于:

const defaultCorsOptions = {
  origin: '*',
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  preflightContinue: false,
  optionsSuccessStatus: 204,
};

#origin

  • 类型:
type StaticOrigin =
  | boolean
  | string
  | RegExp
  | Array<boolean | string | RegExp>;

type CustomOrigin = (
  requestOrigin: string | undefined,
  callback: (err: Error | null, origin?: StaticOrigin) => void,
) => void;

type Origin = StaticOrigin | CustomOrigin;

origin 选项用于配置 Access-Control-Allow-Origin 头:

rsbuild.config.ts
export default {
  server: {
    cors: {
      origin: 'https://example.com',
    },
  },
};

通过数组指定多个允许的 origin:

rsbuild.config.ts
export default {
  server: {
    cors: {
      origin: [
        /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/,
        'https://example.com',
      ],
    },
  },
};

使用正则表达式来允许所有匹配的 origin:

rsbuild.config.ts
export default {
  server: {
    cors: {
      origin: /\.example\.com$/,
    },
  },
};

将 origin 设置为函数,可以动态决定允许的 origin,函数接收两个参数:

  • origin:当前请求的 origin,undefined 表示该请求没有 origin。
  • callback:一个回调函数,用于设置允许的 origin。
rsbuild.config.ts
export default {
  server: {
    cors: {
      origin: (origin, callback) => {
        // loadMyOrigins is an example call to load a list of origins
        loadMyOrigins((error, origins) => {
          callback(error, origins);
        });
      },
    },
  },
};