简体   繁体   English

使用grunt将Jade模板编译为sails.js中的javascript函数

[英]Using grunt to compile jade templates to javascript functions in sails.js

I am building an application in Sails.js and i want to compile some jade templates to javascript functions for use from the client. 我正在Sails.js中构建一个应用程序,我想将一些玉器模板编译为javascript函数以从客户端使用。 I have found a few articles explaining how to do this, but when i try to implement their steps they don't seem to work for me 我发现一些文章解释了如何执行此操作,但是当我尝试执行他们的步骤时,它们似乎对我没有用

I am trying to compile files located in App/Views/Client and put the compiled javascript files into App/.tmp/public/templates 我正在尝试编译位于App / Views / Client中的文件,并将已编译的javascript文件放入App / .tmp / public / templates

My grunt task is in App/tasks/config and is based on the coffeescript grunt task that comes with sails.js. 我的grunt任务位于App / tasks / config中,并且基于sails.js附带的coffeescript grunt任务。 My grunt task looks like this 我的咕unt任务看起来像这样

module.exports = function(grunt) {
    grunt.config.set('jade', {
        dev: {
            templates:{
                options: {
                    pretty: true,
                    client: true,
                    namespace: 'Templates'  
                },
                files: [{
                    expand: true,
                    cwd: 'views/client/',
                    src: ['**/*.jade'],
                    dest: '.tmp/public/templates/',
                    ext: '.js'
                }]
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-jade');
};

I have added it to the compileAssets.js grunt task that comes with sails.js, and i can see it being run when i run the sails lift command. 我已将其添加到sails.js随附的compileAssets.js grunt任务中,并且在运行sails lift命令时可以看到它正在运行。

When it runs it reports 0 files created, which suggests to me that there is a problem with my paths. 它运行时报告创建的0个文件,这向我提示我的路径存在问题。 However i have defined them based on the same working folder as the other grunt tasks in sails.js. 但是我已经基于与sails.js中其他grunt任务相同的工作文件夹定义了它们。 I have tried a number of variations on the paths, including putting ./ at the start of them, but none seem to work. 我在路径上尝试了多种变体,包括将./放在它们的开头,但似乎没有一种起作用。

Can anyone help explain what is wrong with my grunt file? 谁能帮助解释我的grunt文件出了什么问题? Or how to make it output the folder it is looking at to the console so i can figure out whether my path is correct or not? 或者如何使其输出到控制台的文件夹,这样我就可以弄清楚我的路径是否正确?

Your configuration object is one level too deep. 您的配置对象太深了。 dev is already the task's target and Grunt will start looking for the files property in there. dev已经是任务的目标,Grunt将开始在其中查找files属性。 templates is quietly ignored. templates被静默忽略。

So the configuration object should look like this: 因此,配置对象应如下所示:

dev: {
  options: {
      pretty: true,
      client: true,
      namespace: 'Templates'  
  },
  files: [{
      expand: true,
      cwd: 'views/client/',
      src: ['**/*.jade'],
      dest: '.tmp/public/templates/',
      ext: '.js'
  }]
}

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

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