Skip to content

[Bug]: swc env_name is empty in swc plugin metadata context #11903

@Zeahan

Description

@Zeahan

System Info

System:
OS: macOS 14.4
CPU: (12) arm64 Apple M3 Pro
Memory: 100.22 MB / 18.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.19.5
pnpm: 10.15.1
Browsers:
Chrome: 127.0.6533.100
Firefox: 140.0.2
Safari: 17.4
npmPackages:
@rspack/cli: 1.3.1 => 1.3.1
@rspack/core: 1.3.1 => 1.3.1
@swc/plugin-emotion: 9.0.3

Details

When using @swc/plugin-emotion in some rspack-based projects (including both Modern.js and Rsbuild projects), I found that the plugin’s configuration options—specifically the default values autoLabel: 'dev-only' and sourceMap: true—do not take effect as expected.

After reviewing the documentation and source code of @swc/plugin-emotion, I discovered that the plugin determines the current environment (development or production) by calling TransformPluginProgramMetadata::get_context(&TransformPluginMetadataContextKind::Env) to retrieve the environment name (env_name) from swc. This environment information is used to control different build behaviors.

To investigate further, I implemented a simple SWC plugin as a demo to print out the environment information from the metadata. I found that the environment field is an empty string. As a result, any SWC plugins that rely on this field are unable to correctly identify the current build environment, leading to incorrect or unexpected behavior.

Reproduce link

https://github.com/Zeahan/rspack-repro-swc-env-name-error/tree/main

Reproduce Steps

  1. Run pnpm install
  2. Check if the demo plugin is correctly built in the packages/plugin-demo/target directory
  3. Run pnpm build in the project root directory and observe the env name information printed by the demo plugin in the console

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending triageThe issue/PR is currently untouched.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions