javascript – 使用grunt-obfuscator混淆js文件:未捕获的ReferenceError:require未定义

我在互联网上搜索如何模仿我的
javascript代码和使用uglify后转换我的所有文件缩小但没有混淆我决定使用
grunt-obfuscator

在我的简单项目上进行此配置后:

Gruntfile.js

  module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-obfuscator');

  grunt.initConfig({
    connect: {
      server: {
        options: {
          port: 9000,
          base: 'app/'
        }
      }
    },
    watch: {
      project: {
        files: ['app/**/*.js', 'app/**/*.html', 'app/**/*.json', 'app/**/*.css'],
        options: {
          livereload: true
        }
      }
    },
    obfuscator: {
      files: [
        'app/js/app.js',
        'app/js/controllers.js'
      ],
      entry: 'app/js/app.js',
      out: 'app/js/obfuscated.js',
      strings: true,
      root: __dirname
    }
  });

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch');


  grunt.registerTask('default', ['obfuscator', 'connect', 'watch']);

};

我的app.js.

(function () {

    var app = angular.module('myapp', [
            'ngRoute',
            'myapp.controllers'
        ]);

    app.config(['$routeProvider', function ($routeProvider) {

        $routeProvider
            .when('/', {
                templateUrl: 'views/home.html'
            })
            .otherwise({
                redirectTo: '/'
            });
    }]);

})();

我的controller.js

(function() {
    angular.module('myapp.controllers', [])
        .controller('AppController', ['$scope',
         function ($scope) {
            $scope.name = "Test123";
        }]);
})();

执行grunt后,我的obfuscated.js真的很难读,真的很棒.所以我进入我的localhost,现在正在加载obfuscated.js文件,但是我收到了这个错误:

Uncaught ReferenceError: require is not defined

>我错过了什么?

我的混淆.js

!function(a,b){function c(b,d){var e,f;if("\x2e"!=b[0]&&"\x2f"!=b[0])return a(b);if(d=d||"\x72\x6f\x6f\x74",e=c.resolve(b),!e&&/\.json$/i.test(b))return a("\x2e\x2f"+c.basename(b));if(f=c.cache[e],!f)try{return a(b)}catch(g){throw Error("\x66\x61\x69\x6c\x65\x64\x20\x74\x6f\x20\x72\x65\x71\x75\x69\x72\x65\x20\x22"+b+"\x22\x20\x66\x72\x6f\x6d\x20"+d+"\n"+g.message+"\n"+g.stack)}return f.exports||(f.exports={},f.call(f.exports,f,f.exports,c.relative(e))),f.exports}c.cache={},c.basename=a("\x70\x61\x74\x68").basename,c.resolve=function(b){var d,e,f;if("\x2e"!=b[0])return a.resolve(b);for(d=[b,b+"\x2e\x6a\x73",b+"\x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73",b+"\x2e\x6a\x73\x6f\x6e",b+"\x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73\x6f\x6e"],e=0;f=d[e];e++)if(c.cache[f])return f},c.register=function(a,b){c.cache[a]=b},c.relative=function(a){function b(b){var d,e,f,g,h;if("\x2e"!=b[0])return c(b);for(d=a.split("\x2f"),e=b.split("\x2f"),d.pop(),f=0,g=e.length;g>f;f+=1)h=e[f],"\x2e\x2e"==h?d.pop():"\x2e"!=h&&d.push(h);return c(d.join("\x2f"),a)}return b.resolve=c.resolve,b.cache=c.cache,b},c.register("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x61\x70\x70\x2e\x6a\x73",function(a,b,c){!function(){var a=angular.module("\x6d\x79\x61\x70\x70",["\x6e\x67\x52\x6f\x75\x74\x65","\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73"]);a.config(["\x24\x72\x6f\x75\x74\x65\x50\x72\x6f\x76\x69\x64\x65\x72",function(a){a.when("\x2f",{templateUrl:"\x76\x69\x65\x77\x73\x2f\x68\x6f\x6d\x65\x2e\x68\x74\x6d\x6c"}).otherwise({redirectTo:"\x2f"})}]),c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"],c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x6d\x6f\x64\x65\x6c\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"],c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x72\x6f\x75\x74\x65\x73\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"]}()}),c.register("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73\x2e\x6a\x73",function(){!function(){angular.module("\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73",[]).controller("\x41\x70\x70\x43\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72",["\x24\x73\x63\x6f\x70\x65",function(a){a.name="\x54\x65\x73\x74\x31\x32\x33"}])}()}),b.exports=c("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x61\x70\x70\x2e\x6a\x73")}(require,module);

UPDATE
这个问题没有答案,因为这个插件不是为angularjs创建的,只针对Node.js.并且创造者没有为角度js制作它的最小意图. :(但它是一个很酷的工具

最佳答案 这里有几个问题.

我是否需要对所有js文件进行模糊处理才能运行?
没有

混淆文件可以完美地与所有依赖项进行非混淆吗?是

我错过了什么?
很难说,丑化可能非常棘手.
首先,我将验证依赖注入是否完成,或者您可以使用ngAnnotate为您执行此操作
依赖性应定义为以下示例:

.service('myService',['$rootScope',function($rootScope){ ... }]);

如果这不能解决您的问题,您必须挖掘似乎发送问题的requireJS

编辑你的代码并粘贴你的main.js给我们看看你想要什么uglify

点赞