Skip to content

Commit

Permalink
New: Attempt to improve TypeScript support by using explicit exports
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 30, 2016
1 parent f625eb8 commit a7d2324
Show file tree
Hide file tree
Showing 20 changed files with 3,778 additions and 3,702 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,23 @@ There is also an [example for streaming RPC](https://github.com/dcodeIO/protobuf

### Usage with TypeScript

```ts
/// <reference path="node_modules/protobufjs/types/protobuf.js.d.ts" />
Under node.js:

```ts
import * as protobuf from "protobufjs";
...
```

In the browser:

```ts
/// <reference path="path/to/protobuf/index.d.ts" />
import * as protobuf from "path/to/protobuf.js";
...
```

If you need long support, there is also a [TypeScript definition](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/long/index.d.ts) for that (on npm: [@types/long](https://www.npmjs.com/package/@types/long)).

See also: [Generating your own TypeScript definitions](https://github.com/dcodeIO/protobuf.js#generating-typescript-definitions-from-static-modules)

Module Structure
Expand Down Expand Up @@ -453,7 +463,7 @@ Building the documentation to `docs/`:
$> npm run docs
```

Building the TypeScript definition to `types/`:
Building the TypeScript definition to `index.d.ts`:

```
$> npm run types
Expand Down
6 changes: 4 additions & 2 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ exports.main = function(args, callback) {
}

var output = [
"// $> pbts " + process.argv.slice(2).join(" "),
"// $> pbts " + args.join(" "),
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
""
];
if (argv.name !== "protobufjs")
if (argv.name !== "protobuf")
output.push(
"import * as $protobuf from \"protobufjs\";",
""
Expand All @@ -108,6 +108,8 @@ exports.main = function(args, callback) {
fs.writeFileSync(argv.out, output);
else
process.stdout.write(output, "utf8");
if (callback)
callback(null);
} catch (err) {
if (callback)
callback(err);
Expand Down
21 changes: 12 additions & 9 deletions types/protobuf.js.d.ts → index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
// Generated Fri, 30 Dec 2016 12:21:10 UTC
// $> pbts --name protobuf --out index.d.ts src
// Generated Fri, 30 Dec 2016 15:32:08 UTC

declare module "protobufjs" {
export = protobuf;
export as namespace protobuf;

declare namespace protobuf {

/**
* Constructs a class instance, which is also a message prototype.
Expand Down Expand Up @@ -1358,7 +1361,7 @@ declare module "protobufjs" {
* Streaming RPC helpers.
* @namespace
*/
module rpc {
namespace rpc {

/**
* Constructs a new RPC service instance.
Expand Down Expand Up @@ -1676,7 +1679,7 @@ declare module "protobufjs" {
* Common type constants.
* @namespace
*/
module types {
namespace types {

/**
* Basic type wire types.
Expand Down Expand Up @@ -1896,7 +1899,7 @@ declare module "protobufjs" {
* Various utility functions.
* @namespace
*/
module util {
namespace util {

/**
* Returns a promise from a node-style callback function.
Expand All @@ -1913,7 +1916,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module base64 {
namespace base64 {

/**
* Calculates the byte length of a base64 encoded string.
Expand Down Expand Up @@ -2133,7 +2136,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module path {
namespace path {

/**
* Tests if the specified path is absolute.
Expand Down Expand Up @@ -2299,7 +2302,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module utf8 {
namespace utf8 {

/**
* Calculates the UTF8 byte length of a string.
Expand Down
9 changes: 6 additions & 3 deletions lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ exports.publish = function publish(taffy, opts) {

// wrap everything in a module if configured
if (options.module) {
writeln("declare module ", JSON.stringify(options.module), " {");
writeln("export = ", options.module, ";");
writeln("export as namespace " + options.module, ";");
writeln();
writeln("declare namespace ", options.module, " {");
++indent;
}

Expand Down Expand Up @@ -312,7 +315,7 @@ function handleElement(element, parent) {
// handles (just) a namespace
function handleNamespace(element, parent) {
begin(element);
writeln("module ", element.name, " {");
writeln("namespace ", element.name, " {");
++indent;
getChildrenOf(element).forEach(function(child) {
handleElement(child, element);
Expand Down Expand Up @@ -378,7 +381,7 @@ function handleClass(element, parent) {
if (innerClasses.length) {
writeln("");
begin(element);
writeln("module ", element.name, " {");
writeln("namespace ", element.name, " {");
++indent;
innerClasses.forEach(function(inner) {
handleClass(inner, element);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"serialization",
"encoding"
],
"main": "src/index",
"main": "index.js",
"types": "index.d.ts",
"bin": {
"pbjs": "bin/pbjs",
"pbts": "bin/pbts"
},
"types": "types/protobuf.js.d.ts",
"scripts": {
"bench": "node bench",
"build": "gulp",
Expand All @@ -35,7 +35,7 @@
"prepublish": "node scripts/prepublish",
"prof": "node bench/prof",
"test": "tape tests/*.js | tap-spec",
"types": "node bin/pbts --name protobufjs --out types/protobuf.js.d.ts src && node node_modules/typescript/bin/tsc types/test.ts --lib es2015 --noEmit",
"types": "node bin/pbts --name protobuf --out index.d.ts src && tsc tests/typescript.ts --lib es2015 --noEmit && tsc tests/data/test.ts --lib es2015 --noEmit",
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"make": "npm run lint && npm run test && npm run types && npm run build",
Expand All @@ -54,10 +54,10 @@
"@protobufjs/utf8": "^1.0.5"
},
"optionalDependencies": {
"long": "^3.2.0"
"long": "^3.2.0",
"@types/long": "^3.0.31"
},
"devDependencies": {
"@types/long": "^3.0.31",
"@types/node": "6.0.54",
"benchmark": "^2.1.3",
"browserify": "^13.1.1",
Expand Down
25 changes: 23 additions & 2 deletions scripts/gentests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require("fs"),
path = require("path"),
pbjs = require("../cli/pbjs");
pbjs = require("../cli/pbjs"),
pbts = require("../cli/pbts");

[
"tests/data/package.proto",
Expand All @@ -20,6 +21,26 @@ var fs = require("fs"),
], function(err) {
if (err)
throw err;
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\/runtime\"/, "\"../../runtime\""), "utf8");
var pathToRuntime = path.relative(path.dirname(out), "runtime").replace(/\\/g, "/");
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\/runtime\"/g, JSON.stringify(pathToRuntime)), "utf8");
console.log("pbjs: " + file + " -> " + out);
})
});

[
"tests/data/test.js"
]
.forEach(function(file) {
var out = file.replace(/\.js$/, ".d.ts");
pbts.main([
"--name", path.basename(out, ".d.ts"),
"--out", out,
file
], function(err) {
if (err)
throw err;
var pathToProtobufjs = path.relative(path.dirname(out), "").replace(/\\/g, "/");
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\"/g, JSON.stringify(pathToProtobufjs)), "utf8");
console.log("pbts: " + file + " -> " + out);
});
});
2 changes: 2 additions & 0 deletions src/util/aspromise/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = asPromise;

/**
* Returns a promise from a node-style callback function.
* @memberof util
Expand Down
4 changes: 3 additions & 1 deletion src/util/base64/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export = base64;

/**
* A minimal base64 implementation for number arrays.
* @memberof util
* @namespace
*/
declare module base64 {
declare namespace base64 {

/**
* Calculates the byte length of a base64 encoded string.
Expand Down
2 changes: 2 additions & 0 deletions src/util/codegen/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = codegen;

/**
* A codegen instance as returned by {@link codegen}, that also is a sprintf-like appender function.
* @typedef Codegen
Expand Down
2 changes: 2 additions & 0 deletions src/util/eventemitter/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = EventEmitter;

/**
* Constructs a new event emitter instance.
* @classdesc A minimal event emitter.
Expand Down
2 changes: 2 additions & 0 deletions src/util/extend/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = extend;

/**
* Lets the specified constructor extend `this` class.
* @memberof util
Expand Down
2 changes: 2 additions & 0 deletions src/util/fetch/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = fetch;

/**
* Node-style callback as used by {@link util.fetch}.
* @typedef FetchCallback
Expand Down
2 changes: 2 additions & 0 deletions src/util/inquire/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = inquire;

/**
* Requires a module only if available.
* @memberof util
Expand Down
4 changes: 3 additions & 1 deletion src/util/path/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export = path;

/**
* A minimal path module to resolve Unix, Windows and URL paths alike.
* @memberof util
* @namespace
*/
declare module path {
declare namespace path {

/**
* Tests if the specified path is absolute.
Expand Down
2 changes: 2 additions & 0 deletions src/util/pool/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export = pool;

/**
* An allocator as used by {@link util.pool}.
* @typedef PoolAllocator
Expand Down
4 changes: 3 additions & 1 deletion src/util/utf8/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export = utf8;

/**
* A minimal UTF8 implementation for number arrays.
* @memberof util
* @namespace
*/
declare module utf8 {
declare namespace utf8 {

/**
* Calculates the UTF8 byte length of a string.
Expand Down
2 changes: 1 addition & 1 deletion tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict"; // eslint-disable-line strict

var $protobuf = require("../../runtime");
var $protobuf = require("../../../runtime");

// Lazily resolved type references
var $lazyTypes = [];
Expand Down
Loading

0 comments on commit a7d2324

Please sign in to comment.