1

我正在使用 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/'));
 });

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

4

2 回答 2

1

尝试:

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

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

于 2019-01-08T04:51:26.680 回答
1

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

例如

replace("my-(.*)", (full, capture) => {return "string: " + capture}) 
于 2019-10-14T19:59:22.357 回答