While exploring the PubSub Feature of Lightning Web Components and the need to create and import helper JS for the functionality I came across a scenario I don't quite understand and would like some clarification. In order for two components on the same page (with no relationship to one another) to communicate with one another I had create and import a pubsub helper JS. Information on the PubSub can be found here:
Background I have two projects each with LWC inside. These two LWC needed to be able to communicate with one another across the page. In order to accomplish this I created a LWC called 'Pubsub' in one of the Project folders. I was then able to import the Pubsub JS into both LWC in both Projects. So questions come into folder/file structure for LWC and importing files along with best practices.
- Are you able to import JS files from other LWC that are not in the same Project? I would assume yes, as this is the scenario I found.
- Would it not be better to then create a separate Project, that is lets say a 'helper project' with LWC similar to that of pubsub - that can referenced by any other project LWC. Thoughts are to help with file structure and organization
- Could I not just include another JS file in the LWC and reference it there, this way I could know exactly what I am importing without having to hunt down hidden JS.
- In order to import this custom JS I had to do the following:
import { registerListener, unregisterAllListeners, fireEvent } from 'c/pubsub';
-- What is c/? I am not sure I totally understand the overall file structure when it comes to importing external JS and LWC. - Is there a best practice? If so, what is it? I see little to no documentation on this.