简体   繁体   English

如何使用我自己的选项配置concat和uglify与grunt-usemin

[英]How to use my own option configurations for concat and uglify with grunt-usemin

For example: Im using the current configuration below to uglify my JS scripts in my Gruntfile: 例如:即时通讯使用下面的当前配置uglify我的JS脚本在我Gruntfile:

    uglify: {
        options: {
            report: "min", //"gzip",
            sourceMap: true,
            preserveComments: false, //"some", "all"
        },
        application: {
            options: {
                // expand: true,
                banner: '<%= app.banner %>',
                preserveComments: "some"
            },
            src: 'dist/js/application.js',
            dest: ".tmp/js/application.min.js"
        },
        dependencies: {
            options: {
                sourceMap: false
            },
            src: ['dist/js/dependencies.js'],
            dest: ".tmp/js/dependencies.min.js"
        },

Im aware that grunt-usemin generates the src and dest options from the code block in the html file declared in useminPrepare gruntfile option, for example: 我知道grunt-usemin从useminPrepare gruntfile选项中声明的html文件中的代码块生成srcdest选项,例如:

    <!-- build:js js/app.js -->
    <script src="js/app.js"></script>
    <script src="js/controllers/thing-controller.js"></script>
    <script src="js/models/thing-model.js"></script>
    <script src="js/views/thing-view.js"></script>
    <!-- endbuild -->

So how can I configure grunt-usemin to use these same options, such as banner , sourceMap: false with the generated file blocks, I've read through the quick documentation usually given in github or NPM registry but seem not to find a solid answer to this. 那么如何配置grunt-usemin来使用这些相同的选项,例如bannersourceMap: false以及生成的文件块,我已经阅读了通常在github或NPM注册表中给出的快速文档,但似乎找不到可靠的答案对此。

One sentence is very important in the documentation : 一句话在文档中非常重要:

In addition, useminPrepare dynamically generates the configuration for concat, uglify, and cssmin. 另外,useminPrepare动态生成concat,uglify和cssmin的配置。 Important: you still need to manually manage these dependencies and call each task. 重要提示:您仍需要手动管理这些依赖项并调用每个任务。

The principle is to declare only that you want to use usemin (in the grunt register task) with all the main task you want : concat, uglify... Usemin will by default create all these tasks without anymore declaration, based on your registertask options and html markup comments. 原则是只声明你想要使用usemin(在grunt注册任务中)你想要的所有主要任务:concat,uglify ...默认情况下,Usemin将根据你的registertask选项创建所有这些任务而不再声明和HTML标记评论。

Code is better than words : 代码比单词更好:

  1. Express your block markup target files . 表达您的块标记目标文件 In your case something like : 在你的情况下像:
  <!-- build:js js/app.min.js --> <script src="js/app.js"></script> <script src="js/controllers/thing-controller.js"></script> <script src="js/models/thing-model.js"></script> <script src="js/views/thing-view.js"></script> <!-- endbuild --> 

2 - Register the tasks that you want usemin generates for your during the runtime (it does not generate anything in your file - something that should be precised in the documentation). 2 - 在运行期间注册您希望usemin为您生成的任务(它不会在您的文件中生成任何内容 - 应该在文档中进行精确处理)。 For example : 例如 :

grunt.registerTask('minify', [ 'useminPrepare' ,'concat' ,'cssmin' ,'uglify' ,'copy' grunt.registerTask('minify',['useminPrepare','concat','cssmin','uglify','copy'
,'rev ,'usemin' ]) ,'rev,'usemin'])

3 - By default, all these tasks are generated except useminPrepare and usemin (look at the documentation for these 2 blocks grunt-usemin ). 3 - 默认情况下,除了useminPrepare和usemin之外,所有这些任务都会生成(请查看这两个块grunt-usemin的文档 )。

Then if you want to add specific options like sourcemap, just rewrite the configuration code without redefining everything : 然后,如果您想添加像sourcemap这样的特定选项,只需重写配置代码而无需重新定义所有内容:

uglify: { options: { sourceMap: false } } uglify:{options:{sourceMap:false}}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM