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

Vite plugin for FOSJsRoutingBundle #24

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

SkipTheDragon
Copy link

No description provided.

Copy link

netlify bot commented Apr 14, 2024

Deploy Preview for cosmic-bubblegum-aa631a ready!

Name Link
🔨 Latest commit aeb0094
🔍 Latest deploy log https://app.netlify.com/sites/cosmic-bubblegum-aa631a/deploys/66e9a3f1b4fa5b000865fd19
😎 Deploy Preview https://deploy-preview-24--cosmic-bubblegum-aa631a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@SkipTheDragon
Copy link
Author

SkipTheDragon commented Apr 14, 2024

I have used some hooks for rollup that are not inside Vite's Plugin type. So I casted it to Plugin, please advise if there is an alternative or if I didn't use the right hooks.

TODO:

  • Documentation
  • Tests
  • Playground

P.S. This is my first Vite plugin.

@lhapaipai
Copy link
Owner

lhapaipai commented Apr 20, 2024

Hi @SkipTheDragon,
First, thank you for what you started doing !
To facilitate your development I advise you to use your own playground. I created one for you, you can reuse it and personalize it as you wish.
https://github.com/lhapaipai/symfony-vite-dev/tree/skipthedragon-feature/fos-routing
and of course I advise you to use the "vite-plugin-inspect" plugin intensively to see what code your plugin generates.

some ideas and recommendations :

  • can you associate your IDE with prettier src/vite-plugin-symfony/.prettierrc so that the TypeScript code is consistent between all files written by contributors
  • an entry point generally contains imports to other files which are likely to define import Routing from "fos-router";
    our setRoutingData will only need to be called once per entry point (check that with "vite-plugin-inspect")
  • what if user writes import Routing from 'fos-router'; (simple quote ?) or import MyRouter from "fos-router"; or if he use "symfony-ts-router" ?
  • I don't recommend using the hook handleHotUpdate because it can become very complex. maybe start by simply reloading the vite dev server if the fos-routes.json file has been updated? (see full reload vite dev server if twig files are modified src/vite-plugin-symfony/src/entrypoints/index.ts) for the other hooks it seems good to me

Good luck for the future...

ps : maybe as a high priority look for prettier

src/vite-plugin-symfony/src/fos-routing/index.ts Outdated Show resolved Hide resolved
src/vite-plugin-symfony/src/index.ts Outdated Show resolved Hide resolved
@SkipTheDragon
Copy link
Author

SkipTheDragon commented Apr 22, 2024

Hi, thanks for the playground and feedback!

I have already solved some of the raised concerns, hopefully I will have everything ready by the end of next week.

As a status update for this PR, the following are WIP:

  • Writing tests
  • Docs
  • This point from the previous message:

an entry point generally contains imports to other files which are likely to define import Routing from "fos-router";
our setRoutingData will only need to be called once per entry point (check that with "vite-plugin-inspect")

@SkipTheDragon SkipTheDragon marked this pull request as ready for review June 3, 2024 20:49
@SkipTheDragon SkipTheDragon requested a review from lhapaipai June 3, 2024 20:56
@SkipTheDragon
Copy link
Author

I'm not quite sure on how to solve the pnpm-lock.yaml conflict, but I can look into it if needed.

@lhapaipai
Copy link
Owner

hi @SkipTheDragon , thank you for your contribution and sorry for my late response. given the amount of code I haven't yet taken the time to do my review. but your contribution is very appreciated and as soon as I have a little time I will take care of doing the review. have a nice week end !

Copy link
Owner

@lhapaipai lhapaipai left a comment

Choose a reason for hiding this comment

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

hi @SkipTheDragon !
I finally took the time to do the code review. sorry again for the delay... hope you don't mind diving back into the code
First, thank you very much for your great work 🙏🙏.

