简体   繁体   English

如何使用grunt从不同的js文件中创建几个缩小文件

[英]How to make couple of minified files from different js files using grunt

I am new to grunt and task runners in JS, so this might seem a simple question but I have been unable to find exact working answer. 我对JS的咕gr声和任务赛跑者是陌生的,因此这似乎是一个简单的问题,但我无法找到确切的工作答案。

I have : 我有 :

concat: {
            options: {
                // define a string to put between each file in the concatenated output
                separator: '\n\n'
            },
            dist: {
                // the files to concatenate
                src: ['scripts/app.js', 'scripts/constant.js'
                ],
                // the location of the resulting JS file
                dest: 'scripts/custom.js'
            }
        },

This task collects all my custom file together. 此任务将所有我的自定义文件收集在一起。 What I want is to do similar thing for all my vendors file. 我想要对所有供应商文件执行类似的操作。 Finally I should end up with two js only custom.js having my concatenated-minified code and vendor.js having concatenated-minfied libraries. 最后,我应该以两个js结束,只有custom.js具有串联的vendor.js代码, vendor.js具有串联的最小化库。

How do I write grunt configuration for this. 我该如何为此编写grunt配置。 Do I need to make two different tasks. 我需要做两个不同的任务吗? If I write the above code twice with different input files, it seems to run the last code. 如果我使用不同的输入文件两次编写以上代码,则似乎在运行最后一个代码。

grunt-contrib-concat can be configured to utilize multiple-targets . grunt-contrib-concat可以配置为利用多个目标

For further documentation on this subject refer to multi-tasks and Task Configuration and Targets in the grunt documentation. 有关此主题的更多文档,请参阅grunt文档中的多任务以及任务配置和目标

Gruntfile.js Gruntfile.js

For your scenario you need to configure your concat task similar to this ( Note: the new custom and vendor targets): 对于您的方案,您需要配置与此类似的concat任务( 注意:新的customvendor目标):

module.exports = function(grunt) {

    grunt.initConfig({

        concat: {
            options: {
                separator: '\n\n'
            },
            custom: {
                src: ['scripts/app.js', 'scripts/constant.js'],
                dest: 'scripts/output/custom.js'
            },
            vendor: {
                // Modify the src and dest paths as required...
                src: ['scripts/vendor/foo.js', 'scripts/vendor/baz.js'],
                dest: 'scripts/output/vendor.js'
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-concat');

    grunt.registerTask('concatenate', [
        'concat:custom', // <-- Targets in a task are called using a colon separator. 
        'concat:vendor'
    ]);

};

Running concat 运行concat

Using the example gist provided above you can run the concat task via the CLI by typing the following command: 使用上面提供的示例要点,您可以通过以下命令通过CLI运行concat任务:

$ grunt concatenate

Configuring Options 配置选项

If you require different configuration options for both the custom and vendor targets you will need to move the options object inside their respective targets. 如果custom目标和vendor目标都需要不同的配置选项,则需要将options对象移动到它们各自的目标中。 As explained here . 这里所解释。

Note: Using the example gist provided the options specified will apply to both targets. 注意:使用示例要点提供的示例,指定的options将适用于两个目标。

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

相关问题 使用Grunt从两个不同来源Concat和Uglify JS文件 - Concat and Uglify JS Files from Two Different Sources using Grunt 使用grunt通过配置合并来自不同目录的js文件 - Combine js files from different directories via config using grunt 如何使用grunt更改HTML文件以引用缩小的JS和CSS而不是未压缩的文件? - How to use grunt to change my HTML files to reference my minified JS and CSS instead of the uncompressed ones? 如何将非最小化和最小化的js文件与browserify捆绑在一起 - How to bundle a non-minified and minified js files with browserify 如何有效调试缩小后的 JS 文件? - How to effectively debug minified JS files? 一个应用程序中不同html中的不同合并和缩小的js文件 - Different merged and minified js files in different htmls in one app Grunt-如何使用Grunt缓存半身像更新js文件中的html文件引用? - Grunt - How to update html files references within js files using grunt cache bust? 如何在sails.js中使用gruntfile.js缩小所有js和css文件 - How to minified all js and css files using gruntfile.js in sails.js AngularJS缩小的js文件不起作用 - AngularJS minified js files not working 如何配置grunt复制任务以从子目录中排除.js文件 - How to configure grunt copy task to exclude .js files from a subdirectory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM