替换为正则表达式以进行 gulp 替换



我正在使用gulp replace 通过匹配开始和结束字符串将完整路径名替换为另一个名称

输入:

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

输出

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'
                        ]
                    });
                }]
            }
        })

在这里我想在所有状态下用上面的输出替换.js文件路径


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/'));
 });

但它不起作用,它没有得到匹配的路径所以如果有人有想法来解决它。提前谢谢。

尝试:

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

[你也会想要我在那里添加的返回语句。

根据这个答案,gulp-replace将回调作为第二个参数。唯一的诀窍是完全匹配将是第一个参数,所有匹配组将是第二个到第 n 个参数,然后还有更多。

例如

replace("my-(.*)", (full, capture) => {return "string: " + capture}) 

相关内容

  • 没有找到相关文章

最新更新