大量使用通配符的CSS动态构建

问题描述 投票:0回答:1

我目前正在使用Grunt和Less来最小化文件,我只想在我的代码中使用它-许多第三方文件。可以使用某些过滤器功能的通配符吗?我的Drupal模块的前缀为'snype'。

   less: {
        development: {
            files: [
                {
                    expand: true,     
                    cwd: '<%= path.source %>snype*',      
                    src: ['**/*.less'], 
                    dest: '<%= path.destination %>',   
                    ext: '.css',   
                    extDot: 'first'   
                },
            ],  
        },  
    },

但是这不起作用...如果我从cwd中删除snype *,则它可以工作。

dynamic gruntjs less
1个回答
1
投票

在示例中,您已将根文件夹设置为 snype,这可能是不正确的。

相反,您应该在src中指定模式,例如:

src: ['**/snype*.less']

但是您也可以通过在其前面加上来排除第三方文件夹。

// All .js files in my_folder; except those in drupal folder, etc
{src: ['my_folder/*.js', '!drupal/*.js'....]}

看看Globbing模式

部分here

如果您有一个复杂的文件夹结构,但有许多排除项,则可以考虑使用类似globlob的函数

var createFolderGlobs = function(fileTypePatterns) {
    fileTypePatterns = Array.isArray(fileTypePatterns) ? fileTypePatterns : [fileTypePatterns];
    var ignore = ['node_modules', 'bower_components', 'dist', 'temp']; //you put here folders to ignore

    var fs = require('fs');
    var ret = fs.readdirSync(process.cwd())
    .map(function(file) {
        if (ignore.indexOf(file) !== -1 ||
            file.indexOf('.') === 0 ||
            !fs.lstatSync(file).isDirectory()) {
            //console.log("RB JSHINT DEBUG: ignoring file:" + file);
            return null;
        } else {
            return fileTypePatterns.map(function(pattern) {
                return file + '/**/' + pattern;
            });
        }
    })
    .filter(function(patterns) {
        return patterns;
    })
    .concat(fileTypePatterns);
return ret;

};

您可以直接放入files

选项:
    less: {                
            files: [createFolderGlobs(['*.less'])],
            tasks: ['less:dev'], //whatever else you may need 
        },

考虑到排除模式会大大降低Grunt的速度。

我通过使用cgross

中的cg-angular生成器来实现此功能,您可以找到它here。阅读顶部的评论以获取更详细的说明。
© www.soinside.com 2019 - 2024. All rights reserved.