Использование шаблонов глобуса не для ключей Grunt по умолчанию - PullRequest
0 голосов
/ 27 января 2019

1.Резюме

Я не могу установить плагин grunt-clean-console , чтобы он работал для всех моих файлов .html.


2.Подробнее

grunt-clean-console проверить ошибки консоли браузера для файлов .html.

Я хочу проверить ошибки консоли браузера для всех файлов .html моего сайта. В официальном описании Я прочитал, как работает плагин для определенных значений ключа url.У меня много страниц на моем сайте;Я не хочу добавлять каждый .html файл отдельно.Но я не могу найти, как я могу использовать шаблоны.

Я считаю, что я могу использовать шаблоны для встроенных клавиш Grunt cwd, src, dest.Но как я могу использовать шаблоны 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.Globbing

  • Официальная документация

  • 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.Строительство объекта динамически

  • Официальная документация

  • 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.Шаблоны

  • Официальная документация

  • 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.
    

1 Ответ

0 голосов
/ 06 февраля 2019

Создайте функцию перед деталью grunt.initConfig, которая использует grunt.file.expand.Например:

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 возвращаетмассив путей к файлам для всех файлов .html, соответствующих заданному шаблону глобуса, т.е. 'output/**/*.html'.
  • Значение свойства options.url установлено на getFiles() для вызова функции.
...