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

Recommendation: Simplify NPM/Node code sharing #2829

Closed
basarat opened this issue Apr 19, 2015 · 7 comments
Closed

Recommendation: Simplify NPM/Node code sharing #2829

basarat opened this issue Apr 19, 2015 · 7 comments

Comments

@basarat
Copy link
Contributor

basarat commented Apr 19, 2015

Proposal

Support typescript.definition in package.json of node packages as a part of module resolution.

Example

Consider this simple NPM package : https://www.npmjs.com/package/example-typescript-a (src: https://github.com/TypeStrong/atom-typescript-examples/tree/master/node/node_modules/example-typescript-a)

It is super simple to use from atom-typescript. Open any typescript file in atom-typescript, if you have done npm install example-typescript-a then you can just use it without any additional configuration: E.g.

import a = require('example-typescript-a');

var num = a.foo.a;
var str = a.foo.b;
console.log(num,str)

import bar = require("example-typescript-a/dist/bar");
console.log(bar.bar);

import bas = require("example-typescript-a/dist/bas");
console.log(bas.bas);

sample usage

How it works

This is because example-typescript-a specifies a location of a definition using typescript.definition :

{
    "name": "cool-module",
    "version": "1.2.3",
    "typescript": {
        "definition": "dist/cool-module.d.ts"
    }
}

And we look at node_modules up its directory and read package.json for typescript.definition, for each such module we implicitly include this definition file as a part of the current project.

Creating such a module

Creating such a module example-typescript-a is also super easy. All you need to do is a package.json like you would with any NPM package (we care about name, typescript.definition and optionally main) and tell us that you plan to use this package.json from tsconfig. Docs : https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#package

Based on this we can create the correct typescript.definition file for you on build f6 :

Future

If such a module depends on tsd definitions OR other projects that have their own typescript.definition we need a resolution algorithm for that something like take latest and ignore all others. Plan:

/cc @csnover

@basarat basarat changed the title Recommendation: NodeJS: Make typescript:definition a part of the external module resolution Recommendation: NodeJS: Make typescript:definition a part of the tsconfig.json files implicitly Apr 19, 2015
@basarat basarat changed the title Recommendation: NodeJS: Make typescript:definition a part of the tsconfig.json files implicitly Recommendation: Simplify NPM code sharing Apr 19, 2015
@basarat basarat changed the title Recommendation: Simplify NPM code sharing Recommendation: Simplify NPM/Node code sharing Apr 19, 2015
@johnnyreilly
Copy link

👍

@poelstra
Copy link

I certainly like dts-generator, and having support for it in Atom Typescript is awesome. Thanks @csnover and @basarat 🌹

If you're not afraid to npm install -g tsd@next (i.e. v0.6), you can already use tsd link to achieve a similar effect: it uses typescript.definition in package.json.

I'm sure you know that already, but it may help others coming here.

However, I fear that going the dts-generator-road (i.e. the 'wrap modules in declare module "..." road') is not going to work for the more general case of having multiple modules and especially multiple versions of them.
See #2568 (comment) (rather lengthy, sorry...) for the reasoning.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2015

The proposal we have is to use typings (or possibly types) see #2338. i know tsd is using typescript.defntions, now the question is, what is the advantages/disadvantageous of using the same name?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2015

@basarat, this looks like it should be subsumed by #2338; do you agree?

@basarat
Copy link
Contributor Author

basarat commented Apr 20, 2015

Agreed 🌹

@basarat basarat closed this as completed Apr 20, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Apr 20, 2015

Thanks 🙇

@basarat
Copy link
Contributor Author

basarat commented Apr 21, 2015

now the question is, what is the advantages/disadvantageous of using the same name?

Use the new name. Its clear that its a different feature then and we can phase out what I did previously 👍

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants