For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/api/packages/react-rsbuild-plugin/interfaces/DefineDceVisitorConfig.md.
Lynx
  • 简体中文
  • 接口: DefineDceVisitorConfig

    类似各类打包工具中的 define,但它在转换阶段执行,并会执行一次死代码消除(DCE)。

    属性

    define

    define: Record<string, string>

    在编译时将代码中的变量替换为其他值或表达式。

    备注

    注意:与 source.define 的区别

    defineDCE 在转换 background-only 指令之前执行。因此可用于从主线程代码中消除仅在后台线程使用的代码。

    示例

    import { defineConfig } from '@lynx-js/rspeedy'
    import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin'
    
    export default defineConfig({
      plugins: [
        pluginReactLynx({
          defineDCE: {
            define: {
              __FOO__: 'false',
              'process.env.PLATFORM': '"lynx"',
            },
          },
        })
      ],
    })

    Then, __FOO__ and process.env.PLATFORM could be used in source code.

    if (process.env.PLATFORM === 'lynx') {
      console.log('lynx')
    }
    
    function FooOrBar() {
      if (__FOO__) {
        return <text>foo</text>
      } else {
        return <text>bar</text>
      }
    }
    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。