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

Migrate codebase to TypeScript, close #323 #406

Merged
merged 7 commits into from
Dec 15, 2021

Conversation

Kocal
Copy link
Contributor

@Kocal Kocal commented May 6, 2021

Hi 👋

This PR is a proposal for migrating the codebase to TypeScript. This way:

  • working on the "javascript-side" of the bundle will be easier, I think
  • types definitions for end-developpers using TypeScript will be synchronized with the codebase

Some things to know:

  • I've upgraded to Babel ^7, in order to use the @babel/typescript-preset
  • only the js/router.js file has been migrated to TypeScript. But I didn't rewrite the whole file, just adapting it to make TypeScript and tests happy
  • tests for types (with tsd) have been added with real genereated data. Thanks to them I was able to fix a lot of minor typings issues
  • The TypeScript module FOS does not exist anymore. TBH I was not able to re-introduce this behaviour and didn't spend too much time on this, since the majority of users will just do a import { Routing } from 'vendor/friendsofsymfony/jsrouting-bundle' I guess

WDYT? Thanks!

@Kocal
Copy link
Contributor Author

Kocal commented May 6, 2021

Hum, the CI fails and I think it's because the Node.js version is outdated. I was able to reproduce the issue on local with Node 8, but everything works fine with Node 10.:
image

Given Node.js 8 is unmaintened since a long time ago (https://nodejs.org/en/about/releases/), I think it won't be a problem to setup Node.js 10 on Travis.

@Kocal Kocal force-pushed the migrate-to-typescript branch 3 times, most recently from d8f1016 to b47af3b Compare May 6, 2021 21:11
Resources/js/router.ts Outdated Show resolved Hide resolved
Comment on lines +19 to +20
defaults: undefined[] | RouteDefaults;
requirements: undefined[] | RouteRequirements;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of JSON serialization from PHP, an empty array is serialized to [] but a non-empty array is serialized to { 'default1': '...', 'default2: '...' }

@Kocal
Copy link
Contributor Author

Kocal commented May 18, 2021

I've fixed some bugs I faced when trying to use my pull request on an existing project, and confirm the following code works (no types errors, and routes are generated correctly):

import Routing, { RouteParams } from '../../../vendor/friendsofsymfony/jsrouting-bundle/Resources';
import routes from '../../_generated/routes.json';

Routing.setRoutingData(routes);

export function generateRoute(name: string, params: RouteParams = {}, absolute = false): string {
  return Routing.generate(name, params, absolute);
}

image

Friendly ping @tobias-93

Copy link
Collaborator

@tobias-93 tobias-93 left a comment

Choose a reason for hiding this comment

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

Looks good to me, some small comments but otherwise nice job!

Resources/js/router.ts Outdated Show resolved Hide resolved
Resources/ts/router.test-d.ts Outdated Show resolved Hide resolved
Resources/ts/routes.json Outdated Show resolved Hide resolved
@stof
Copy link
Member

stof commented Jun 2, 2021

* The TypeScript module `FOS` does not exist anymore. TBH I was not able to re-introduce this behaviour and didn't spend too much time on this, since the majority of users will just do a `import { Routing } from 'vendor/friendsofsymfony/jsrouting-bundle'` I guess

This makes it a BC break for people loading the router and the routes separately rather than using a webpack (see the first code snippet in the doc at https://symfony.com/doc/current/bundles/FOSJsRoutingBundle/usage.html)

@Kocal
Copy link
Contributor Author

Kocal commented Jun 2, 2021

@tobias-93 The text has been removed and a more generic route (feed_post => eg: /feed/post.xml which deals with defaults and requirements) has been added.

@stof Sorry, maybe I was not clear enough. I was speaking about the TypeScript definition, FOS module does not exist anymore since. But fos.Router.setData is still a globally-accessible function:
image
image

@Kocal Kocal requested a review from tobias-93 June 2, 2021 18:06
@Kocal
Copy link
Contributor Author

Kocal commented Jul 18, 2021

Hi everyone, is there any news on this? Thanks!

@tobias-93
Copy link
Collaborator

Hi @Kocal,

Sorry, this bundle has been under my radar for a long time.
Please rebase on the newest master and rework the tests for Github Actions, and I will have a new look on merging this.

@Kocal Kocal force-pushed the migrate-to-typescript branch from 23681af to b9f5de6 Compare December 14, 2021 22:59
@Kocal
Copy link
Contributor Author

Kocal commented Dec 14, 2021

Hi @tobias-93, no worries! The PR has been rebased.

I will do the same for #408.

Thanks

@tobias-93 tobias-93 merged commit ed97d4c into FriendsOfSymfony:master Dec 15, 2021
@tobias-93
Copy link
Collaborator

@Kocal awesome, thanks! I will make a 3.0 release with this new implementation, including support for SF 6 and dropping old PHP versions.

@Kocal Kocal deleted the migrate-to-typescript branch December 15, 2021 09:46
@Kocal
Copy link
Contributor Author

Kocal commented Dec 15, 2021

Perfect! :)

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.

3 participants