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

feat: built in env files support #3759

Open
wants to merge 76 commits into
base: master
Choose a base branch
from

Conversation

burhanuday
Copy link
Member

What kind of change does this PR introduce?

feature

Adds built in support to webpack to handle .env files

Did you add tests for your changes?
not yet

If relevant, did you update the documentation?
not yet

Summary

Closes #3747

Does this PR introduce a breaking change?

No

Other information

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should implement this as a plugin, so developers can reuse it, we need to impement this as a package, but I am fine with starting it as a plugin inside webpack-cli package, the main idea get https://github.com/mrsteele/dotenv-webpack/blob/master/src/index.js and rework it:

  • allow to set custom logic for loading dotenv files (by default we will use the same as you written)
  • allow to set multiple prefix, because you can have ESM and common js files in projects, we need to allow set import.meta.env and process.env
  • no silent, webpack has built-in logger, so let's use it
  • implement cache to avoid extra reading

and maybe other tweeak, yeah, the issue is not small (and not so big), so we can do it step by step

@burhanuday
Copy link
Member Author

Creating a new folder under packages. packages/environment-variables-webpack-plugin. Does this name work?

@alexander-akait
Copy link
Member

Let's take dovenv-webpack-plugin

@burhanuday
Copy link
Member Author

@alexander-akait if we do want to distribute it as a standalone plugin in the future, isnt it better to add it to the webpack codebase or put it under webpack-contrib org? creating it as a plugin here and then moving it to a different repo to distribute it additional unnecessary work
if we want this to be an inbuilt feature in webpack then lets add it to webpack repo or if its going to be an installable plugin then lets create a new repo under the contrib org?

@alexander-akait
Copy link
Member

@burhanuday That's right, we can do it this way, but I'd rather keep it here for better integration with CLI (i.e. allows to have special flags for this) and fast fixes and fast releases, not sure it really make sense to use it as standalone plugin (except complex cases)

@burhanuday
Copy link
Member Author

Okay. Creating new package here then

@alexander-akait
Copy link
Member

The main goal is a seamless integraiton, moving it and rename is not long task, implementation is the most long task

@codecov
Copy link

codecov bot commented Apr 30, 2023

Codecov Report

Attention: Patch coverage is 99.04762% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.77%. Comparing base (78f57d1) to head (ccf341e).
Report is 298 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3759      +/-   ##
==========================================
+ Coverage   91.64%   91.77%   +0.13%     
==========================================
  Files          22       23       +1     
  Lines        1628     1776     +148     
  Branches      466      505      +39     
==========================================
+ Hits         1492     1630     +138     
- Misses        136      146      +10     
Files Coverage Δ
packages/dotenv-webpack-plugin/src/index.js 100.00% <100.00%> (ø)
packages/webpack-cli/src/webpack-cli.ts 92.54% <95.00%> (-0.51%) ⬇️

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 78f57d1...ccf341e. Read the comment docs.

@burhanuday
Copy link
Member Author

since env param in the cli is already taken, what should we use for passing array of env file names in the cli?
does env-file work?

// files at the end of the array have higher priority
envFiles = this.defaultFileList,
prefixes = ["process.env.", "import.meta.env."],
envVarPrefix = "PUBLIC_",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's allow it to be string | string[], some env variables can be from other environments, for example SENTRY_ (error reporting) requre prefix SENTRY_, and it can be used in client, so you can have ["PUBLIC_", "SENTRY_"]

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I deeply look at dotenv-plugin and what we missing:

  • allowEmptyValues, we really need it, it allows to catch bad cases
  • let's add test case for const { DB_HOST, DB_PASS } = process.env; (we supports it in webpack, just to check it), same for import.meta

And I still think safe is good idea, but we can postpone it

Good job, thank you, I think we will merge/release it on this week

@burhanuday
Copy link
Member Author

burhanuday commented May 23, 2023

  • let's add test case for const { DB_HOST, DB_PASS } = process.env; (we supports it in webpack, just to check it), same for import.meta

@alexander-akait

In this plugin we are defining the variables using string like "process.env.varname":"value" and not the object syntax. Object syntax would be required for the above test case to pass.

While figuring this out, i was reading the webpack docs for define plugin and saw that this is recommended against doing in a note on this link - https://webpack.js.org/plugins/define-plugin/#usage
image

Other than that, i have implemented allowEmptyValues and set default value to false. also added tests for it as well as docs

@burhanuday
Copy link
Member Author

@alexander-akait friendly ping

@alexander-akait
Copy link
Member

@burhanuday yeah, sorry for delay, on my TODOs list on the top after #3739,

@hardfist
Copy link

hardfist commented Jun 6, 2023

web-infra-dev/rspack#2208 this is also the requested features from rspack users, glad to see webpack-cli is willing to support it

@alexander-akait
Copy link
Member

@hardfist Yeah, it will be an official part of webpack ecosystem, I want to be align with next.js logic, it is flexible and solves almost all requests, hope we will finish it soon (this week)

Copy link
Member

@evenstensberg evenstensberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Could you fix the .gitignore glob and I'll do a manual review after?

@@ -64,5 +64,7 @@ test/**/**/**/bin/
test/**/**/binary/**
test/**/dist
test/**/**/dist
test/**/**/dist1
test/**/**/dist2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/**/**/dist*

@evenstensberg evenstensberg self-assigned this Nov 21, 2023
@evenstensberg
Copy link
Member

@burhanuday I think also we need to check how the new node --env-file option could be used alongside this. Wil review your PR soon! :)

@Ivan198407
Copy link

Ivan198407 commented Nov 24, 2023 via email

@webpack-bot
Copy link

@ Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@snitin315 Please review the new changes.

@info-arnav info-arnav mentioned this pull request Mar 25, 2024
Copy link
Member

@evenstensberg evenstensberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you rebase?

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

Successfully merging this pull request may close these issues.

implement built-in .env support
7 participants