【问题标题】:adding a custom grunt task to sails.js向sails.js 添加自定义grunt 任务
【发布时间】:2017-11-10 19:38:15
【问题描述】:

我是sails.js 的新手,但过去几天我一直在阅读大量文档,所以我觉得我对该平台有了基本的了解。但我似乎无法添加和注册自定义的 grunt 任务。我已经尝试了几种方法,但它们似乎都不起作用。所以我想我会保持简单,只需使用现有的注册文件之一注册一个任务,但我仍然不打算让这个方法工作。

所以我将以下 Gruntfile 添加到 tasks/config/comp.js

module.exports = function(grunt) {
    grunt.config.set('comp', {
        dev: {
            options: {
                module: 'system',
                moduleResolution: 'node',
                target: 'es3',
                experimentalDecorators: true,
                emitDecoratorMetadata: true,
                noImplicitAny: false
            },
            files: [{
                expand: true,
                cwd: 'assets/js/',
                src: [ '**/*.ts' ],
                dest: '.tmp/public/js',
                ext: '.js'
            }]
        }
    });

    grunt.loadNpmTasks('grunt-ts');
};

我在 tasks/register/compileAssets.js 中添加了以下行

/**
 * `compileAssets`
 *
 * ---------------------------------------------------------------
 *
 * This Grunt tasklist is not designed to be used directly-- rather
 * it is a helper called by the `default`, `prod`, `build`, and
 * `buildProd` tasklists.
 *
 * For more information see:
 *   http://sailsjs.org/documentation/anatomy/my-app/tasks/register/compile-assets-js
 *
 */
module.exports = function(grunt) {
  grunt.registerTask('compileAssets', [
    'clean:dev',
    'jst:dev',
    'less:dev',
    'copy:dev',
    'coffee:dev',
    'comp:dev'
  ]);
};

但是,每当我运行 sails lift 时,我都会收到以下错误

信息:正在启动应用程序...

信息: 信息:.-..-. 信息: 信息:帆 --'-------' info: __---___--___---___--___---___--___ info: ____---___--___---___--___---___--___-__ info: info: Server lifted inC:\Users\josh\Documents\PGW` 信息:要查看您的应用,请访问http://localhost:1337 信息:要关闭 Sails,请随时按 + C。

调试:--------------------------------- ---------- 调试: :: Thu Jun 08 2017 16:32:01 GMT-0600(山地夏令时间)

调试:环境:开发 调试:端口:1337 调试:------------------------------------------------ -------- 错误:** Grunt :: 发生错误。 **

错误:

由于警告而中止。

警告:找不到任务“comp:dev”。

错误:看起来发生了 Grunt 错误—— 错误:请修复它,然后重新启动 Sails 以继续运行任务(例如,监视资产的变化) 错误:或者如果您遇到问题,请查看下面的故障排除提示。

错误:故障排除提示: 错误: 错误:*-> 是否在本地安装了“grunt”和相关的 grunt 任务模块?如果您不确定,请运行npm install。 错误: 错误:*-> 您可能有格式错误的 LESS、SASS、CoffeeScript 文件等。 错误: 错误:*-> 或者您没有访问.tmp 目录的权限? 错误:例如,C:\Users\josh\Documents\PGW\.tmp? 错误: 错误:如果您认为可能是这种情况,请尝试运行: 错误:sudo chown -R YOUR_COMPUTER_USER_NAME C:\Users\josh\Documents\PGW.tmp

我已经为此苦苦思索了好几个小时,我不明白为什么sails lift 不会运行我的任务。我觉得在阅读sails 文档和其他stackoverflow 文章之间我已经很好地遵循了指示。有人可以帮助我了解我在这里缺少什么吗?

谢谢

【问题讨论】:

    标签: typescript gruntjs sails.js grunt-ts


    【解决方案1】:

    您正在尝试运行您定义的 comp 目标,但 grunt 没有找到关联的 comp taskname --> grunt plugin 映射来执行它。

    您似乎正在尝试使用grunt-ts

    grunt.loadNpmTasks('grunt-ts');
    

    其中添加了ts 任务。将目标定义中的“comp”更改为ts

    grunt.config.set('ts', {
    

    并更新 compileAssets.js 文件

     grunt.registerTask('compileAssets', [
            'clean:dev',
            'jst:dev',
            'less:dev',
            'copy:dev',
            'coffee:dev',
            'ts:dev' //execute grunt-ts
          ]);
    

    这应该足以解决。如果您仍有问题,请确保您已安装 grunt-ts

    npm install grunt-ts
    

    如果您真的想将任务名称静态映射到插件,您可以使用 jit-grunt 之类的工具为您执行此操作:

    require('jit-grunt')(grunt, {
          comp: 'grunt-ts', //comp now maps to grunt-ts plugins
    });
    

    【讨论】:

    • 这个答案是正确的,我在发布我的原始问题后不久就发现了。但是现在,我想弄清楚我是如何知道添加一个专门称为 ts 的任务的。我也是 grunt 的新手,所以我想我只是想弄清楚我在 grunt-ts npm 页面上看到的 grunt init 语法和我为sails 输入的语法之间的区别。再次感谢您的帮助。
    猜你喜欢
    • 2013-03-04
    • 2015-05-10
    • 2013-06-09
    • 2015-10-08
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多