typescriptprotocol-buffersbrowserifytsify

Bundle typescript and protobuf.js with tsify


I used the google API to generate a lib.js from a lib.proto. In Typescript I have used it with declare var lib: any;. My problem is that browserify ignores the lib.js because it's only a declaration.

Is there any way to add the lib.js at the right place to the bundle.js?

my tsify command:

browserify -p tsify src/main.ts > bundle.js

my tsconfig:

{
  "compilerOptions": {
    "declaration": false,
    "noImplicitAny": true,
    "target": "ES6",
    "removeComments": true,
    "module": "commonjs",
    "sourceMap": true,
    "rootDir": "src",
    "moduleResolution": "node"
  }
}

my hierarchy:

root
    src
        main.ts
        lib.proto
        lib.js
        lib.d.ts
    bundle.js
    index.html
    package.json
    tsconfig.json

statment:

declare var lib: any;
let p = lib.deserializeBinary(data);

lib.d.ts


Solution

  • The problem is that you only declare the type of a lib. All module loaders will never bundle lib if you have never imported it.

    Just place require('./lib.js');, before you use the lib variable. It should work.