vite-plugin / vite-plugin-commonjs

A pure JavaScript implementation for CommonJs
MIT License
96 stars 14 forks source link

Allowing import Id customization #45

Open VeekeeFr opened 6 months ago

VeekeeFr commented 6 months ago

Hello @yejimeiming

While converting my project packager to Vite, I encountered some issues with a CJS dependency declaring its imports with "/node_modules/..."

Example : require("/node_modules/react")

Even with your plugin, it causes some issues and I couldn't find any other options or solutions (like aliases...) other than customizing your plugin.

This MR adds a "namingRules" advanced option (function), allowing to customize any detected import to a proper value).

I hope you'll find it useful/usable and include it in your plugin.

Feel free to contact me Β²for any information

PS : Sorry if my TS code is imperfect (I'm used to JS :D )

yejimeiming commented 6 months ago

Hey! πŸ‘‹ Did someone intentionally write require('react') as require('/node_modules/react') ? Additionally, the following code can works in Vite now. πŸ‘€

require('/node_modules/react')
// ↓↓↓↓ convert ↓↓↓↓
import '/node_modules/react'
VeekeeFr commented 6 months ago

Hello πŸ‘‹

Yep, in a project dependency I have no control of :(

My project architecture is not that simple :

Dependency Project A (module with 'node_modules/react')
// ↓↓↓↓ dependency ↓↓↓↓
Dependency Project B (module => migrated to Vite)
// ↓↓↓↓ dependency ↓↓↓↓
Project C (using Webpack)

Without the patch :

Project C is able to load/work with 'node_modules/react' (webpack / conf magic ?)

Project B can only be started/tested via StoryBook. In that environnement, StoryBook fails to load "node_modules/react". That may be caused by a Storybook default Vite settings, though I tried to remove them one by one but couldn't find the culprit.

I found easier to work with your plugin :)