1

I currently have an internal module defined in foo.ts (edit: that can't be modified)

module Foo {
    export function foo(){
        console.log('foo');
    }
}

that I want to transform in an external module (to use in Node). So I created a bar.ts where I reference foo.ts and then I try to export it.

/// <reference path='foo.ts' />

export = Foo;

When I compile it using tsc -m commonjs --out bundle.js bar.ts I expect it to concatenate the declaration from foo.ts and then the export from bar.ts. However I get foo.ts and bar.ts compiled but separated and then bundle.js only has the same code as foo.js.

var Foo;
(function (Foo) {
    function foo() {
        console.log('foo');
    }
    Foo.foo = foo;
})(Foo || (Foo = {}));

Is this even possible to achieve?

2 Answers 2

1

Replace:

module Foo {
    export function foo(){
        console.log('foo');
    }
}

With:

export function foo(){
    console.log('foo');
}

Root file level export or import statements identify a file as an external module.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that I would prefer not to modify the original declaration. I can do it now cause it's my project but what if it's a foreign project I've just cloned?
0

It seems this can't be done using the export keyword of Typescript. The solution is (using the same foo.ts) use this bar.ts

/// <reference path="foo.ts" />

exports = Foo; // You will need node.d.ts

which gets compiled to a bundle of foo.ts and bar.ts. Bad news is that you lose the ability to (easily) create AMD and CommonJS modules with the same source (i.e. using export).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.