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

refactor: rewrite tests to use webpack compiler, drop node 8 #360

Merged
merged 25 commits into from
Mar 5, 2021
Merged

refactor: rewrite tests to use webpack compiler, drop node 8 #360

merged 25 commits into from
Mar 5, 2021

Conversation

beeequeue
Copy link
Contributor

@beeequeue beeequeue commented Mar 3, 2021

  • Replaces the {} replacement with "MISSING_ENV_VAR" which seems to not error, which the old one started doing for some reason
  • Uses the Webpack target to decide to stub process.env or not.
    For now it will not do it for Node and Electron targets
  • Rewrite all the tests to use the webpack compiler, and then testing on the output files
  • Drops support for Node 8 as the tests can't run on it because of BigInt not existing

You can test it for yourselves by using a yarn resolution:

  "resolutions": {
    "dotenv-webpack": "npm:@beequeue/dotenv-webpack"
  },

Fixes #271
Closes #289

CI runs: https://github.com/BeeeQueue/dotenv-webpack/actions?query=branch%3Afix-stubbing

@beeequeue beeequeue changed the title fix: only stub process.env if not targeting node fix: only stub process.env if not targeting node, fix stub replacement Mar 3, 2021
src/index.js Outdated Show resolved Hide resolved
src/index.js Show resolved Hide resolved
@mrsteele
Copy link
Owner

mrsteele commented Mar 3, 2021

Thanks for checking this out @beeequeue, any chance you ran this with all the supported versions of Webpack? Would love to keep it compatible with the other versions. Also, any chance we can add some tests in for this new check?

@beeequeue
Copy link
Contributor Author

beeequeue commented Mar 3, 2021

Turns out that the CI actions are not being run on PRs, and all the tests are failing! 😄

For this solution to be testable I think we need to set up a webpack compiler and compile stuff in the tests, so I'll have to do that.

I think it would make more sense to drop support for webpack <4 considering those versions are 4 years old at this point, and any changes made at this point will most likely be to support or use new features in webpack.

@mrsteele
Copy link
Owner

mrsteele commented Mar 3, 2021

As long as we have at least 4 and 5 I think I'd be good. I imagine supporting those older versions would be challenging?

@mrsteele
Copy link
Owner

mrsteele commented Mar 3, 2021

(I went back and looked and webpack@v3 was last updated May 2018)

@beeequeue
Copy link
Contributor Author

More about it being time consuming to (manually!) test for them, and then creating workarounds if something is found when no one should really be using them.

@mrsteele
Copy link
Owner

mrsteele commented Mar 4, 2021

@beeequeue would it make more sense to make the user make that distinction? #289 <- That PR puts a flag in the config to opt out of the stub but curious on your thoughts.

@mrsteele mrsteele mentioned this pull request Mar 4, 2021
2 tasks
@beeequeue
Copy link
Contributor Author

I think having the option to override this functionality is a very good idea, and having sensible defaults on when to do it ourselves.

Or maybe it would be even better to extract that to another plugin, ProcessEnvStubPlugin that will take care of this...

Another benefit of merging this PR is that the tests will be better, as in they are testing the real behavior of webpack as the tests will use the compiler now

@beeequeue beeequeue changed the title fix: only stub process.env if not targeting node, fix stub replacement refactor: rewrite tests to use webpack compiler and test on result Mar 4, 2021
@beeequeue
Copy link
Contributor Author

Since the test refactoring is very large I'm changing this PR to be about the tests and dropping Node only, and opening another one for the fixes afterwards.

@beeequeue beeequeue changed the title refactor: rewrite tests to use webpack compiler and test on result refactor: rewrite tests to use webpack compiler and test on result, drop node 8 Mar 4, 2021
@beeequeue beeequeue changed the title refactor: rewrite tests to use webpack compiler and test on result, drop node 8 refactor: rewrite tests to use webpack compiler, drop node 8 Mar 4, 2021
Copy link
Owner

@mrsteele mrsteele 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 looking really good. Any chance we can add the stub functionality in as well as drop the deprecated argument? I think once those are in this is a perfect release candidate for #363

@beeequeue
Copy link
Contributor Author

I extracted the check to its own function, made it not run on webpack <5, and changed the check so it will not stub if the target includes node, rather than starts with it.

I have no idea what the various other node targets are used for or if they have process, but if something breaks for the few people who use those I'm sure they'll come screaming...

@mrsteele
Copy link
Owner

mrsteele commented Mar 4, 2021

Everything I have tested so far looks good, seems to be right where it needs to be. Really great work and excited to get this out there!

On the browser list... target is SO confusing and not seeing a lot of documentation about the inner workings.

Looking at the source-code is a little more illuminating.

Would it make more sense to use getTargetsProperties and if !node then we stub MISSING_ENV_VAR since that is ultimately the problem?

@beeequeue
Copy link
Contributor Author

I think the current checks are good enough.

browserslist is a special case that we can't account for, since it can target whatever it wants (chrome 89, node 14, es5), but it's mainly used for browsers so we can assume it as browser for now.

@mrsteele
Copy link
Owner

mrsteele commented Mar 5, 2021

At minimum node would need to be added, and I think it just begs the question on what others should be added. I personally don't know enough about the other targets to make that decision.

Do you think it would be safer to just drop the implicit route so we don't overstep some of those grey-area targets?

@beeequeue
Copy link
Contributor Author

beeequeue commented Mar 5, 2021

As I said I think the current implementation will work for 90% of users, if not more.

If someone has a very special setup they are most likely an advanced user and should be able to debug and find our solution and how to disable it if they need to.

Sadly getTargetsProperties is not a public API, so using it is not recommended

@@ -82,7 +83,7 @@ beforeEach(() => {

const outputDir = resolve(__dirname, `output/${hash(expect.getState().currentTestName)}`)
try {
rmdirSync(outputDir, { recursive: true })
removeSync(outputDir)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly recursive: true was added in Node 12, and as such our Node 10 tests fail if we use it.

@mrsteele
Copy link
Owner

mrsteele commented Mar 5, 2021

Looking really good. I'm about ready to push this button. Anything else left?

@beeequeue
Copy link
Contributor Author

Not that I can think of. I'm sure there'll be something after it's out though. That's just how it is 😄

@mrsteele mrsteele merged commit 67c0aeb into mrsteele:master Mar 5, 2021
@github-actions
Copy link

github-actions bot commented Mar 5, 2021

🎉 This PR is included in version 7.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Unexpected token: punc (.)
3 participants