简体   繁体   English

为每个模块发出咕unt声,然后最小化

[英]Grunt concat for each module then minify

Trying to do this with Grunt. 尝试用Grunt做到这一点。 I have a file structure like this: 我有一个像这样的文件结构:

/app
--app.js
--/module1
----module1.js
----module1Model.js
--/module2
----module2.js
----module2Model.js

How can I concatenate & minify each module into one file so it looks like: 如何将每个模块串联并缩小到一个文件中,使其看起来像:

/app
--app.min.js
--module1.min.js
--module2.min.js

We want this to be scalable so when we add module3, 4 it'll still work. 我们希望它具有可伸缩性,因此当我们添加module3,4时它仍然可以工作。 Thank you for your help! 谢谢您的帮助!

you can use uglify for solution and "dist" and "src" can be changed according to use: 您可以使用uglify作为解决方案,并且可以根据用途更改“ dist”和“ src”:

uglify:{
        liveproject:{
            files:{
                'dist/js/main.min.js' : ['src/js/*.js'],
                'dist/js/m1.min.js' : ['src/js/easing.js','src/js/modernizer.js'],
            }   

        }

    },

I derived my answer from this post: Using grunt to concat many files from many dirs into single renamed file in new directory 我从这篇文章中得出了我的答案: 使用grunt将来自许多目录的许多文件合并为新目录中的单个重命名文件

grunt.registerTask('prepareModule', 'process dist module for concat', function() {

        //get configured concat task object
        var concat = grunt.config.get('concat') || {};

        //iterate through all file/folders at build/src/app/
        grunt.file.expand(grunt.config('build_dir') + '/src/app/*').forEach(function(dir) {

            //grab the last path name
            var dirName = dir.substr(dir.lastIndexOf('/') + 1);

            //filter for folders only
            if (dirName.slice(-3) !== '.js') {

                //add target for each module
                concat[dirName] = {
                    src: [grunt.config('build_dir') + '/src/app/' + dirName + '/*.js'],
                    dest: grunt.config('dist_dir') + '/src/app/' + dirName + '/' + dirName + '.js'
                };
            }
        });

        grunt.config.set('concat', concat);

    });

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

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