Run one gulp task on multiple folders. The folders look like plugins/lsmwp-*

时光怂恿深爱的人放手 提交于 2019-12-29 09:36:25

问题


I create a lot of WordPress plugins, each of these all follow the same folder structure and all use Gulp to handle their SASS / JS assets.

Folder Structure is as follows:

|-- .gulpfile.js

|-- lsmwp-one
    |-- plugin.php
    |-- assets
        |-- src
            |-- style.scss
        |-- dist

|-- lsmwp-two
    |-- plugin.php
    |-- assets
        |-- src
            |-- style.scss
        |-- dist

I would like to have a single Gulpfile that watches each 'lsmwp-*' folder, runs all related tasks and outputs to plugin dist folder.

My Gulpfile currently watches these folders but I am having trouble setting the dest location. Any help would be appreciated.

var pluginSrc = 'wp-content/plugins/lsmwp-*/assets/src/style.scss';

gulp.task('lsmwp-plugins', () => {
    gulp.src( pluginSrc )
    .pipe(sass())
    .pipe(concat('style.min.css'))
    .pipe(autoprefixer())
    .pipe(cssmin())
    .pipe(gulp.dest(); // Stuck here...
});

回答1:


It looks like you have two issues. One, running the task separately for each plugin folder. Two, setting the destination relative to each plugin folder.

const gulp = require('gulp');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const autoprefixer = require('gulp-autoprefixer');
const cssmin = require('gulp-cssmin');
const glob = require('glob');

// this gets an array of matching folders
const wpPluginFolders = glob.sync('lsmwp-*');

// for my testing I simplified the folder structure above, you would use
// const wpPluginFolders = glob.sync('wp-content/plugins/lsmwp-*');

const pluginSrc = '/assets/src/style.scss';

gulp.task('default', () => {

  let stream;

  // work on each folder separately
  wpPluginFolders.forEach(function (pluginFolder) {

    stream = gulp.src( pluginFolder + pluginSrc )
      .pipe(sass())
      .pipe(concat('style.min.css'))
      .pipe(autoprefixer())
      .pipe(cssmin())
      .pipe(gulp.dest( pluginFolder + '/assets/dist' ));
  });
  return stream;
});


来源:https://stackoverflow.com/questions/51746981/run-one-gulp-task-on-multiple-folders-the-folders-look-like-plugins-lsmwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!