webpack-cli icon indicating copy to clipboard operation
webpack-cli copied to clipboard

feat: built in env files support

Open burhanuday opened this issue 1 year ago • 26 comments

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 https://github.com/webpack/webpack-cli/issues/3747

Does this PR introduce a breaking change?

No

Other information

burhanuday avatar Apr 27 '23 09:04 burhanuday

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

burhanuday avatar Apr 27 '23 14:04 burhanuday

Let's take dovenv-webpack-plugin

alexander-akait avatar Apr 27 '23 14:04 alexander-akait

@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?

burhanuday avatar Apr 27 '23 15:04 burhanuday

@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)

alexander-akait avatar Apr 27 '23 15:04 alexander-akait

Okay. Creating new package here then

burhanuday avatar Apr 27 '23 15:04 burhanuday

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

alexander-akait avatar Apr 27 '23 15:04 alexander-akait

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%) :arrow_down:

... 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.

codecov[bot] avatar Apr 30 '23 04:04 codecov[bot]

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?

burhanuday avatar Apr 30 '23 04:04 burhanuday

Let's implement --dot-env flag to enable it, maybe we will enable it in the next major release, but now it can break a logic, so we need to move it under the option

alexander-akait avatar May 04 '23 16:05 alexander-akait

Regarding to --env, good questions, let's add TODO for this, because I am not sure

alexander-akait avatar May 04 '23 16:05 alexander-akait

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?

@burhanuday I didn't get this, do you mean in case the user wants to read variables from any other env file for example .env.my-custom-file?

snitin315 avatar May 06 '23 08:05 snitin315

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?

@burhanuday I didn't get this, do you mean in case the user wants to read variables from any other env file for example .env.my-custom-file?

Yes. But i think since we are publishing the plugin anyway, this can be passed as an option to the plugin and we dont need to take this via the CLI

burhanuday avatar May 07 '23 03:05 burhanuday

@alexander-akait a PR with failing tests was merged into master. so tests are also failing on this PR - https://github.com/webpack/webpack-cli/pull/3783

burhanuday avatar May 10 '23 06:05 burhanuday

I'll check the CI

snitin315 avatar May 10 '23 07:05 snitin315

Looks like another flaky test

burhanuday avatar May 13 '23 04:05 burhanuday

all tests have passed

burhanuday avatar May 15 '23 15:05 burhanuday

  • 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 avatar May 23 '23 11:05 burhanuday

@alexander-akait friendly ping

burhanuday avatar May 31 '23 17:05 burhanuday

@burhanuday yeah, sorry for delay, on my TODOs list on the top after https://github.com/webpack/webpack-cli/pull/3739,

alexander-akait avatar May 31 '23 19:05 alexander-akait

https://github.com/web-infra-dev/rspack/issues/2208 this is also the requested features from rspack users, glad to see webpack-cli is willing to support it

hardfist avatar Jun 06 '23 12:06 hardfist

@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)

alexander-akait avatar Jun 06 '23 16:06 alexander-akait

@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! :)

evenstensberg avatar Nov 21 '23 20:11 evenstensberg

Х̶о̶р̶о̶ш̶о̶ с̶ н̶е̶т̶е̶р̶п̶е̶н̶и̶е̶м̶ ж̶д̶у̶

ср, 22 нояб. 2023 г., 03:04 Even Stensberg @.***>:

@burhanuday https://github.com/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! :)

— Reply to this email directly, view it on GitHub https://github.com/webpack/webpack-cli/pull/3759#issuecomment-1821601486, or unsubscribe https://github.com/notifications/unsubscribe-auth/BBO6JJWAWFJIWT3AXTUV5QTYFUCOXAVCNFSM6AAAAAAXNRXH5KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRRGYYDCNBYGY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Ivan198407 avatar Nov 24 '23 18:11 Ivan198407

@ Thanks for your update.

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

@snitin315 Please review the new changes.

webpack-bot avatar Feb 28 '24 11:02 webpack-bot