TypeStrong / dts-bundle

Export TypeScript .d.ts files as an external module definition
MIT License
308 stars 57 forks source link

support for absolute imports #46

Closed dgoldstein0 closed 7 years ago

dgoldstein0 commented 7 years ago

currently any absolute imports (resolved relative to the baseDir) - e.g.

import { NestedNavLevel } from 'modules/clean/react/maestro_nav/maestro_nav_src/nested-nav';

are not followed by dts-bundle; instead, they seem to always be ignored - copied to the output as is, with no declare module ... {...} generated in the output for them. AFAICT dts-bundle only understands relative imports (used mainly in commonjs).

I was looking for a config-only workaround to this and discovered that if I use externals: true and try to exclude: /^react$/ I get fairly close to what I want, but it still ends up crashing here: https://github.com/TypeStrong/dts-bundle/blob/master/lib/index.ts#L270

/srv/server/metaserver/build_static/js_tools/node_modules/dts-bundle/lib/index.js:173
                if (isExclude(path.relative(baseDir, p.file), true)) {
                                                      ^
TypeError: Cannot read property 'file' of undefined
    at /srv/server/metaserver/build_static/js_tools/node_modules/dts-bundle/lib/index.js:173:55
    at Array.forEach (native)
    at Object.bundle (/srv/server/metaserver/build_static/js_tools/node_modules/dts-bundle/lib/index.js:166:35)
    at Object.<anonymous> (/srv/server/metaserver/build_static/js_tools/declaration_bundler.js:12:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)

A few console.log's later and I've learned that exportMap is empty, but I don't really understand what it is expected to represent. I've attempted to change baseDir to make things work but from reading the code it doesn't seem like it'll help at all. My best guess is that it doesn't understand that these are actually local files - it thinks they are node_modules - and is somehow hoping I've given some instructions as to how to handle them.

At the moment it seems like my best option is to attempt to fork this project (and maybe send a patch?) as I can't tell that any possible configuration values will solve my conundrum. If there's some configuration options I could use to accomplish this that I've missed, please let me know.

dgoldstein0 commented 7 years ago

btw I've decided to hack on dts-generator instead of dts-bundle, so doesn't matter a ton to me if this gets fixed, but will leave this here as a record of the deficiency.

tolemac commented 7 years ago

Pull requests are welcome.