我目前正在尝试每页使用一个svg精灵,以便它仅为每个单独的页面加载必要的svg。当前正在工作,但是我想创建一个数组,以便为我想要的N页调用任务。
我目前在gulpfile.js中具有此结构(版本4):
var autoprefixer = require('autoprefixer'),
browsersync = require('browser-sync').create(),
cssnano = require('cssnano'),
sourcemaps = require('gulp-sourcemaps'),
del = require('del'),
gulp = require('gulp'),
imagemin = require('gulp-imagemin'),
newer = require('gulp-newer'),
plumber = require('gulp-plumber'),
postcss = require('gulp-postcss'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
svgSprite = require('gulp-svg-sprite');
//Create SVG's into one single image file
function svgs(){
return gulp
.src('app/images/src/pageName/*')
.pipe(svgSprite({
shape: {
spacing: {
padding: 5
}
},
mode: {
css: {
dest: './',
layout: 'diagonal',
sprite: 'images/dist/pageName/sprite.svg',
bust: false,
render: {
scss: {
dest: 'sass/modules/sprites/_pageName.scss',
template: 'app/sass/modules/sprites/_pageNameTemplate.scss'
}
}
}
},
variables: {
mapname: 'icons'
}
}))
.pipe(gulp.dest('app/'));
}
exports.svgs = svgs;
想法是使用页面名称创建一个数组并执行一个for循环:
var pages = ['page1', 'page2',...];
function svgs(){
for (var i = 0; i < pages.length; i++) {
return gulp
.src('app/images/src/' + pages[i] + '/*').........
}
然后在控制台中仅调用任务,但是返回仅进行一次迭代,任何人以前都曾做过此事?
谢谢!
尝试glob.sync它返回一个数组:
const glob = require('glob');
const pageArray = glob.sync('path/to/your/*.pages');
function svgs() {
pageArray.forEach( page => {
return gulp.src......
}
}