Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove reference tags in compiled output

Tags:

typescript

I'm compiling all .ts files into one file using the command

tsc -out app.js app.ts --removeComments

based on the npm usage instructions. The problem is it still keeps all reference tags in the compiled code afterwards! As far as I'm aware, there's no use for these in JavaScript.

For example, these TypeScript files

Application.ts

/// <reference path="../references/backbone.d.ts" />

module Example {
  export class Application extends Backbone.View<Backbone.Model> {
    ...
  }
}

and

app.ts

/// <reference path="Example/Application.ts" />

import Application = Example.Application;

class App extends Application {
  ...
}

will compile into something like

/// <reference path="../references/backbone.d.ts" />
var __extends = (this && this.__extends) || function (d, b) {
    ...
};
var Example;
(function (Example) {
    var Application = (function (_super) {
       ...
    })(Backbone.View);
    Example.Application = Application;
})(Example || (Example = {}));
/// <reference path="Example/Application.ts" />
var Application = Example.Application;
var App = (function (_super) {
    ...
})(Application);
//# sourceMappingURL=app.js.map

The sourceMappingURL I absolutely want included, and the --removeComments works as expected there. But the reference tags seem pointless when sitting on the server. Is there an option to remove these from the compiled source? Or am I incorrect in thinking these are not used (possibly when debugging with source maps)?

like image 666
Chris Bornhoft Avatar asked Aug 11 '15 16:08

Chris Bornhoft


1 Answers

There are two methods to removing the references, as pointed out in the comments.

  1. Using tsconfig.json removes the need for /// <reference> tags altogether. For example:

    /// <reference path="../references/backbone.d.ts" />
    

    becomes

    {
      "compilerOptions": {
        ...
      },
      "files": [
        'references/backbone.d.ts'
      ]
    }
    

    within tsconfig.json.

  2. If you wish to not use a config file, minifying the compiled JavaScript with a tool like Closure Compiler will remove all comments and references.

like image 61
Chris Bornhoft Avatar answered Nov 10 '22 21:11

Chris Bornhoft