Cant find variable after javascript minification

199 Views Asked by At

I have an AngularJS web app. The app works great without any minification. But when I minified with grunt, like this:

uglify: {
        options: {
                report: 'min',
                mangle: false
        },
        dist: {
            files: {'app/dist/js/yanpy.min.js': [Many scripts, 'app/js/controllers/shopping-cart.js', more scripts]

I get next error:

ReferenceError: shoppingCart is not defined
    at Object.$get (yanpy.min.js:9)
    at Object.invoke (yanpy-libs-1.min.js:1)
    at yanpy-libs-1.min.js:1
    at getService (yanpy-libs-1.min.js:1)
    at invoke (yanpy-libs-1.min.js:1)
    at Object.instantiate (yanpy-libs-1.min.js:1)
    at yanpy-libs-1.min.js:2
    at yanpy-libs-1.min.js:2
    at forEach (yanpy-libs-1.min.js:1)
    at nodeLinkFn (yanpy-libs-1.min.js:2)

The shoppingCart script "shopping-cart.js" in included in the grunt file for minification. The file looks like:

function shoppingCart(cartName) {
    this.cartName = cartName;
    this.clearCart = false;
    this.checkoutParameters = {};
    this.items = [];

    // load items from local storage when initializing
    this.loadItems();           
}

// load items from local storage
shoppingCart.prototype.loadItems = function () {

   // Do whatever
}

What is happening? And Why is not working when I minified and it´s working without minification?

UPDATE_1: According to the mark as duplicated, please note shopping-cart is not an angular module, but just a javascript script. So, not sure if the approach provided in the original referenced post is the real answer.

UPDATE_2: According to @UncleDave comment. I copy the two parts of code there shoppingCart is referenced.

.directive('adminOfferExtras', function(Extra, ExtraService, LocationService, OfferService, DataService) {        

    function link(scope, element, attrs) { 

        var boatId = parseInt(scope.boat.id);
        var extrasCart = new shoppingCart("OfferBoatExtrasCart_" + boatId);

and

.factory("DataService", function () {

    // create shopping cart
    var myCart = new shoppingCart("Store");    
    return {
        cart: myCart
    };
})

Please note, the only other place where I see shoppingCart referenced is in the shopping-cart.js file, that I already copied. Maybe is this call that should be marked with a var?

shoppingCart.prototype.loadItems = function () {

   // Do whatever
}
0

There are 0 best solutions below