1

I am using gulp replace to replace full path name with another name by matching starting and ending strings

example

input:

src/app/Client/Home/home.service.js

output

dist/Home/home.service.min.js


.state('masterPage.blogArticle', {
            url: "/article/:postId",
            templateUrl: "src/app/Client/Blog/article/index.html",
            controller: "articleCtrl",
            controllerAs: 'vm',
            resolve: {
                deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'competitiveClient',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'src/app/Client/Blog/article/article.service.js',
                            'src/app/Client/Blog/article/article.controller.js'
                        ]
                    });
                }]
            }
        })

Here I want to replace .js file path with above output in all state


gulp.task('templates', () => {
  gulp.src(['dist/client/app/js/app.config.min.js'])
    .pipe(replace(/^(src\/app\/Client\/)(.*)(.js)$/g, 'dist/client/app'))
    .pipe(replace(/.js/g, '.min.js'))
    .pipe(gulp.dest('dist/client/app/js/'));
 });

but it's not working, it not getting matching path so if anyone have idea to solve it. thanks in advance.

Kalpesh Kashyap
  • 824
  • 11
  • 18
  • Unclear what you are seeking. What is the string in your file you are looking to match and what do you want the final string to be after replacement? Please edit your question. – Mark Jan 08 '19 at 15:41
  • You aren't trying to use gulp-replace to rename the file itself are you? It only modifies text within a file not the filename itself. – Mark Jan 08 '19 at 15:51
  • @Mark I have string path as "src/app/Client/Home/home.service.js" and I want to replace string with "dist/Home/home.service.min.js" and there are many string paths with same pattern – Kalpesh Kashyap Jan 08 '19 at 16:14
  • Okay, I edited the answer - I was confused by your replace string in your code 'dist/client/app' that is misleading. – Mark Jan 08 '19 at 16:31
  • @Mark thanks but this still not working, but I have tried this " /src\/app\/Client\/(.*?)\.*\.js/ig " it is working but it replacing all content instead of .js file only – Kalpesh Kashyap Jan 08 '19 at 17:14
  • My answer below works perfectly for me. So you have some other issue in your file. Show an actual cut and paste from a few lines of your file in your question above. – Mark Jan 08 '19 at 17:25
  • @Mark I have updated question, added state please have look – Kalpesh Kashyap Jan 08 '19 at 17:36
  • Updated - is that the result you wanted? – Mark Jan 08 '19 at 17:54
  • @Mark I have updated your answer in which regex is valid and it able to find .js string, but the problem is it replacing all contents – Kalpesh Kashyap Jan 08 '19 at 19:04

2 Answers2

1

Try:

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

  return gulp.src(['dist/client/app/js/app.config.min.js'])

      // need the matching groups 2 and 3 as well, and the m flag

      //.pipe(replace(/^(src\/app\/Client\/)(.*)(.js)$/gm, 'dist/$2$3'))

      .pipe(replace(/(src\/app\/Client\/)(.*)(.js)/g, 'dist/$2$3'))

      // escape the .
      .pipe(replace(/\.js/g, '.min.js'))

      .pipe(gulp.dest('dist/client/app/js/'));
});

src/app/Client/Home/home.service.js

==>

dist/Home/home.service.min.js

[And you'll want the return statement I added in there too.]

Mark
  • 143,421
  • 24
  • 428
  • 436
1

Per this answer, gulp-replace will take a callback as a second argument. The only trick to that is that the full match will be the first param, all matching groups will be the second-through-nth argument, then there are a couple more.

E.G.

replace("my-(.*)", (full, capture) => {return "string: " + capture}) 
ABMagil
  • 4,579
  • 4
  • 21
  • 35