11ty / webc

Single File Web Components
MIT License
1.33k stars 38 forks source link

Module resolution `npm:` alias mapping assumes `node_modules` and eleventy config exist in same directory #211

Open filmaj opened 3 months ago

filmaj commented 3 months ago

First of all: you rock! Love the projects, excellent work, very grateful ❤️

I described my issue in a comment in some more detail over in the eleventy-plugin-webc repo.

I have an issue loading the is-land plugin in an 11ty webc project. I have a large, complicated webapp codebase, where eleventy is but a small part of a larger system. The eleventy bits are located in a subdirectory of this project, ./public, while the package.json for this project exists one directory up, in the root of the project. Thus, node_modules exists one parent directory above where the eleventy project exists.

My particular problem boils down to two issues, I think, which are related:

  1. The fsCache module seems (to me) unnecessarily restrictive re: requiring components to be subdirectories of the "working directory"
  2. npm: component loading aliasing assumes npm: maps to ./node_modules

Issue (1) effectively prevents setting webc component loading to any directory that is not an immediate subdirectory within the eleventy project. For a monorepo style setup, this seems like an unnecessarily restrictive constraint. As mentioned, since in my setup, node_modules is one level up above the eleventy project, that exception is troublesome. Could we consider dropping the code I linked to in (1)?

Issue (2) is related to the same problem: the npm: component loading alias assumes something about the location of the node_modules directory. There is a TODO here, tantalizingly hinting at perhaps the author foreseeing an issue similar to what I'm describing here. I was thinking one way to improve this would be to search up the file tree to find the npm project root first (by looking for the presence of a package.json file), and then joining that found project root with ./node_modules. I think that would solve this issue? What do you think?

I am happy to give a PR a go to try to fix this! I wanted first to check if my thinking is correct / is a PR worth it / would the project be responsive to the issue / provide guidance on how to go about improving the situation.

Thanks for any discussion and thank you once more for an amazing open source project!

Ryuno-Ki commented 3 days ago

Issue (1) was introduced to fix #10. Limiting the scope to subdirectories is a good security measure.

I would expect the monorepo tool (lerna and friends) to handle the dependencies properly. Are you allowed to share how you manage the repo?