简体   繁体   English

在Sails.js中使用load-grunt-config

[英]Using load-grunt-config with Sails.js

Context 上下文

I have a few grunt tasks that I've already written, and I'd like to use them with a new project I'm writing in Sails.js. 我已经编写了一些繁琐的任务,我想将它们用于我在Sails.js中编写的新项目中。

With Sails.js, you can add additional grunt tasks by adding a JS file to the /tasks/register folder. 使用Sails.js,您可以通过将JS文件添加到/tasks/register文件夹来添加其他grunt任务。 Before we get to the file I've added, let's talk about the problem. 在进入我添加的文件之前,让我们谈谈问题。

The Problem 问题

Sails won't lift. 帆不会升起。 Debugger shows: 调试器显示:

debug: --------------------------------------------------------
error: ** Grunt :: An error occurred. **
error: 
------------------------------------------------------------------------
ERROR
>> Unable to process task.
Warning: Required config property "clean.dev" missing. 

The issue in question is obviously with grunt, so then I try grunt build (which automatically runs with sails lift ): 有问题的问题显然是咕gr声,所以我尝试grunt build (它会随sails lift自动运行):

Running "clean:dev" (clean) task
Verifying property clean.dev exists in config...ERROR
>> Unable to process task.
Warning: Required config property "clean.dev" missing. Use --force to continue.

From this, I've garnered that this is a path issue. 由此,我认为这是一个路径问题。 Let's take a look at the file I've added. 让我们看一下我添加的文件。

/tasks/register/customTask.js /tasks/register/customTask.js

The task here loads load-grunt-config , which is the source of my problems: 这里的任务加载load-grunt-config ,这是我的问题的根源:

module.exports = function(grunt) {

    // measures the time each task takes
    require('time-grunt')(grunt);

    // This require statement below causes my issue
    require('load-grunt-config')(grunt, {
        config: '../../package.json',
        scope: 'devDependencies',
        overridePath: require('path').join(process.cwd(), '/asset-library/grunt')
    });

    grunt.registerTask('customTask', [
        'newer:jshint',
        'newer:qunit',
        'newer:concat',
        'newer:cssmin',
        'newer:uglify'
    ]);

};

I had assumed that using overridePath instead of configPath would solve my issue, but alas, it's not quite that simple. 我以为使用overridePath而不是configPath可以解决我的问题,但是可惜,它并不是那么简单。 Is there some way to make it so that I can use my own custom tasks folder with load-grunt-config like I've done in other projects, or is there some magic conditional I can wrap the require statement around? 是否有某种方法可以使我像在其他项目中一样使用自己的自定义任务文件夹和load-grunt-config进行操作,还是有一些魔术条件可以包装require语句?

I only need it to run with grunt customTask , and not run with grunt * (anything else). 我只需要它与grunt customTask一起运行, 而不需要grunt * (其他任何东西)一起运行。

Okay, this was actually pretty easy. 好的,这实际上很容易。 All I had to do was change the grunt.registerTask call in my customTask.js file from this: 我要做的就是从此更改我的customTask.js文件中的grunt.registerTask调用:

grunt.registerTask('customTask', [
    'newer:jshint',
    'newer:qunit',
    'newer:concat',
    'newer:cssmin',
    'newer:uglify'
]);

to this: 对此:

grunt.registerTask('customTask', 'My custom tasks', function() {
    // The require statement is only run with "grunt customTask" now!
    require('load-grunt-config')(grunt, {
        config: '../../package.json',
        scope: 'devDependencies',
        overridePath: require('path').join(process.cwd(), '/asset-library/grunt')
    });
    grunt.task.run([
        'newer:jshint',
        'newer:qunit',
        'newer:concat',
        'newer:cssmin',
        'newer:uglify'
    ]);
});

In case it's not clear, I did have to move the require('load-grunt-config') call, so if you're copy + pasting, make sure to remove the require statement that's outside the grunt.registerTask call. 如果不清楚,我确实必须移动require('load-grunt-config')调用,因此,如果您要复制+粘贴,请确保删除grunt.registerTask调用之外的require语句。

You can find more information about custom Grunt tasks here . 您可以在此处找到有关自定义Grunt任务的更多信息

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

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