使用gulp-replace-task在php文件中使用replace(index.php)

时间:2015-12-01 19:29:57

标签: gulp gulp-rev gulp-replace

我正在尝试使用gulp-replace-task替换index.php文件中的文本。我知道这可以在.html.css.js文件中完成,但似乎无法在.php文件中运行。

有人可以帮帮我吗?

这是我的代码:

gulp.task('header', function() {

return gulp.src(rootPath + 'templates/header.php')

    .pipe(replace({
        patterns: [{
            json: {
                'main.css': 'style.css'
            }
        }]
    }))
    // Save the hashed css file
    .pipe(gulp.dest(rootPath + 'templates/header.php'));
});

1 个答案:

答案 0 :(得分:1)

不使用“json”对象查找/替换,而是使用下面gulpfile.js中的RegEx语法。您最终会得到包含替换文字的build/php/templates/header.php

<?php
    $color = "blue";
?>
<head>
    <link rel="stylesheet" src="style.css">
</head>
<body>
    Your color is <?php echo $color; ?>
<body>

gulpfile.js

var gulp = require('gulp')
var replace = require('gulp-replace-task')

var config = {
   src : 'src/php/**/*',
   build: 'build/php'
}

gulp.task('build', function(cb) {
   gulp.src(config.src)
      .pipe(replace({
         patterns: [
            {
               match: /COLOR/,
               replacement: 'blue'
            },
            {
               match: /main.css/,
               replacement: 'style.css'
            }
         ]
      }))
      .pipe(gulp.dest(config.build))

   cb()
})

gulp.task('watch', function(cb) {
   gulp.watch(config.src, ['build']) 
})

gulp.task('default', ['build', 'watch'])

的header.php

<?php
    $color = "COLOR";
?>
<head>
    <link rel="stylesheet" src="main.css">
</head>
<body>
    Your color is <?php echo $color; ?>
<body>

项目文件结构

$ tree -I node_modules
.
├── build
│   └── php
│       └── templates
│           └── header.php
├── gulpfile.js
├── package.json
└── src
    └── php
        └── templates
            └── header.php

6 directories, 4 files