我正在尝试使用grunt和terser来简化一个angularjs应用程序。我一开始使用的是uglifiy-es,但后来发现它有一些问题。所以我试着使用了terser。但是输出并没有给出缩小的文件。
The gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
//grunt task configuration will go here
ngAnnotate: {
options: {
singleQuotes: true
},
app: {
files: {
'./public/min-safe/js/_config_min.js': ['./controllers/_config.js'],
'./public/min-safe/js/ctrl_accountingdashboard.js': ['./controllers/ctrl_accountingdashboard.js'],
}
}
},
concat: {
js: { //target
src: ['./public/min/app.js', './public/min-safe/js/*.js'],
dest: './public/min/app.js'
}
},
terser: {
options: {},
files: {
'./public/min/app.js': ['./public/min/app.js'],
},
}
});
//load grunt tasks
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-terser');
grunt.loadNpmTasks('grunt-ng-annotate');
//register grunt default task
grunt.registerTask('default', ['ngAnnotate', 'concat', 'terser']);
}发布于 2019-06-13 16:52:36
我也有同样的问题。根据文档,这应该可以工作,但它不适用于我。在自定义目标中包装"files“设置对我来说很有效:
terser: {
options: {},
main: {
files: {
'./public/min/app.js': ['./public/min/app.js'],
}
}
}发布于 2020-11-11 18:39:07
补充@Tim的精彩答案:
下面是一个示例,它允许使用路径/文件通配符模式(全局模式)运行grunt它不支持开箱即用。
请注意terser配置中的帮助器属性_src和_dest,它们不是由grunt本身读取的,而是由任务terser_all读取的。此任务在_src中展开全局模式,并在files属性中构建真正的配置。完成后,它将使用更新后的配置运行terser。
module.exports = function (grunt) {
grunt.initConfig({
terser: {
dist: {
options: {
compress: {
drop_console: true // remove console.log, console.info, ...
}
},
files: {
// FILLED through terser_all task below!
// Examples config:
// "dist/example.js": [ "path/to/files/example.js" ]
// "dist/example_joined.js": [ "path/to/files/*.js" ]
},
// -----
// HELPER PROPERTIES to build the files prop (see above) in the terser_all task below.
_src: [
"path/to/files/*.js"
],
_dest: "dist/"
}
}
});
grunt.registerTask('terser_all', function () {
// work on this target in terser config
var terser_target_name = "dist";
// read the terser config
var terser_config = grunt.config.get('terser') || {};
var terser_target_config = terser_config[terser_target_name] || {};
// get the destination dir
var destDir = terser_target_config._dest;
// loop through all source files and create an entry in the terser config for each of it
var files = grunt.file.expand(terser_target_config._src);
for (const [i, file] of files.entries()) {
grunt.log.writeln(file);
// add this file to the terser config as: dest_file: src_file
terser_target_config.files[destDir + file] = file;
}
// show new config on CLI
grunt.log.writeflags(terser_target_config);
// write back config and run task
grunt.config.set('terser', terser_config);
grunt.task.run('terser');
});
grunt.loadNpmTasks('grunt-terser');
grunt.registerTask('build', ['terser_all']);
grunt.registerTask('default', ['build']);
};发布于 2021-01-07 16:13:49
只需注意:如果您试图通过重命名来“禁用”某些选项,则会禁用整个过程。至少这是我在grunt-terser上的结果。留给我的是原始的js文件。
{
mangleX: {
reserved: [/* ... */]
}
}https://stackoverflow.com/questions/56520941
复制相似问题