Dwi*_*igh 5 javascript angularjs gruntjs terser
我正在尝试使用 grunt 和 terser 缩小 angularjs 应用程序。我首先使用 uglifiy-es 但后来读到它有一些问题。所以我尝试了更简洁。但是输出没有给我缩小的文件。
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']);
}
Run Code Online (Sandbox Code Playgroud)
要添加@Tim 的精彩答案:
\n这里是一个示例,允许使用路径/文件通配符模式(通配符)\xe2\x80\x93 运行 grunt-terser,但它不支持开箱即用。
请注意辅助属性_src和_destterser 配置中的内容不是由 grunt-terser 本身读取而是由任务读取terser_all。此任务扩展了通配模式_src并在属性中构建了真实的配置files。完成后,它会使用更新的配置运行 terser。
module.exports = function (grunt) {\n grunt.initConfig({\n terser: {\n dist: {\n options: {\n compress: {\n drop_console: true // remove console.log, console.info, ...\n }\n },\n files: {\n // FILLED through terser_all task below!\n\n // Examples config:\n // "dist/example.js": [ "path/to/files/example.js" ]\n // "dist/example_joined.js": [ "path/to/files/*.js" ]\n\n },\n // -----\n // HELPER PROPERTIES to build the files prop (see above) in the terser_all task below.\n _src: [\n "path/to/files/*.js"\n ],\n _dest: "dist/"\n }\n }\n });\n\n grunt.registerTask('terser_all', function () {\n // work on this target in terser config\n var terser_target_name = "dist";\n\n // read the terser config\n var terser_config = grunt.config.get('terser') || {};\n var terser_target_config = terser_config[terser_target_name] || {};\n\n // get the destination dir\n var destDir = terser_target_config._dest;\n\n // loop through all source files and create an entry in the terser config for each of it\n var files = grunt.file.expand(terser_target_config._src);\n for (const [i, file] of files.entries()) {\n grunt.log.writeln(file);\n // add this file to the terser config as: dest_file: src_file\n terser_target_config.files[destDir + file] = file;\n }\n\n // show new config on CLI\n grunt.log.writeflags(terser_target_config);\n\n // write back config and run task\n grunt.config.set('terser', terser_config);\n grunt.task.run('terser');\n });\n\n grunt.loadNpmTasks('grunt-terser');\n\n grunt.registerTask('build', ['terser_all']);\n grunt.registerTask('default', ['build']);\n};\nRun Code Online (Sandbox Code Playgroud)\n
我有同样的问题。根据文档,这应该有效,但对我来说却没有。在自定义目标中包装“文件”设置对我有用:
terser: {
options: {},
main: {
files: {
'./public/min/app.js': ['./public/min/app.js'],
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1735 次 |
| 最近记录: |