Skip to content

Recommendation: Simplify NPM/Node code sharing #2829

Closed
@basarat

Description

@basarat

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions