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(remix-dev/cli/migrate): add convert-to-javascript migration #3150

Merged

Conversation

MichaelDeBoey
Copy link
Member

@MichaelDeBoey MichaelDeBoey commented May 10, 2022

When working on supporting the isTypeScript variable in remix.init in our default stacks (see remix-run/indie-stack#63, remix-run/blues-stack#57 & remix-run/grunge-stack#50), I needed a way to transform all import statements in files outside the app folder from

import Prisma from 'prisma';
import { foo } from 'bar';

to

const Prisma = require('prisma');
const { foo } = require('bar');

As discussed with @kentcdodds & @mcansh, it would be a good idea to do this transform for all stacks, so we came to the conclusion that we should have something inside the @remix-run/dev package for that.

We're currently already have a convertTemplateToJavaScript function, that's used in our create command

if (!useTypeScript) {
await convertTemplateToJavaScript(projectDir);
}

but as it's name says, that's only converting all files from TS to JS (which in practice is just removing all type-related things and leave the rest of the code as-is).
let result = babel.transformSync(source, {
filename,
presets: [[babelPresetTypeScript, { jsx: "preserve" }]],
plugins: [babelPluginSyntaxJSX],
compact: false,
retainLines: true,
cwd: projectDir,
});

This PR will add the necessary changes to the import statements in all files outside the app folder.

I took the liberty to also include @mcansh's changes to convertTemplateToJavaScript from #3012, as these are also some changes that all stacks could benefit from.

To see how this transform works, you can watch it on AST explorer.


I'll create a follow-up PR to remove the convertTemplateToJavaScript function from the create command & replace it by this new migration.

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch 3 times, most recently from 7e02d37 to 6832f06 Compare May 10, 2022 22:57
@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 6832f06 to b1d17a0 Compare May 12, 2022 09:14
@remix-run remix-run deleted a comment from changeset-bot bot May 12, 2022
@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from b1d17a0 to 83e4006 Compare May 22, 2022 14:30
@MichaelDeBoey MichaelDeBoey marked this pull request as ready for review May 22, 2022 14:34
@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 83e4006 to 7fdc365 Compare May 22, 2022 14:39
Copy link
Member

@kentcdodds kentcdodds 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 good!

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 7fdc365 to f82a697 Compare May 22, 2022 15:27
@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch 2 times, most recently from d0160db to 5c04ce1 Compare May 22, 2022 15:36
@kentcdodds
Copy link
Member

I think we want to remove as much as possible without reducing the level of confidence that the feature works. I should write a blog post about this, but it's related to what I talk about here: https://kcd.im/aha-testing. Most of those routes don't need to be there for example. Let's reduce it down to as little as possible please. Thanks!

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 5c04ce1 to c35e39f Compare May 24, 2022 08:05
@MichaelDeBoey
Copy link
Member Author

Kept the index route from the templates in app/routes + removed all non-JS/TS-files outside app

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from c35e39f to 312da08 Compare May 24, 2022 13:38
@pcattori
Copy link
Contributor

pcattori commented May 28, 2022

@MichaelDeBoey : Organizationally, this doesn't fit into migrations since migrations are for moving users towards compatibility with newer Remix versions and conceptually are moving users towards what we think is a better way.

But convert-to-javascript is more of a stand-alone utility for users who prefer JS, isn't about upgrading Remix versions, and moves away from our recommendation to use TS. Maybe this is just a naming thing, but I'd like to explore a different namespace for this other than migrations. We can still reuse all the migration utils for it. Maybe we don't find something else better than migrations, but I think its worth considering.

@MichaelDeBoey
Copy link
Member Author

MichaelDeBoey commented May 29, 2022

@pcattori I can't think of another/better name tbh.
If you don't want it to be public, we can always remove it from the list?

I wouldn't say migrations are only for updating remix versions though, as we can do all sort of things with it I think.

The idea came from a codemod kind of view and because we were doing more than only changing code, it made sense to rename it to migrations, but I don't think we should limit it to only update remix versions.
We could even create a migration to automate a transition from other frameworks (like CRA of Next) to Remix at one point if we ever want to have that.

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch 2 times, most recently from 71cf6f8 to 1356691 Compare June 3, 2022 00:44
@kentcdodds
Copy link
Member

I've asked @chaance to work on moving stuff out of the CLI this week (including the migrations stuff). We'll still keep the option, but we're going to move the code into a separate package (which the CLI would basically run npx for). The goal is to slim down the CLI package and remove everything that's not frequently used.

I think this could probably go into that package when he's gotten that set up. I'll ping Chance about this so he's aware that the package he's working on may be for more than just "migrations." It's more for "one-time-scripts" generally. Maybe it could be called @remix-run/scripts.

@MichaelDeBoey
Copy link
Member Author

Once the refactor is finished, I'm happy to rebase my PR & move files accordingly.

On the naming part: @remix-run/scripts sounds more like an internal package to me tbh.
Can't think of a better name than migrations or scripts though 🤔😕

@kentcdodds
Copy link
Member

FWIW, there's react-scripts which isn't an internal package.

Also, people won't be interfacing with @remix-run/scripts directly very often anyway, so I think it's ok.

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch 2 times, most recently from 24afe9e to 300b65a Compare June 8, 2022 18:09
Copy link
Collaborator

@mcansh mcansh left a comment

Choose a reason for hiding this comment

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

I'm not super familiar (or at all 🙈 actually) with jscodeshift, but this LGTM

@kentcdodds
Copy link
Member

I'm sorry I haven't had a chance to review this PR yet. We're going to need to hold off for a little while longer while we decide some stuff on the scripts package.

@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 61d4aff to 341d238 Compare June 15, 2022 10:36
@MichaelDeBoey MichaelDeBoey force-pushed the add-convert-to-javascript-migration branch from 341d238 to 1013c25 Compare June 17, 2022 11:01
@kentcdodds
Copy link
Member

We decided to wait on the scripts package. Reviewing now.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

This looks good to me 👍 Thank you for working on this!

@kentcdodds kentcdodds merged commit 556cd2c into remix-run:dev Jun 20, 2022
@kentcdodds
Copy link
Member

@chaance, when the next release goes out, let's add this to the release notes for this one:

As usual, the Remix CLI will prompt developers whether they want to remove TypeScript if the template includes a tsconfig.json file. Previously, only the TypeScript files in the app directory were updated to JavaScript. Now all files in the project will be converted to JavaScript. Make sure you account for this in your remix.init script.

@MichaelDeBoey
Copy link
Member Author

MichaelDeBoey commented Jun 21, 2022

We decided to wait on the scripts package.

@kentcdodds I think you meant "not to wait..."? 🙈

@chaance, when the next release goes out, let's add this to the release notes for this one:

This is not correct though.
This PR is only adding the migration.

The move in the CLI will happen in a follow-up PR, as I didn't want to clutter this PR to much and really keep it focussed on the migration itself.

@MichaelDeBoey MichaelDeBoey deleted the add-convert-to-javascript-migration branch June 21, 2022 10:55
@kentcdodds
Copy link
Member

Correct on both counts 😅 thanks @MichaelDeBoey

@MichaelDeBoey
Copy link
Member Author

@kentcdodds This migration will be internally used once #3529 is merged

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version v0.0.0-nightly-556cd2c-20220621 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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.

None yet

4 participants