Skip to content

define doesn't replace a variable which is declared in a same typescript file #7090

@hi-ogawa

Description

@hi-ogawa

I initially had a repro in rolldown, but it looks like this is a part of oxc transform, so I raised this issue here.

reproduction: https://github.com/hi-ogawa/reproductions/blob/main/rolldown-define-ts-declare/repro-oxc.js

import oxc from "oxc-transform";

const result = oxc.transform(
  "test.ts",
  `\
declare let __TEST_DEFINE__: string;
console.log({ __TEST_DEFINE__ });
`,
  {
    define: {
      __TEST_DEFINE__: "1234",
    },
  },
);
console.log(result);
// { code: 'console.log({ __TEST_DEFINE__ });\n', errors: [] }

I'm not sure how common such code is, but personally I tend to quickly get around the global variable typing by declare in a same file, so when I first saw this behavior, I was quite confused as I had no idea declare is the cause.

For comparison, esbuild replaces define in this case https://esbuild.github.io/try/#YgAwLjI0LjAAeyBkZWZpbmU6IHsgX19URVNUX0RFRklORV9fOiAnMTIzNCcgfSB9AGUAZW50cnkudHMAZGVjbGFyZSBsZXQgX19URVNUX0RFRklORV9fOiBzdHJpbmcKY29uc29sZS5sb2coeyBfX1RFU1RfREVGSU5FX18gfSkKCmNvbnNvbGUubG9nKChfX1RFU1RfREVGSU5FX18pID0+IF9fVEVTVF9ERUZJTkVfXyk

Metadata

Metadata

Assignees

Labels

C-bugCategory - Bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions