我尝试配置我的Gruntfile以将所有Jade文件编译为单个HTML文件。例如,如果我有以下源文件夹:
source
└── templates
├── first.jade
├── second.jade
└── third.jade
然后我希望grunt jade
输出:
build
└── templates
├── first.html
├── second.html
└── third.html
这是我的Gruntfile使用grunt-contrib-jade
:
module.exports = function(grunt) {
grunt.initConfig({
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [ {
src: "*.jade",
dest: "build/templates/",
ext: "html",
cwd: "source/templates/"
} ]
}
},
});
grunt.loadNpmTasks("grunt-contrib-jade");
};
但是,当我运行jade命令时,我收到以下错误:
Running "jade:compile" (jade) task
>> Source file "first.jade" not found.
>> Source file "second.jade" not found.
>> Source file "third.jade" not found.
我做错了什么?
答案 0 :(得分:50)
完成上述答案
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [ {
cwd: "app/views",
src: "**/*.jade",
dest: "build/templates",
expand: true,
ext: ".html"
} ]
}
}
因此,如果您的来源结构如此:
app
└── views
└── main.jade
└── user
└── signup.jade
└── preferences.jade
grunt jade
将创建以下结构
build
└── templates
└── main.html
└── user
└── signup.html
└── preferences.html
编辑:
不推荐使用grunt-contrib-jade
。你应该使用grunt-contrib-pug
。它完全一样,但他们不得不将玉重命名为哈巴狗!
答案 1 :(得分:2)
万一有人需要它。上面没有任何工作。这就是它最终对我有用的方式。
我正在使用grunt.loadNpmTasks('grunt-contrib-pug');
我不知道如果contrib-jade已被弃用,但这个解决方案适合我。我需要第一个文件对象来处理index.jade和第二个来处理模板。现在,如果我不拆分它,只是指向项目目录,jade编译器会丢失在我的npm包文件夹中,因此运行速度要快得多。
pug: {
compile: {
options: {
client: false,
pretty: true,
data: {
debug: false
}
},
files: [
{
'dist/index.html': ['index.jade']
},
{
src: "templates/*.jade",
dest: "dist",
expand: true,
ext: ".html"
} ]
}
}
答案 2 :(得分:1)
我知道这是一个老帖子,但我一直在回到这里,同时试图解决类似的问题。我想使用for循环从单个jade模板文件输出多个html文件。所以需要更好地控制文件'对象
我遇到并最终解决的两个问题是设置输出文件名(javascript对象文字KEY)并确保立即运行内联javascript函数以便循环变量可用。
这是我的完整源代码和评论。我希望这对任何绊倒这篇文章的人都有用。
Gruntfile.js:
module.exports = function(grunt) {
// Create basic grunt config (e.g. watch files)
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
grunt: { files: ['Gruntfile.js'] },
jade: {
files: 'src/*.jade',
tasks: ['jade']
}
}
});
// Load json to populate jade templates and build loop
var json = grunt.file.readJSON('test.json');
for(var i = 0; i < json.length; i++) {
var obj = json[i];
// For each json item create a new jade task with a custom 'target' name.
// Because a custom target is provided don't nest options/data/file parameters
// in another target like 'compile' as grunt wont't be able to find them
// Make sure that functions are called using immediate invocation or the variables will be lost
// http://stackoverflow.com/questions/939386/immediate-function-invocation-syntax
grunt.config(['jade', obj.filename], {
options: {
// Pass data to the jade template
data: (function(dest, src) {
return {
myJadeName: obj.myname,
from: src,
to: dest
};
}()) // <-- n.b. using() for immediate invocation
},
// Add files using custom function
files: (function() {
var files = {};
files['build/' + obj.filename + '.html'] = 'src/index.jade';
return files;
}()) // <-- n.b. using () for immediate invocation
});
}
grunt.loadNpmTasks('grunt-contrib-jade');
grunt.loadNpmTasks('grunt-contrib-watch');
// Register all the jade tasks using top level 'jade' task
// You can also run subtasks using the target name e.g. 'jade:cats'
grunt.registerTask('default', ['jade', 'watch']);
};
的src / index.jade:
doctype html
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
if (foo) {
bar(1 + 5)
}
body
h1 #{myJadeName} - node template engine
#container.col
p.
Jade is a terse and simple
templating language with a
strong focus on performance
and powerful features.
test.json:
[{
"id" : "1",
"filename" : "cats",
"tid" : "2016-01-01 23:35",
"myname": "Cat Lady"
},
{
"id" : "2",
"filename" : "dogs",
"tid" : "2016-01-01 23:45",
"myname": "Dog Man"
}]
跑完&#39; grunt&#39;输出应该是:
build/cats.html
build/dogs.html