from a general point of view the whole thing holds together very well and that's the main thing! so I left you 2 - 3 comments here and there. afterwards I will have to look in more depth at the transform function of the file src/vite-plugin-symfony/src/fos-routing/index.ts I admit that given the lateness of the hour I did not had the courage to tackle the regular expression 🤯🤯

good night !!

docs/src/fos-routing/installation.md Show resolved Hide resolved
docs/src/fos-routing/configuration.md Show resolved Hide resolved
docs/src/fos-routing/configuration.md Outdated Show resolved Hide resolved
docs/src/fos-routing/in-depth.md Outdated Show resolved Hide resolved
src/vite-plugin-symfony/src/fos-routing/index.ts Outdated Show resolved Hide resolved
src/vite-plugin-symfony/src/fos-routing/index.ts Outdated Show resolved Hide resolved
src/vite-plugin-symfony/src/fos-routing/index.ts Outdated Show resolved Hide resolved
src/vite-plugin-symfony/src/fos-routing/index.ts Outdated Show resolved Hide resolved
playground/fos-js-routing/vite.config.js Show resolved Hide resolved
@lhapaipai
Copy link
Owner

lhapaipai commented Jun 22, 2024

last thing.
I don't know why but for me if I import fos-router from ./vendor/friendsofsymfony/jsrouting-bundle/Resources/ with a package manager other than yarn (npm or pnpm) it doesn't work (this is why I imported the npm package 'fos-router' (https://www.npmjs.com/package/fos-router) in the playground

this crashes in my code

import Routing from "fos-router";

it does not recognize the default import. I suspect a problem between the es module and the umd format for local packages. but I don't fully understand the subject.
Can you tell me if you also encounter this issue ?
Thanks

@lhapaipai
Copy link
Owner

hi @SkipTheDragon,
finally for the pnpm-lock.yaml conflict, simply take the pnpm-lock.yaml content from the main branch, put it on your branch and run pnpm i. It should work otherwise I will take care of it.

@lhapaipai
Copy link
Owner

lhapaipai commented Jun 23, 2024

last thing. I don't know why but for me if I import fos-router from ./vendor/friendsofsymfony/jsrouting-bundle/Resources/ with a package manager other than yarn (npm or pnpm) it doesn't work (this is why I imported the npm package 'fos-router' (https://www.npmjs.com/package/fos-router) in the playground

this crashes in my code

import Routing from "fos-router";

it does not recognize the default import. I suspect a problem between the es module and the umd format for local packages. but I don't fully understand the subject. Can you tell me if you also encounter this issue ? Thanks

ok I found the solution, it's because the 'fos-router' package is installed from local system and it's a commonjs package.

explanation Automatic dependency discovery

solution:

import { defineConfig } from "vite";
import symfonyPlugin from "vite-plugin-symfony";

export default defineConfig({
  plugins: [symfonyPlugin()],
  // that is for vite dev server
  optimizeDeps: {
    include: ["fos-router"],
  },
  build: {
    rollupOptions: {
      input: {
        app: "./assets/app.js",
      },
    },
    // that is for the build step
    // for the @rollup/plugin-commonjs
    commonjsOptions: {
      include: [
        "vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js",
        /node_modules/,
      ],
    },
  },
});

another solution would be to ask FOSJsRoutingBundle to export a ESModule compatible export of their package (just a transpiled version of their router.ts would be enough ?).
Actually, their code is wrapped into a js/router.template.js
https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/gulpfile.js

// https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/package.json
{
  "name": "fos-router",
  "version": "2.5.0",
  "main": "public/js/router.js",
  "module": "public/js/router.mjs",
 ...
}

or to maintain an up to date version of their package in npmjs cf your comment

@SkipTheDragon
Copy link
Author

Hi, thanks for the thorough review, I'll see what I can do. :)

@SkipTheDragon
Copy link
Author

last thing. I don't know why but for me if I import fos-router from ./vendor/friendsofsymfony/jsrouting-bundle/Resources/ with a package manager other than yarn (npm or pnpm) it doesn't work (this is why I imported the npm package 'fos-router' (https://www.npmjs.com/package/fos-router) in the playground
this crashes in my code

import Routing from "fos-router";

it does not recognize the default import. I suspect a problem between the es module and the umd format for local packages. but I don't fully understand the subject. Can you tell me if you also encounter this issue ? Thanks

ok I found the solution, it's because the 'fos-router' package is installed from local system and it's a commonjs package.

explanation Automatic dependency discovery

solution:

import { defineConfig } from "vite";
import symfonyPlugin from "vite-plugin-symfony";

export default defineConfig({
  plugins: [symfonyPlugin()],
  // that is for vite dev server
  optimizeDeps: {
    include: ["fos-router"],
  },
  build: {
    rollupOptions: {
      input: {
        app: "./assets/app.js",
      },
    },
    // that is for the build step
    // for the @rollup/plugin-commonjs
    commonjsOptions: {
      include: [
        "vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js",
        /node_modules/,
      ],
    },
  },
});

another solution would be to ask FOSJsRoutingBundle to export a ESModule compatible export of their package (just a transpiled version of their router.ts would be enough ?). Actually, their code is wrapped into a js/router.template.js https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/gulpfile.js

// https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/package.json
{
  "name": "fos-router",
  "version": "2.5.0",
  "main": "public/js/router.js",
  "module": "public/js/router.mjs",
 ...
}

or to maintain an up to date version of their package in npmjs cf your comment

I have added this in the docs for now, until they add it on NPM or I find some time to add it myself.

@SkipTheDragon SkipTheDragon changed the title [WIP] Vite plugin for FOSJsRoutingBundle Vite plugin for FOSJsRoutingBundle Aug 10, 2024
*/
const defaultFosRouterPluginOptions = {
args: {
target: "var/cache/fosRoutes.json",
Copy link
Owner

Choose a reason for hiding this comment

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

I am bothered by this target which is by default in the server.watch.ignored (src/vite-plugin-symfony/src/entrypoints/index.ts) we could add an exception this poses us 2 problems.

  • the 2 plugins are dependent on each other at the config level
  • if a developer defines the server.watch.ignored option by himself, he will not think to pay attention to the fosRouting.json file.

Copy link
Owner

Choose a reason for hiding this comment

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

Hi @SkipTheDragon,
We have one last blocker left with this
what do you think about target path ?

Copy link
Author

Choose a reason for hiding this comment

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

I don't really understand the problem, the target does not have anything to do with server.watch.ignored.

Copy link
Owner

Choose a reason for hiding this comment

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

However, you have written a case with the target here : https://github.com/SkipTheDragon/symfony-vite-dev/blob/aeb00940877ea7eb9d335565ecf48c35c288f385/src/vite-plugin-symfony/src/fos-routing/index.ts#L106

so you wanted to listen for changes in the json file to reload the page ?
currently this scenario cannot occur

Copy link
Author

Choose a reason for hiding this comment

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

I am not quite sure how to solve this, and my time has been pretty limited these past months, I don't know when i will be able to look deeper into this.

Copy link
Owner

Choose a reason for hiding this comment

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

no problem, I will see and I will offer you a solution.
like you I haven't spent much time on this project in recent weeks but it would be cool if we could merge this pr.

Copy link
Owner

@lhapaipai lhapaipai left a comment

Choose a reason for hiding this comment

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

Hi @SkipTheDragon,
I applied a small review last night that I applied on the main branch (in the meantime I had switched TypeScript to strict mode which caused some errors)
https://github.com/lhapaipai/symfony-vite-dev/tree/skipthedragon/fos-routing

I left you some comments on some blockers that I have and that require your approval to continue and I will correct this on the code then I will translate the doc into fr.

I think I will merge your code for the next major release in a few days.

It will be great there will be lots of new features !!

Thank you !! and have a nice day

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.

2 participants