0
votes

I am trying to create a javascript module by TypeScript which is compatible with ES5 browsers and NodeJs modules. So I don't want to use the import and export in TypeScrtipt because it makes the output depends on SystemJS or RequireJS or commonJs. I have just created a library with namespace and add the following code to make it compatible with SystemJs:

// global html object (pure javascript compatiblity)
if (typeof window !="undefined") (<any>window).myNamespace = myNamespace;

// create exports
declare var module:any;
if (typeof module !="undefined") module.exports = {myNamespace};

Everything is OK, I just need to add the following code so I can use the library in NodeJS modules and have propery typing information

export {myNamespace}

So I can use my library in NodeJs lib like this:

import {myNamespace} from "../lib/myNamespace.js";

But I couldn't find how to tell the TypeScript compiler to add that export without creating a module in its own way. How can I add a few lines directly at the end of generated d.ts automatically after each compile such as:

Generated typing ..
...
...

// Custom declaration
// Remark Remark
export {myNamespace}
// Remark Remark

**Note: ** The modulation is none and many things are custom, I cannot use the regular export feature of typescript modulation.

1

1 Answers

0
votes

How can I add a few lines directly at the end of generated d.ts

There is no TypeScript option to magically have some code only appear in the generate .d.ts if it doesn't exist in the origin .ts.

Options

Add a build pipeline e.g. gulp / grunt and use their completion event to tag on more stuff after the .d.ts is generated.

Personally I would recommend reconsider why you need to do this.