Permalink
Browse files

New: Attempt to improve TypeScript support by using explicit exports

  • Loading branch information...
dcodeIO committed Dec 30, 2016
1 parent f625eb8 commit a7d23240a278aac0bf01767b6096d692c09ae1ce
@@ -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
@@ -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
@@ -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\";",
""
@@ -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);
@@ -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.
@@ -1358,7 +1361,7 @@ declare module "protobufjs" {
* Streaming RPC helpers.
* @namespace
*/
module rpc {
namespace rpc {
/**
* Constructs a new RPC service instance.
@@ -1676,7 +1679,7 @@ declare module "protobufjs" {
* Common type constants.
* @namespace
*/
module types {
namespace types {
/**
* Basic type wire types.
@@ -1896,7 +1899,7 @@ declare module "protobufjs" {
* Various utility functions.
* @namespace
*/
module util {
namespace util {
/**
* Returns a promise from a node-style callback function.
@@ -1913,7 +1916,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module base64 {
namespace base64 {
/**
* Calculates the byte length of a base64 encoded string.
@@ -2133,7 +2136,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module path {
namespace path {
/**
* Tests if the specified path is absolute.
@@ -2299,7 +2302,7 @@ declare module "protobufjs" {
* @memberof util
* @namespace
*/
module utf8 {
namespace utf8 {
/**
* Calculates the UTF8 byte length of a string.
@@ -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;
}
@@ -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);
@@ -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);
@@ -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",
@@ -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",
@@ -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",
@@ -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",
@@ -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);
});
});
@@ -1,3 +1,5 @@
export = asPromise;
/**
* Returns a promise from a node-style callback function.
* @memberof util
@@ -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.
@@ -1,3 +1,5 @@
export = codegen;
/**
* A codegen instance as returned by {@link codegen}, that also is a sprintf-like appender function.
* @typedef Codegen
@@ -1,3 +1,5 @@
export = EventEmitter;
/**
* Constructs a new event emitter instance.
* @classdesc A minimal event emitter.
@@ -1,3 +1,5 @@
export = extend;
/**
* Lets the specified constructor extend `this` class.
* @memberof util
@@ -1,3 +1,5 @@
export = fetch;
/**
* Node-style callback as used by {@link util.fetch}.
* @typedef FetchCallback
@@ -1,3 +1,5 @@
export = inquire;
/**
* Requires a module only if available.
* @memberof util
@@ -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.
@@ -1,3 +1,5 @@
export = pool;
/**
* An allocator as used by {@link util.pool}.
* @typedef PoolAllocator
@@ -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.
@@ -1,6 +1,6 @@
"use strict"; // eslint-disable-line strict
var $protobuf = require("../../runtime");
var $protobuf = require("../../../runtime");
// Lazily resolved type references
var $lazyTypes = [];
Oops, something went wrong.

0 comments on commit a7d2324

Please sign in to comment.