【问题标题】:Using glob patterns not for default Grunt keys不使用默认 Grunt 键的 glob 模式
【发布时间】:2019-06-20 16:25:20
【问题描述】:

1。总结

我无法设置grunt-clean-console 插件,它适用于我所有的.html 文件。


2。详情

grunt-clean-console 检查 .html 文件的浏览器控制台错误。

我想检查我网站上所有.html 文件的浏览器控制台错误。 In official descripition 我读到,插件如何为 url 键的特定值工作。我的网站上有很多页面;我不想单独添加每个 .html 文件。但我找不到,我如何使用模式。

我发现,我可以将模式用于内置 Grunt cwdsrcdest 键。但是我如何将 glob(或其他)模式用于自定义键作为这个插件的 url


3。数据

  • Gruntfile.coffee:

    module.exports = (grunt) ->
    
        grunt.loadNpmTasks 'grunt-clean-console'
    
        grunt.initConfig
            'clean-console':
                all:
                    options:
                        url: 'output/index.html'
        return
    
  • 示例项目配置:

    output
    │   404.html
    │   index.html
    │
    ├───KiraFirstFolder
    │       KiraFirstfile.html
    │
    └───KiraSecondFolder
            KiraSecondFile.html
    
  • 如果我为 url 键设置特定值而没有如上例所示的模式,则 grunt-clean-console 成功工作:

    phantomjs: opening page output/index.html
    
    phantomjs: Checking errors after sleeping for 5000ms
    ok output/index.html
    
    phantomjs process exited with code 0
    
    Done.
    

3.1。重现步骤

我在控制台中运行:

grunt clean-console --verbose

4。没有帮助

4.1。通配符

  • Official documentation

  • Gruntfile.coffee:

    module.exports = (grunt) ->
    
        grunt.loadNpmTasks 'grunt-clean-console'
    
        grunt.initConfig
            'clean-console':
                all:
                    options:
                        url: 'output/**/*.html'
        return
    
  • 输出:

    phantomjs: opening page http://output/**/*.html
    
    phantomjs: Unable to load resource (#1URL:http://output/**/*.html)
    
    
    phantomjs:   phantomjs://code/runner.js:30 in onResourceError
    Error code: 3. Description: Host output not found
    
      phantomjs://code/runner.js:31 in onResourceError
    
    phantomjs: loading page http://output/**/*.html status fail
    
      phantomjs://code/runner.js:50
    
    phantomjs process exited with code 1
    url output/**/*.html has 1 error(s)
    >> one of the urls failed clean-console check
    Warning: Task "clean-console:all" failed. Use --force to continue.
    
    Aborted due to warnings.
    

4.2。动态构建对象

  • Official documentation

  • Gruntfile.coffee(示例):

    module.exports = (grunt) ->
    
        grunt.loadNpmTasks 'grunt-clean-console'
    
        grunt.initConfig
            'clean-console':
                all:
                    options:
                        url:
                            files: [
                                expand: true
                                cwd: "output/"
                                src: ['**/*.html']
                                dest: "output/"
                            ]
        return
    
  • 输出:

    File: [no files]
    Options: urls=[], timeout=5, url=["output/**/*.html"]
    
    Fatal error: missing url
    

4.3。模板

  • Official documentation

  • Gruntfile.coffee:

    module.exports = (grunt) ->
    
        grunt.loadNpmTasks 'grunt-clean-console'
    
        grunt.initConfig
            'clean-console':
                all:
                    options:
                        url: '<%= kiratemplate %>'
            kiratemplate: ['output/**/*.html'],
        return
    
  • 输出:

    phantomjs: opening page http://output/**/*.html
    
    phantomjs: Unable to load resource (#1URL:http://output/**/*.html)
    
    
    phantomjs:   phantomjs://code/runner.js:30 in onResourceError
    Error code: 3. Description: Host output not found
    
      phantomjs://code/runner.js:31 in onResourceError
    loading page http://output/**/*.html status fail
    
      phantomjs://code/runner.js:50
    
    phantomjs process exited with code 1
    url output/**/*.html has 1 error(s)
    >> one of the urls failed clean-console check
    Warning: Task "clean-console:all" failed. Use --force to continue.
    
    Aborted due to warnings.
    

【问题讨论】:

    标签: gruntjs glob gruntfile grunt-plugins


    【解决方案1】:

    在使用grunt.file.expandgrunt.initConfig 部分之前创建一个函数。例如:

    Gruntfile.js

    module.exports = function(grunt) {
    
      grunt.loadNpmTasks 'grunt-clean-console'
    
      // Add this function...
      function getFiles() { return grunt.file.expand('output/**/*.html'); }
    
      grunt.initConfig({
        'clean-console': {
          all: {
            options: {
              url: getFiles() // <-- invoke the function here.
            }
          }
        }
        // ...
      });
    
      // ...
    }
    

    注意事项:

    • getFiles 函数返回与给定 glob 模式匹配的所有 .html 文件的文件路径数组,即'output/**/*.html'
    • options.url 属性的值设置为getFiles() 以调用函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多