Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define cannot be used with indexed accessors #2050

Closed
fwouts opened this issue Feb 24, 2022 · 1 comment
Closed

define cannot be used with indexed accessors #2050

fwouts opened this issue Feb 24, 2022 · 1 comment

Comments

@fwouts
Copy link

fwouts commented Feb 24, 2022

I like to use TypeScript with all strictness checks turned on. One of them is noPropertyAccessFromIndexSignature which forces indexed types such as { [key: string]: string } to be used with element access expressions, and not property access expressions.

For example, I cannot write process.env.FOO, I must write process.env["FOO"] or TypeScript will complain.

This doesn't play well with define, as it only replaces property access expressions and not element access expressions.

Here's an example config:

const { build } = require("esbuild");

build({
  entryPoints: ["./src/main.ts"],
  minify: false,
  bundle: true,
  outfile: "./dist/main.js",
  platform: "node",
  define: {
    "process.env.FOO": JSON.stringify("Hello, World!"),
  },
}).catch((err) => {
  console.error(err);
  process.exit(1);
});

This will not replace the expression process.env["FOO"], so it will still appear as is in the output bundle.

I tried also using:

  define: {
    'process.env["FOO"]': JSON.stringify("Hello, World!"),
  },

but esbuild doesn't like it:

error: The define key "process.env[\"FOO\"]" contains invalid identifier "env[\"FOO\"]"

I've created a repository that reproduces this issue: https://github.com/fwouts/esbuild-process-env-define

Obviously, the workaround is to disable noPropertyAccessFromIndexSignature in tsconfig.json, which is precisely what I'm planning to do for now.

However, I figure this could be a gotcha for those who tend to use both process.env["FOO"] and process.env.FOO interchangeably, especially if they're migrating from webpack (which does seem to handle both with DefinePlugin).

Would this be considered a bug?

I'd be happy to try making a PR for this if that helps :)

fwouts added a commit to fwouts/previewjs that referenced this issue Feb 24, 2022
This is in preparation for migrating from webpack to esbuild.

See evanw/esbuild#2050.
@evanw evanw closed this as completed in 58e2734 Mar 2, 2022
@fwouts
Copy link
Author

fwouts commented Mar 2, 2022

That was fast. Thank you @evanw!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant