How can gulp-load-plugins replace all gulp- to g

194 Views Asked by At

Default gulp-load-plugins remove all plugin name gulp-

But how can make gulp-load-plugins default replace all plugin name which start with gulp- to g?

For example

gulp-sass

other-gulp-plugin

rename to

gSass

other-gulp-plugin
1

There are 1 best solutions below

0
On

From gulp-load-plugins options :

pattern: ['gulp-', 'gulp.', '@/gulp{-,.}'], // the glob(s) to search for

replaceString: /^gulp(-|.)/, // what to remove from the name of the module when adding it to the context

renameFn: function (name) { ... }, // a function to handle the renaming of plugins (the default works)

So you would need something like this (i.e., untested)

var plugins = require("gulp-load-plugins")({
  renameFn: function(name) {

    //  if only it were as easy as the below : the simple case
    //  return name.replace(/^@*gulp(-|.)(.)/, 'g\U$2\E');

   // but try this 
    return name.replace(/^@*gulp(-|.)(.)/, function (match, p1, p2) {
       return "g" + p2.toUpperCase(); })
   // p2 is the second capture group
   }
});

The simple case:

  1. /@*gulp(-|.) : find the patterns @gulp-, @gulp., gulp- and gulp.
  2. (.) : capture the next character
  3. g\U$2\E : replace only the 2nd capture group (.) with
  4. 'g' followed by capitalized \U 2nd capture group $2 and
  5. end capitalizations of 2nd capture group \E [not really needed in your case since you only want one character].

I left the simple case in for instruction but you probably need the longer uncommented version in node/gulp.