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

[v0.36] fix(babel-hooks): Import babel hooks correctly for console, exec and dm up commands #3231

Merged
merged 16 commits into from
Aug 17, 2021

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Aug 11, 2021

Critical fix for v36 release - to be able to run rw console, rw exec and rw dm commands.

The issue was the removal of .babel.rc.js from the api side here: #3187 so these commands could not regsiter the babel hook

What does this do?

  • Fixes how the above commands register the babel hook
  • Registering babel hooks now moved to @redwoodjs/internal, so they all work consistently
  • Makes sure babel hooks are only registered inside handlers (in particular data-migrate) - this might have been why prerender was failing in certain situations
  • Makes change in Use user babel.config.js on api side. #3187 backwards compatible i.e. users no longer need to remove api/.babelrc.js - we simply ignore it

What this PR won't do

  • Modify anything on the web side, apart from how prerender loads it's babel config to be consistent and provider a pattern for future use.
  • Refactor how prerender exports stuff, now that the babel register issue may be resolved. I will do this separately

Outstanding

  • Update tests
  • Investigate prerender failure

@redwoodjs-bot redwoodjs-bot bot added this to New issues in Current-Release-Sprint Aug 11, 2021
@dac09 dac09 marked this pull request as draft August 11, 2021 10:37
['@babel/plugin-transform-typescript', undefined, 'rwjs-babel-typescript'],
[
require('@redwoodjs/core/dist/babelPlugins/babel-plugin-redwood-src-alias')
.default,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@peterp

Notice I had to add .default because I think.... with the datamigrate hook was doing the transform for us (accidentally)

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you manage to fix this by making it inline? Super weird behaviour, it should be ignore node_modules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure what you mean by inline - as in require them before the plugin block?

Copy link
Contributor

Choose a reason for hiding this comment

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

You mentioned that you changed something in datamigrate when you registered the hook, is this still a problem?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it was the opposite actually - because the cli was registering the hooks, we didn't need to do the .default. It is very strange to me as well...

@dac09
Copy link
Collaborator Author

dac09 commented Aug 11, 2021

@peterp any thoughts on how to add more tests for these?

@thedavidprice thedavidprice changed the title fix(babel-hooks): Import babel hooks correctly for console, exec and dm up commands [v0.36] fix(babel-hooks): Import babel hooks correctly for console, exec and dm up commands Aug 11, 2021
@thedavidprice thedavidprice added this to the next-release-priority milestone Aug 11, 2021
@redwoodjs-bot redwoodjs-bot bot moved this from New issues to In progress (priority) in Current-Release-Sprint Aug 11, 2021
@thedavidprice thedavidprice moved this from In progress (priority) to v0.36 in Current-Release-Sprint Aug 11, 2021
…ed-babel-location-require-hook

* 'main' of github.com:redwoodjs/redwood:
  webpack and misc upgrades (#3235)
  upgrade Prisma v2.29.0 (#3233)
  Fix API sourcemaps (babel+esbuild) for debugging (#3230)
  set canary as default (#3228)
  Fix issue where Clerk client is sometimes unset (#3224)
@dac09 dac09 marked this pull request as ready for review August 12, 2021 12:50
Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

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

Looking good ⚡

packages/internal/src/build/babel/api.ts Outdated Show resolved Hide resolved
if (fs.existsSync(p)) {
return p
} else {
return undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a difference between returning false and undefined. In the false case it doesn't search for any additional configuration - essentially turning it off, in the undefined case it invokes the traditional methods where it searches for the babel config.

We actually don't want it to search for .babelrc.js because of the way it extends the base configuration. (It would mean duplicate plugins in a lot of cases) and it would be difficult to track down.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes... sorry I should change this to false. I had it as undefined because I was using extends.

Copy link
Contributor

Choose a reason for hiding this comment

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

I read the docs, it might not actually be searching for .babelrc.js, but I would still prefer it to be turned off completely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Peter, so using false is different from using undefined. Using false just makes it fail... my suspicion babel register behaves differently to babel transform.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I'm not loving about this PR is that it feels like it's introducing more uncertainty in a process that I would love to be solid. This is the outcome I'm trying to achieve:

So this is what I want:

  1. I want people to delete api/.babelrc.js, or to convert it to api/babel.config.js and remove the "extends" part.
  2. I want this to be a breaking change if you have custom babel transformations. The reason I want it to be a breaking change is because I want to discourage people from adding their own babel transformations because I would like to move our own transformations to a faster toolchain and would not like babel to make that process difficult.

Can we work around making that a realistic goal in this PR instead of muddying the water? And is it something that we can have tests for?

  1. Test that <side>/.babelrc.js is not imported
  2. Test that <side>/babel.config.json can be imported if it's available.

Copy link
Contributor

Choose a reason for hiding this comment

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

^^ @thedavidprice Does this reasoning sound solid to you?

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterp The items 1 and 2 above regarding your plan to deprecate and why we should deprecate are SuperImportant™. I'm 100% in agreement with 2 and moving forward with implementation on 1. (But not in this PR, see "next steps" below.)

Instead of this information being buried here, however, what would be really helpful is having this plan communicated in an Issue for visibility and alignment. I'm very interested in the move to a "faster toolchain" (tell me more!), I just don't know what that means, when it would happen, what it will require, etc.

I think I understand what you mean by "introducing more uncertainty". In this PR, we are deprecating bablerc, but because it won't throw an app error anymore you are concerned it will be confusing, correct? I could go either way. Regardless, I think we can resolve this with 1) good Release Note communication and 2) a two-step process to full deprecation (e.g. move forward with v0.36 and throw if present in v0.37).

Next Steps

We've been 2 weeks on this bug as a critical-path blocker to releasing v0.36. The code here is an improvement and, most importantly, resolves the release-blocking bugs.

  • I'll approve and merge, then cut the v0.36 release branch
  • I'll open an Issue with these next steps for v0.37
  • PP, I'll leave next steps regarding communicating a plan for item 2 above (babel + faster toolchain) if applicable

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to close the loop on this -- we spoke offline.

The PR where I removed the .babelrc.js files (#3187) includes a deprecation notice for the release notes. I'll update the follow on PR #3245 with a bit more information about next steps.

Copy link
Contributor

Choose a reason for hiding this comment

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

And the current Release Notes draft is very clear about the Breaking change + Code Mods regarding I want people to delete api/.babelrc.js, or to convert it to api/babel.config.js and remove the "extends" part. — says this explicitly.

Sound good?

packages/internal/src/build/babel/common.ts Outdated Show resolved Hide resolved
packages/internal/src/build/babel/common.ts Outdated Show resolved Hide resolved
packages/internal/src/build/api.ts Show resolved Hide resolved
packages/internal/package.json Outdated Show resolved Hide resolved
['@babel/plugin-transform-typescript', undefined, 'rwjs-babel-typescript'],
[
require('@redwoodjs/core/dist/babelPlugins/babel-plugin-redwood-src-alias')
.default,
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you manage to fix this by making it inline? Super weird behaviour, it should be ignore node_modules.

Comment on lines +31 to +41
'babel-plugin-module-resolver',
{
alias: {
src: rwWebPaths.src,
},
root: [getPaths().web.base],
// needed for respecting users' custom aliases in web/.babelrc
// See https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#cwd
cwd: 'babelrc',
loglevel: 'silent', // to silence the unnecessary warnings
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the rw alias plugin I wrote not work in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suggest we don't use that here for now - because there's a chance people may have custom aliases setup (like Burnsy).

My suggestions: keep as is, and in a later PR - we add support for specifying your own aliases, and ask users to migrate to this new system.

Copy link
Collaborator Author

@dac09 dac09 Aug 12, 2021

Choose a reason for hiding this comment

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

Maybe we can be clever, and just read their tsconfig/jsconfig instead of them having to specify it somewhere else.

And get rid of the module-resolver plugin all together ☠️

Copy link
Contributor

Choose a reason for hiding this comment

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

Using ours would not invalidate them using theirs.

Comment on lines 51 to 60
[
'babel-plugin-module-resolver',
{
alias: {
$api: getPaths().api.base,
},
},
'exec-$api-module-resolver',
],
],
Copy link
Contributor

Choose a reason for hiding this comment

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

I would kick this babel plugin out and try to use the one I wrote.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mind pointng me to the plugin please? Didn't realise you added it in 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

Here you go! I really struggled with the other one:

[
require('@redwoodjs/core/dist/babelPlugins/babel-plugin-redwood-src-alias'),
{
srcAbsPath: rwjsPaths.api.src,
},
'rwjs-babel-src-alias',
],

Copy link
Contributor

Choose a reason for hiding this comment

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

It does not support multiple alias, just "srcAbsPath", but it makes the files relative in the resulting ast.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I guess in this case it WOULD not work. Sorry, super tired, but for pre-render it would work

Copy link
Collaborator Author

@dac09 dac09 Aug 12, 2021

Choose a reason for hiding this comment

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

Ah.... I think we should leave this as is for now in that case. Because one of the changes I made a while ago was to support user defined aliases (like in Burnsy's project).

What do you think of leaving this for now (to make it non-breaking) - and switch everything over (including babel-preset) and remove babel-plugin-module-resolver once we get to pre-transpiling web?

@dac09
Copy link
Collaborator Author

dac09 commented Aug 12, 2021

All comments addressed!

Copy link
Contributor

@thedavidprice thedavidprice left a comment

Choose a reason for hiding this comment

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

Approved. Moved next steps here for v0.37 "Full deprecation of .babelrc.js" #3245

Current-Release-Sprint automation moved this from v0.36 to Ready to merge Aug 17, 2021
Current-Release-Sprint automation moved this from Ready to merge to Done Aug 17, 2021
@redwoodjs-bot redwoodjs-bot bot removed this from the next-release-priority milestone Aug 17, 2021
@redwoodjs-bot redwoodjs-bot bot removed this from Done in Current-Release-Sprint Aug 17, 2021
@thedavidprice thedavidprice reopened this Aug 17, 2021
@thedavidprice thedavidprice merged commit 4112c2c into main Aug 17, 2021
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Aug 17, 2021
@thedavidprice thedavidprice deleted the fix/dc-changed-babel-location-require-hook branch August 17, 2021 00:52
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

Successfully merging this pull request may close these issues.

None yet

3 participants