How can I register a pipe for gulp-replace for several js in gulpfile.js ?

In index.html :

 <script src="js/main.js"></script> <script src="js/plagin-1.js"></script> <script src="js/plagin-2.js"></script> 

In gulpfile.js now:

 .pipe(replace('main.js', assets['main.js'])) 

How to make a replacement for all js files, and not for one main.js?

  • If simple, add .pipe(replace('plagin-1.js', assets['plagin-1.js'])) and .pipe(replace('plagin-2.js', assets['plagin-2.js'])) (if there are many files, then of course you need a more automated way). - Vladimir Gamalyan
  • I wanted to know more about the more automated one, but apparently there isn’t for this plug-in) - HamSter
  • one
    You can try this plugin npmjs.com/package/gulp-replace-assets , it accepts an object in which the keys are the replaced strings, and the values ​​are the replacement strings. Just what gulp-hash gives in assets.json - Vladimir Gamalyan
  • Thanks, helped, everything is fine and just works! - HamSter
  • You can throw a star here github.com/vladimirgamalian/gulp-replace-assets if it helped) - Vladimir Gamalyan

1 answer 1

You can use regular expressions. For example, like this:

 .pipe(replace(/(.{4,8})\.js/g, 'foo')) 

Where:
.{4,8} - any character from 4 to 8 times in the name
\. - point
js - the letters "j" and "s" in the extension

More information on the link: gulp-replace

  • I know about regulars. I need to match the names - 'main.js', assets ['main.js']! So .pipe (replace (/ (. {4,8}) \. Js / g, '/(.4,8})\\.js/g')) you can't write) - HamSter