Skip to content
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

Module resolution strategy for libraries created in Typescript #11809

Closed
davnn opened this issue Oct 24, 2016 · 7 comments
Closed

Module resolution strategy for libraries created in Typescript #11809

davnn opened this issue Oct 24, 2016 · 7 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@davnn
Copy link

davnn commented Oct 24, 2016

It would be great if you could use absolute paths when creating reusable Typescript libraries, but as of now it looks like Typescript code with absolute paths is not easily reusable by other projects as every consumer would have to map the absolute paths to relative ones.

TypeScript Version: 2.0.3

Say you have a tsconfig as follows:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "baseUrl": "./src",
        "outDir": "lib"
}

The project is structure like this:
tsconfig.json
src/

  • core
  • test
  • helpers

Inside your library you can now import modules as follows:

import * as X from "core/X";

Actual behavior:
Compiled with tsc your commonjs modules now reference "core/X" and consumers of your library don't know where to find "core/X", the same is true for declaration files.

Expected behavior:
You can either let tsc convert absolute paths to relative ones or provide an exportable path mapping.

From issue 5039:

The compiler does not rewrite module names. module names are considered resource identifiers, and are mapped to the output as they appear in the source

That means rewriting to relative paths is no option, what is the option then?

@mhegazy
Copy link
Contributor

mhegazy commented Oct 24, 2016

That means rewriting to relative paths is no option

why is that? what is write import * as X from "./core/X" ?

@davnn
Copy link
Author

davnn commented Oct 26, 2016

You mean that you just should not use absolute paths for code that is going to be published somewhere?

@kitsonk
Copy link
Contributor

kitsonk commented Oct 26, 2016

UMD declaration files allow relative imports. It is only global/ambient ones that do not. All of our (@dojo) distributable packages have relative mids in their .d.ts UMD files and it doesn't cause any problems consuming them downstream.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 26, 2016

You mean that you just should not use absolute paths for code that is going to be published somewhere?

How is your library going to be used?

@davnn
Copy link
Author

davnn commented Oct 27, 2016

How is your library going to be used?

Compiled with TSC as "module": "commonjs", published via NPM and then imported in other nodeJS projects, but that does not work if the library uses absolute paths.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 27, 2016

then do not use baseUrl. baseUrl is useful for AMD scenarios where you have a release process that "merges" all modules into one location. in node, there is a well-established concept of modules, packages, and module references.

if you are using node packages, use relative imports for files within your package, and non-relative (i.e. do not start with ./ or ../, e.g. "rx/lite" or "express") for modules you get from other packages.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 27, 2016
@davnn
Copy link
Author

davnn commented Oct 27, 2016

Alright then, thank you very much.

@davnn davnn closed this as completed Oct 27, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants