问题
I was searching over the internet about how to ofuscate my javascript code and after use uglify that convert all my files minified but not obfuscated I decided to use grunt-obfuscator
After make this configuration on my simple project:
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']);
};
My app.js
(function () {
var app = angular.module('myapp', [
'ngRoute',
'myapp.controllers'
]);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html'
})
.otherwise({
redirectTo: '/'
});
}]);
})();
My controller.js
(function() {
angular.module('myapp.controllers', [])
.controller('AppController', ['$scope',
function ($scope) {
$scope.name = "Test123";
}]);
})();
After execute grunt my obfuscated.js
is really hard to read, wich is really great. So I'm enter to my localhost wich is loading now the obfuscated.js
file but I'm getting this error:
Uncaught ReferenceError: require is not defined
- What am I missing?
My obfuscated.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 There is no answer for this question since this plugin is not created for angularjs, only for Node.js. And the creator has not the minimal intention to make it for angular js. :( However it is a cool tool
回答1:
there are several questions here.
Do I need to obfuscate all my js files in order to get this run? no
An obfuscate file can work perfect with all your dependencies NON-obfuscated? yes
What am I missing? Hard to tell, uglification can be very tricky. First I'll verify if dependencies injections are well done or you can use ngAnnotate to do that for you dependency shall be defined as example bellow:
.service('myService',['$rootScope',function($rootScope){ ... }]);
If that does not solve your issue, you have to dig on requireJS that seems to send an issue
Edit your code and paste your main.js for us see what you are trying to uglify
来源:https://stackoverflow.com/questions/31150558/obfuscating-js-files-with-grunt-obfuscator-uncaught-referenceerror-require-is