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

Support TS/JSX inside node_modules #53

Closed
spa5k opened this issue Feb 3, 2023 · 16 comments
Closed

Support TS/JSX inside node_modules #53

spa5k opened this issue Feb 3, 2023 · 16 comments
Labels
enhancement New feature or request

Comments

@spa5k
Copy link

spa5k commented Feb 3, 2023

[plugin:vite:import-glob] Invalid glob import syntax: Expect CallExpression, got BinaryExpression

F:/app/node_modules/.pnpm/generouted@1.7.2_vite@4.1.1/node_modules/generouted/src/react-location.tsx:9:19

7  |  type Module = { default: Element; Loader: LoaderFn; PendingElement: Element; ErrorElement: Element }
8  |  
9  |  const PRESERVED = import.meta.glob<Module>('/src/pages/(_app|404).{jsx,tsx}', { eager: true })
   |                    ^
10 |  const ROUTES = import.meta.glob<Module>(['/src/pages/**/[\\w[]*.{jsx,tsx}', '!**/(_app|404).*'])
11 |

How to reproduce-

  1. Clone this example - https://github.com/oedotme/generouted/tree/main/examples/react-location/nested-layouts
  2. Update vite config to use plugin-react-swc rather than the original one, you will get the error.
@ArnaudBarre
Copy link
Member

Hi,

I checkout the repo and tried it to migrate to SWC. I got another issue about cannot resolving react. This make sense because it's used inside your main project without having it as a dev-dependency. Adding react & react-dom as dev-deps works.

@ArnaudBarre ArnaudBarre added the invalid This doesn't seem right label Feb 10, 2023
@vallerydelexy
Copy link

Hi,

I checkout the repo and tried it to migrate to SWC. I got another issue about cannot resolving react. This make sense because it's used inside your main project without having it as a dev-dependency. Adding react & react-dom as dev-deps works.

not sure what do you mean by

Adding react & react-dom as dev-deps works.

but its not working
image

@ArnaudBarre
Copy link
Member

@spa5k
Copy link
Author

spa5k commented Feb 20, 2023

@spa5k spa5k closed this as completed Feb 20, 2023
@HarunKilic
Copy link

Not sure if that fixed the issue with Glob, but i'm still having the main issue with the latest package.

Vite: v4.1.4
@vitejs/plugin-react-swc: v3.2.0
React+-dom: v18.2.0
Generouted: v1.7.16

@ArnaudBarre
Copy link
Member

Can you provide a minimal repro?
The first repro was inside the monorepo which is a special case that was fixed

@HarunKilic
Copy link

HarunKilic commented Feb 23, 2023

Can you provide a minimal repro? The first repro was inside the monorepo which is a special case that was fixed

Sure, here you go :) Repro link

@ArnaudBarre
Copy link
Member

Thanks for the repro.
So the package ships raw TSX with Vite globs into node_modules.
For now this plugins does not transpile node modules, that why you get this error. This is a two lines patch if you want to workaround for now.
Vite takes care not inluding this into deps prebundling, I will see with the team if we should support this pattern or not

@spa5k
Copy link
Author

spa5k commented Feb 24, 2023

Can you mention the patch for it? I tried optimized deps, but it didn't seem to work.

@spa5k spa5k reopened this Feb 24, 2023
@ArnaudBarre
Copy link
Member

You need to if (id.includes("node_modules")) return;

This might be something that change in the future, I need to look on how it plays with deps prebundling (on the repo it seems that the file is not part of dependencies pre-bundling)

@ArnaudBarre ArnaudBarre changed the title Invalid glob import syntax: Expect CallExpression, got BinaryExpression, works with plugin-react, the original one but not with SWC one. Support TS/JSX inside node_modules Feb 24, 2023
@ArnaudBarre ArnaudBarre added enhancement New feature or request and removed invalid This doesn't seem right labels Feb 24, 2023
@HarunKilic
Copy link

You need to if (id.includes("node_modules")) return;

This might be something that change in the future, I need to look on how it plays with deps prebundling (on the repo it seems that the file is not part of dependencies pre-bundling)

Can you be a bit more specific about the patch fix?
What do we need to do? Add or remove the highlighted value? And where? In which package?
Thanks

@ArnaudBarre
Copy link
Member

You need to find this line in the compiled version (cjs or mjs depending on your package.json): https://github.com/vitejs/vite-plugin-react-swc/blob/main/src/index.ts#L154

@opeologist
Copy link

any way to get this into the plugin instead of having to patch it?

@TomCaserta
Copy link

TomCaserta commented Mar 20, 2023

I believe this plugin should accept includes and excludes, this can be achieved by using https://www.npmjs.com/package/@rollup/pluginutils createFilter method. By default it could filter out node modules while still giving the option to those that need it to effectively override that filter.

It's a very common pattern found in a lot of rollup plugin packages but I do not know your philosophy on adding optionality.

Would this be a good solution @ArnaudBarre ? If so I am able to create the PR to support this as its a feature I am looking for.

@ArnaudBarre
Copy link
Member

For now the philosophy of this plugin is to limit to options and support common future proof pattern out of the box. For example mdx is supported by default.

We will discuss it with the team this Thursday to see if this is a pattern that should be encouragd and maybe better supported by the core (the same question applies for .vue or .svelte (svelte plugin allows it but not vue currently)).

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Mar 27, 2023

Fixed in 4b9b2d5.

Note that for now this not supported by TS and errors from these files cannot be silenced if the user is using a stricter configuration than the library author: microsoft/TypeScript#30511. I advise to use it only for internal libraries for now.

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

No branches or pull requests

6 participants