我构建了一些 gulp 任务来帮助构建我的 Web 项目。在其中之一中,我正在缩小 js 文件。这是任务:
gulp.task('minify' , function() {
console.log('Copy minified js ');
return gulp.src('dist/www/**/*.js'])
.pipe(uglify().on('error', function(e){
console.log(e);
}))
.pipe(gulp.dest('dist/www'));
});
当我执行任务时:gulp minify
我收到以下错误:
{ [Error: E:XXXXXX\dist\tmp\bootstrap.js: SyntaxError: Unexpected token: name (mainModule)]
message: 'E:XXXXXX\\dist\\tmp\\bootstrap.js: SyntaxError: Unexpected token: name (mainModule)',
fileName: 'E:XXXXXX\\dist\\bootstrap.js',
lineNumber: 1,
stack: 'Error\n at new JS_Parse_Error (eval at <anonymous> (E:XXXXXX\gfgfgfgf\\gulp-uglify\\node_modules\\uglify-js\\tools\\node.js:28:1), <anonymous>:1534:18)\n at js_error (eval at <anonymous> (
这里是 bootstrap.js 文件:
import mainModule from './src/main';
angular.element(document).ready(function() {
angular.bootstrap(document, [mainModule.name], { strictDi: true });
});
发生什么事了?有人可以帮助我解释为什么它不起作用吗?
谢谢你!
最佳答案
在丑化 Angular 组件之前,您需要添加 Angular 依赖注入(inject)注释。您可以通过管道 ng-annotate在通过 uglify 进行管道传输之前。
编辑:此外,uglifying ES6 javascript 也存在已知问题。 。如果您使用 ES6 语法,您应该 pipe your code through Babel在丑化它之前。
希望这有帮助!
关于javascript - 为什么 uglify 不能处理 AngularJS 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334269/