-
Notifications
You must be signed in to change notification settings - Fork 109
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
Resolving module's name within node_modules #1039
Comments
externs are always global (in TS lingo: scripts, not modules). But typings can be modules, containing import/export statements as you observe here. We don't yet have a good plan for how to reconcile these together. Currently if you give tsickle a d.ts that is a module, it puts all of the types into a fake namespace that Closure doesn't even see, so I am not sure it helps you too much. We'd love your help on this, but first I'd like to understand your plan for how this works, independently of node_modules. |
In my use case, that namespace being fake is not a problem; it needs to be consistent across different I am currently grabbing the fake namespace (which is predictable) to use as a type declaration of global variable, and replacing imports of such modules to a reference to a global variable, similar to how one provides an external imported module as a global variable by |
(Wow, tscc looks neat! I would love it if you could tell us how tsickle ought to work to make tscc easier. For example, maybe we should just remove the tsickle binary (main.ts) entirely, and instead say it's only a library and that if you wanna run it yourself you should use tscc? I would warn you that we make breaking API changes frequently though. I am confused by Can you describe the inconsistent namespace thing in more detail then? I guess I don't follow how node_modules is specifically involved, relative to the more general issue of "we don't handle module typings". |
I created a demo for the issue - https://github.com/theseanl/ts-declare-module-test off topicThanks for your words! Currently there were something that weren’t possible with public APIs, like blacklisting types for external modules. In tscc setup, if a module import * as React from ‘react’should not produce something like goog.requireType(“node_modules.react.umd.react_production_min”)so I am hacking isBlacklisted function exported from type_translator.ts . This is produced by jsDocTransformer and I’m not sure if I can use some source transformer to remove the import before jsDocTransformer and keep the source file to be a valid typescript. Would there be a way to provide such a functionality to blacklist type by ts.Symbol.name as a public API?
Regarding |
I think there are maybe two problems here:
So I think we here should maybe figure out module augmentation in the non-node_modules case (like your example) and the come back to the larger issue of what to do about imports from paths like 'react'. The latter touches on some much larger problems with how to make libraries like react work in closure compiler at all. (If you're a googler, read go/tpl-js.) |
…1039 fix boolean arguments passing, minor refactoring
I thought module augmentation was working well in general, the only problem here was the handling of node_modules. I'm not sure what you are talking about the point 1. My example shows problem only when node_modules are involved. So here IMO only the node_modules case needs to be fixed and the larger problem is less relevant. To fix an immediate issue of using Regarding the general issue, I'm not a googler so I have no idea about how external library is used with closure compiler internally, but many module bundlers like rollup and webpack have a way of marking certain module as external, so I guess such a functionality may be added to closure compiler, or tsickle creates externs for a hypothetical |
I wrote a longer thing above for you, let me know if it helps. I believe the problematic resolveModuleName code you wrote was intended for the case where someone runs tsickle first and then runs the output through something other than Closure, which seems like madness to me. I think Angular did this for a while for reasons I don't fully understand. I think I agree that tsickle should drop this special-casing since (as I describe in #1041) it really just doesn't work. I don't really understand your solution, do you run the closure output through rollup afterwards? |
In reality, I'm passing the generated code directly to Closure.
No, rollup is a separate, alternative compilation means. Closure output is the final output. It's unclear to me what is unclear about my use case to you; In the current situation, when tsickle is run on https://github.com/theseanl/ts-declare-module-test, the output externs.js file cannot be fed to Closure, it will produce errors about undeclared namespaces. If the My idea have been to make the option 2 in your explanation #1041 (Thanks for writing it) systematic:
As I understand, the goal of tsickle is to produce codes that are at least consumable by Closure. The current state is that when it is fed with certain files spread across node_modules boundary, the output extern will not be consumable by Closure. That's why I expect this issue to be fixed from the tsickle's side. |
@alexeagle I think is the author of the node_modules special case, maybe he can say whether it's safe to remove? |
I'm trying to use tsickle to write externs from type declarations from DefinitelyTyped packages.
Although typescript does not transpile *.ts files in
node_modules/@types
by default, by manually providing them to the compiler, it was possible to trigger tsickle to generate externs for them.However, for type packages that are using module name with relative paths (e.g. lodash), tsickle generates broken externs, because module names are resolved with
resolveModuleName
function and the resolved path is in a node_modules directory relative to the current typescript project root. Then tsickle does not resolve such names and writes externs on inconsistent namespaces.Would there be any way to properly generate externs in such a use case?
It would have worked if
resolveModuleName
have discarded resolved path iff it is piercingnode_modules
package boundary, i.e.node_module
directory's child directory.resolved
and returnimported
literally only if a package boundary ofpathOfImportingFile
andresolvedModule
is different.and it looks like to be a behavior that was intended from the beginning, if such a change would not break other projects using tsickle, would it be possible to adopt such changes?
The text was updated successfully, but these errors were encountered: