【问题标题】:Grunt j with regular expressionsGrunt j 使用正则表达式
【发布时间】:2016-09-28 08:55:49
【问题描述】:
grunt.config('sass', {
    options: {
      sourceMap: true
    },
    dist: {
      options: {
        outputStyle: 'compact'
      },
      files: {
        '/css/site.css': 'main/scss/site.scss',
        '/css/template.home.css': 'main/scss/templates/home.scss'
        '/css/template.contact.css': 'main/scss/templates/contact.scss'
        '/css/template.something.css': 'main/scss/templates/something.scss'
      }
    }
});

有什么方法可以很好地处理这部分(正在考虑一种模式,但我不确定可能性)

'/css/template.home.css': 'main/scss/templates/home.scss'
'/css/template.contact.css': 'main/scss/templates/contact.scss'
'/css/template.something.css': 'main/scss/templates/something.scss'

【问题讨论】:

  • “处理这部分”?
  • 是的@ClasG 我想将这三行减少为一行,因为我还需要添加其他几行。谢谢

标签: regex node.js sass gruntjs


【解决方案1】:

如果您将配置声明为 JS 对象,您可以这样做:

var cfg = {
        options: {
            sourceMap: true
        },
        dist: {
            options: {
                outputStyle: 'compact'
            },
            files: {
                '/css/site.css': 'main/scss/site.scss',
                '/css/template.home.css': 'main/scss/templates/home.scss',
                '/css/template.contact.css': 'main/scss/templates/contact.scss',
                '/css/template.something.css': 'main/scss/templates/something.scss'
            }
        }
    },
    grunt = {
      config: function() {
        document.write('<br/><strong>grunt config set</string><br/>');
      }
    };

    document.write('<br/>"files" before new config:<br/><br/>');

    for (var property in cfg.dist.files) {
        if (cfg.dist.files.hasOwnProperty(property)) {
            document.write(property + ' = ' + cfg.dist.files[property] + '<br/>');
        }
    }

    document.write('<br/>"files" after added config:<br/><br/>');

    cfg.dist.files['/css/what ever...'] = 'the/latest/path';

    for (var property in cfg.dist.files) {
        if (cfg.dist.files.hasOwnProperty(property)) {
            document.write(property + ' = ' + cfg.dist.files[property] + '<br/>');
        }
    }

    grunt.config('sass', cfg);

注意

    cfg.dist.files['/css/what ever...'] = 'the/latest/path';

这就是添加额外配置的地方。

【讨论】:

  • 非常感谢!我不想在任何地方定义文件名。我需要包含遵循该模式的所有文件以包含我的配置。像这样的东西`'/css/template.%1.css': 'main/scss/templates/$1.scss'`
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 2014-03-29
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多