使用gulp创建样式指南/样式库
我知道已经有大量的自动化工具可以创建样式指南/模式库,但出于学习的兴趣,我想看看是否可以推出自己的样式指南/模式库 编制SASS是直截了当的。js也是如此。我还可以看到如何用一个类包装来自多个文件的HTML块,并将其编译成单个文件。非常适合在一个页面上同时显示所有“部分”使用gulp创建样式指南/样式库,gulp,filenames,Gulp,Filenames,我知道已经有大量的自动化工具可以创建样式指南/模式库,但出于学习的兴趣,我想看看是否可以推出自己的样式指南/模式库 编制SASS是直截了当的。js也是如此。我还可以看到如何用一个类包装来自多个文件的HTML块,并将其编译成单个文件。非常适合在一个页面上同时显示所有“部分” gulp.task('inject:wrap', function(){ return gulp.src('./_patterns/*/*/*.html') /// get the partial html filename
gulp.task('inject:wrap', function(){
return gulp.src('./_patterns/*/*/*.html')
/// get the partial html filename here and insert below @@@
.pipe(inject.wrap('<div id="@@@" class="pattern">', '</div>'))
.pipe(concat('patterns.html'))
.pipe(gulp.dest('build'));
});
gulp.task('process', ['inject:wrap']);
gulp.task('inject:wrap',function(){
返回gulp.src('./_patterns/*/*/*.html')
///在此处获取部分html文件名并在下面插入@@@
.管道(注入.包裹('',''))
.pipe(concat('patterns.html'))
.pipe(吞咽目标(“构建”);
});
任务('process',['inject:wrap']);
我正在努力解决的是如何获取块的文件名,比如说_button.html,并将其作为上面的元素id“@@@@”传递给包装器。然后我可以使用它来构建样式指南导航/锚定链接。这里有一个示例代码,使用jade模板语言(它自己负责注入、部分、评估等);有两个任务,一个生成静态HTML页面,另一个预编译模板以用作AMD中包装的运行时模板函数
// preprocess & render jade static templates
gulp.task('views:preprocess', function () {
return gulp.src([ 'source/views/*.jade', '!source/views/layout.jade' ])
.pipe(plumber()) // plumber, because why not?
.pipe(data(function (file) {
// prepare data to be passed to the template
// here we can use the file name to map specific data to each file
return _.assign(settingsData, { timestamp: timestamp });
}))
// render template with data
.pipe(jade())
.pipe(gulp.dest('destination'));
});
// precompile jade runtime templates
gulp.task('views:precompile', function () {
// grab folder names
var folders = fs.readdirSync('source/templates').filter(function (file) {
return fs.statSync(path.join('source/templates', file)).isDirectory();
});
// create a separate task for each folder
var tasks = folders.map(function (folder) {
return gulp.src(path.join('source/templates', folder, '*.jade'))
.pipe(plumber())
// pre-compile the template as functions, for runtime
.pipe(jade({
client: true
}))
// wrap it in AMD, so we can use stuff like require.js to fetch them later
.pipe(wrap({
moduleRoot: 'source/templates',
modulePrefix: 'templates',
deps: [ 'jade' ],
params: [ 'jade' ]
}))
// concat all the templates in each folder to a single .js file
.pipe(concat(folder + '.js'))
.pipe(uglify())
.pipe(header(banner, { package: packageData }))
.pipe(gulp.dest('destination/scripts/templates'));
});
return merge(tasks);
});
我使用的模块有merge stream
,path
,gulp
,fs
,gulp data
,gulp jade
,gulp plumber
等。
我不太明白你想要实现什么,但我希望这能给你一些线索。这是我得到的一个示例代码,使用jade模板语言(它自己负责注入、部分、求值等);有两个任务,一个生成静态HTML页面,另一个预编译模板以用作AMD中包装的运行时模板函数
// preprocess & render jade static templates
gulp.task('views:preprocess', function () {
return gulp.src([ 'source/views/*.jade', '!source/views/layout.jade' ])
.pipe(plumber()) // plumber, because why not?
.pipe(data(function (file) {
// prepare data to be passed to the template
// here we can use the file name to map specific data to each file
return _.assign(settingsData, { timestamp: timestamp });
}))
// render template with data
.pipe(jade())
.pipe(gulp.dest('destination'));
});
// precompile jade runtime templates
gulp.task('views:precompile', function () {
// grab folder names
var folders = fs.readdirSync('source/templates').filter(function (file) {
return fs.statSync(path.join('source/templates', file)).isDirectory();
});
// create a separate task for each folder
var tasks = folders.map(function (folder) {
return gulp.src(path.join('source/templates', folder, '*.jade'))
.pipe(plumber())
// pre-compile the template as functions, for runtime
.pipe(jade({
client: true
}))
// wrap it in AMD, so we can use stuff like require.js to fetch them later
.pipe(wrap({
moduleRoot: 'source/templates',
modulePrefix: 'templates',
deps: [ 'jade' ],
params: [ 'jade' ]
}))
// concat all the templates in each folder to a single .js file
.pipe(concat(folder + '.js'))
.pipe(uglify())
.pipe(header(banner, { package: packageData }))
.pipe(gulp.dest('destination/scripts/templates'));
});
return merge(tasks);
});
我使用的模块有merge stream
,path
,gulp
,fs
,gulp data
,gulp jade
,gulp plumber
等。
我不太明白你想要实现什么,但我希望这能给你一些线索。为什么不使用模板引擎,比如jade或Handlebar?我可以给你发一些我写的示例代码,它与jade一起工作,获取文件/文件夹名,并做更复杂的事情…谢谢Soheil。我不确定你是否理解我想要实现的目标,但fn看起来可能有用。我建议你尝试一下,或者。两者都是模板引擎不可知的模式库/样式指南/文档生成器。为什么不使用模板引擎,如jade或Handlebar?我可以给你发一些我写的示例代码,它与jade一起工作,获取文件/文件夹名,并做更复杂的事情…谢谢Soheil。我不确定你是否理解我想要实现的目标,但fn看起来可能有用。我建议你尝试一下,或者。两者都是模板引擎不可知模式库/样式指南/文档生成器。