Grunt是如何缩小或丑化IIFE的

How does Grunt minify or uglify IIFE?

本文关键字:IIFE 缩小 何缩小 Grunt      更新时间:2024-06-01

在标准grunt serve中,我在我的一个指令周围使用IIFE,如下所示:

    (function() {
  "use strict";
  angular.module('napiRest')
    .directive('componentStaffReport', ['$modal', 'ComponentStaffFactory', function ($modal, ComponentStaffFactory) {
      return {
        restrict: 'E',
        transclude: true,
        scope: {
          componentId: '=componentId',
          linkableContent: '=linkableContent'
        },
        templateUrl: 'app/pages/report/component-staff/component-staff-popup.html',
        link: function (scope, element, attr) {
          ComponentStaffFactory.componentId = scope.componentId;
          var modalInstance = null;
          scope.open = function () {
            modalInstance = $modal.open({
              templateUrl: 'ComponentStaffModal.html',
              controller: 'ComponentStaffingCtrl',
              size: 'lg',
              backdrop: true,
              windowClass: 'x-x-large-modal'
            });
            ComponentStaffFactory.modalInstance = modalInstance;
          };
        } //link
      }; //return
    }])
})();

当我使用grunt serve:dist进行部署时,这不起作用。我得到错误:Uncaught TypeError: object is not a function

然而,当我移除周围的IIFE时,它可以正常工作。这是由dist的咕哝声中丑陋或缩小的方式引起的吗?

问题似乎是由另一个文件中缺少分号引起的。另一个文件是一个角度控制器,末尾没有分号。当grunt丑化代码时,它将这个文件和上面的文件混合在一起,创建了自己的匿名函数,这当然是编译器不喜欢的。