简体   繁体   中英

AngularJS: TypeScript compile process and gulp-uglify - is there a way to force TS to produce functions instead of variable with IIFE?

I have AngularJS controller :

ArticleController.prototype = Object.create(BaseController.prototype);

/* @ngInject */

function ArticleController (CommunicationService){
    //Some code not related with problem
}

Which is minified with gulp:

return gulp.src(pathsToMinify)
        .pipe(require('gulp-ng-annotate')())
        .pipe(require('gulp-uglify')())
        .pipe(require('gulp-concat')('application.min.js'))
        .pipe(gulp.dest('dist'));

And then I decided to migrate from plain Javascript to Typescript, starting with BaseController :

class BaseController {
    constructor() {
        //Some code not related with problem
    }
}

After minification and concatenation, I got:

Uncaught TypeError: Cannot read property 'prototype' of undefined

Related to line:

ArticleController.prototype = Object.create(BaseController.prototype);

Then I realised that Typescript compiler otputs BaseController as variable with IIFE:

var BaseController = (function () {
    function BaseController() {

    }
    BaseController.prototype.setPath = function (path) {
        this._path = path;
    };
    //Some code not related with problem
    return BaseController;
})();

Problem IMO is related with variable/function hoisting in Javascript - when I manually replace variable and IIFE with function:

function BaseController() {
}
//Some code not related with problem

It works properly. Is there any idea to dispose of this problem, like forcing Typescript compiler to output function instead of variable with IIFE? Or, I can not change it, and I have to deal with it in other way? Thank you in advance for any help, I am pretty new to Typescript and I didn't realize that I can find problems like this.

Likely, BaseController is not visible from ArticleController (because it is concatenated after for instance).

To avoid these sort of issues, write modular Typescript (es6 and/or commonjs style) and use webpack or browserify

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