-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow granular visibility for modules #57216
Comments
Then this is not the right place to ask for it, JavaScript is defined by the ECMAScript spec, changes to the spec are made via proposals FWIW I think what you're looking for is a lint rule, not a change in syntax. |
@aduh95 please don't close it immediately as not planned. If you read the ES discourse conversation, one possible way is for the host to provide it. That's why I asked here too for this feature. Just like |
What are you expecting from Node.js though? We cannot change the JS language |
@aduh95 I know. The language change should come from the ES group. But since you are the host of the language, you can add a feature to support this through a directive. Is that possible? I mean, a token that won't break the syntax, means nothing to the ES itself, but has a very specific meaning for the host itself (Node.js). |
@aduh95 also I don't mean a lint rule. I mean gray visibility, between black and white. Not private to the file, neither public to the entire world. Only visible to the family (siblings and descendants). |
We'd have to twist the ES spec to get there, there's an assumption that the same
That really sounds like something TS could be great for Anyway, let me reopen the issue, even though I'm not convinced there's anything that can happen on Node.js side. |
@aduh95 thank you for reopening it.
In other terms, we're talking about singletons. Right? Once a module is imported, Node creates a single instance of it, and uses that instance for other imports. Did I get it right?
I didn't understand this part though. Why changes to that assumption?
Yes, the TS team can add its own keyword and make some files Can't it be done by an |
In your example of |
@aduh95, I think I can give you some examples. That might help clarify:
This example breaks. Because
This resolves. Everyone can import it. Even I think if I package my directory and publish it to npm, it's still importable (I'm not sure though). Now an example of partial visibility:
I hope those examples make the requirement clearer. The |
The host can’t provide syntax in a practical sense, and directives are not a good API. The only way i could see node providing something like this is with separate metadata - and it already does via the “exports” field in packages, somewhat. This seems much better handled by a linter, and eslint-plugin-import already has rules for exactly that. |
@ljharb, thanks for the response:
what argument or evidence supports that? Because big players use them.
That's why I mean exactly. Even if the metadata is in the file name, like for example "Utils.Internal.js" or even if it comes with a special extension like
That would be useful too. I looked at the repo, but could not find the rule. Can you give me a link or so? |
Strict mode is why we know directives are not a good API for JavaScript; “use client” isn’t a standard and isn’t universally well-regarded; shebangs are an unavoidable shell legacy, and everything else isn’t JS. By separate metadata i mean another file - using anything in the filename but the extension would break a lot of tooling and conventions. Both https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-internal-modules.md and https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-restricted-paths.md can be used for this purpose. |
For example, you can rename the files you don't want to expose to end in |
@aduh95 I think that is for an npm package, right? I'm inside a directory. |
@ljharb, with respect, you just say "We know it's bad" without giving an argument, or even your personal experience explaining why it's bad. Regarding the filenames, I understood that if we put anything in the file name (except the extension) then the tools break. How about an extension? We already have .js, .jsx, .tsx, .cjs, and .mjs. These are what I know, we may have other extensions too that I don't know. Is it possible to add the I looked at the links you sent. Examples are not very clear. I need to read them multiple times. I guess the no-restricted-path is not suitable in this case. It's a conditional import for the server/client. The condition I have in mind is not based on environment, it's based on family. A family means the current directory and every subdirectory (recursive). |
You don’t have to publish your package on npm for it to work, thanks to self-referencing imports; you’d still need a linter to force imports to not use relative specifier to import outside the current directory though |
@aduh95, I read self-referencing imports. I didn't understand it. I don't know how that can help. Thanks for your help though. |
What is the problem this feature will solve?
I want to share some code only amongst the sibling or descendent files. For example, consider this directory structure:
I want the
/Core/App/Utils.js
to be visible only to theExtract.js
andMigrate.js
files.What is the feature you are proposing to solve the problem?
A directive at the top of the file:
What alternatives have you considered?
I reported this requirement to the JS discourse group:
https://es.discourse.group/t/do-we-have-other-granular-levels-for-visibility/2308/1
I think it's better to add a keyword to the language itself:
The text was updated successfully, but these errors were encountered: