简体   繁体   中英

Using browserify with typescript to include a node module

I'm attempting to use a node module in a typescript application and I've included it in my TypeScript code via require() .

I build my typescript to JS and all works well if I run it through node which is great.

I'm looking for a way to use the same code and run it through the browser. Browserify seemed like a sensible solution.

I popped a post build action on my VS project and I'm building a 206KB file via browserify (so it's certainly doing something!). Unfortunately my tiny bit of Typescript doesn't seem to be accessible when it's been browserified. I'm not that familiar with what browserify should be generating so not quite sure whether what it's wrapped my .js code in is correct (can post snippets if it helps).

So my question is twofold really, I'm looking for the answer to either:

  1. Is there a recommended way to write TypeScript post 0.9 to allow it to be run after it's been browserified?
  2. Is there a way to simple tell TypeScript to pull in the 'require' dependency on its own?

Any thoughts or info in this area would be greatly appreciated.

EDIT:

Just to clarify, I'm generating my .js from the .ts during save/build, and in a post build action, pointing browserify to the output. An abridged js output looks like this:

var TestModule;
(function (TestModule) {
    function launchDrone() {
        var exports = require('ar-drone');
        var client = exports.createClient();
    }
})(TestModule || (TestModule = {}));

When I generate the browserified file from that, I can't access TestModule or launchDrone in any context (or certainly not from window. ) is there some trick to accessing the browserified code?

It looks like you potentially are not exporting TestModule? Your TestModule file should look like this:

module TestModule {
    export function launchDrone() {
        var exports = require('ar-drone');
        var client = exports.createClient();
    }
}
export = TestModule;

This way you should be able to launch TestModule and TestModule.launchDrone from the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM