Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Remapping Module Alias Names #558

Open
3 tasks done
kitsonk opened this issue May 27, 2016 · 3 comments
Open
3 tasks done

Remapping Module Alias Names #558

kitsonk opened this issue May 27, 2016 · 3 comments

Comments

@kitsonk
Copy link

kitsonk commented May 27, 2016

Prerequisites

  • Please first check README. There are breaking changes from 0.x to 1.0
  • Did you check the FAQ?
  • Did you search open and closed issues to see if your issue has already been reported?

Description

Ability to remap module names. In cases where other loaders are being used and TypeScripts moduleResolution is set to classic (and with TypeScript 2.0, where paths will be able to be supplied), there can be a disconnect between the module ID managed by typings and the location of the corresponding emitted code.

For example, if I have a ./node_modules/my-package/dist/amd/foo.js and a ./node_modules/my-package/dist/amd/foo.d.ts I would configure my package loader so that my-package/foo would resolve to node_modules/my-package/dist/amd/foo.js and therefore in my TypeScript code, I would want to import foo from 'my-package/foo'; to resolve to the typings in ./node_modules/my-package/dist/amd/foo.d.ts.

As far as I can tell though, typings will always create the "real" module as ~my-package/dist/amd/foo and alias the module to my-package/dist/amd/foo, neither of which will represent the run-time configuration.

I have read the documentation and open and closed tickets... While I found a couple things that sort of maybe would have helped, I have been unable to find a way of configuring a typings.json to be able to import a package and remap the module names.

As I mention above, in TypeScript 2.0 (which is unreleased as of this writing) there will be an ability to explicitly remap paths (see: Microsoft/TypeScript#5039) and this should allow TypeScript to resolve .ts files in other locations and sort of accomplish the above, but it would not provide the additional management of dependencies, etc. that typings/typings brings.

Currently we use dts-generator to general ambient/global definitions that are mapped, but then that of course causes other challenges of trying to manage ambient/global definitions.

Steps to Reproduce

  1. Read lots of documentation
  2. Mash lots of keys
  3. Swear at laptop
  4. Open an issue, wondering what I am missing

Expected behavior: [What did you expect to happen?]

Find a way to remap/alias/use a different root when using typings/typings.

Actual behavior: [What actually happened?]

Unable to find a way.

Versions

1.0.4

@blakeembrey
Copy link
Member

There isn't currently any way to achieve this. I'd be open to a proposal, but I'm also hoping more of this would work at the TypeScript level. If TypeScript is remapping module paths, why wouldn't it automatically remap the global module paths to match?

@kitsonk
Copy link
Author

kitsonk commented May 28, 2016

For < TS 2.0, TypeScript does no remapping. We have used classic module resolution and expressed our typings as global typings, where the MID matches where the run-time location will be. We then keep the AMD loader configured to match that run-time location.

For example, if we had a module package/dist/umd/foo.js we would emit in our package.d.ts file:

declare module 'package/foo' {
  export foo;
}

And we would configure in our AMD loader:

{
  packages: [
    { name: 'package', location: 'node_modules/package/dist/umd' }
  ]
}

As mentioned above though, typings will always emit package/dist/umd/foo.d.ts:

declare module '~package/dist/umd/foo' {
  export foo;
}

declare module 'package/dist/umd/foo' {
  import alias = require('~package/dist/umd/foo');
  export = alias;
}

Although I haven't tried it, for TypeScript 2, in the tsconfig.json I would be able to supply the following:

{
  "paths": {
    "package/*": [ "node_modules/package/dist/umd/*" ]
  }
}

And that would essentially allow TypeScript to resolve a reference to 'package/foo' to find a match in /node_modules/package/dist/umd/foo.d.ts based on the resolution logic.

I am not sure how TypeScript will be manifesting that in the TypeScript services, but even then it would only inform of a possible way of resolving the reference, versus actually giving a definitive name for a module.

So as far as a proposal, what would work for our use case (though I don't know if it would work for everyone) is to allow something like this in typings.json:

{
  "dependencies": {
    "package": {
      "typings": "npm:package",
      "rootDir": "dist/umd"
    }
  }
}

Where the rootDir would imply that modules should be calculated in reference to that path, which would be relative to the base of the npm package.

@kitsonk
Copy link
Author

kitsonk commented May 28, 2016

Just to follow up on module resolution in TypeScript 2.0.

The following configuration in the tsconfig.json allows the compiler to resolve the module of package/foo to ./node_modules/package/dist/umd/foo.d.ts:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "package/*": [ "./node_modules/package/dist/umd/*" ]
        }
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants