node.js - How to load contents of an external file into gulp browser-sync -


i loading browser-sync proxy , want load search , replace terms external file in order amend page loaded browser.

the reason want load search , replace terms separate file because want make use of gulp-watch , reload browser-sync search , replace terms updated.

my project folder:

  • regex/search.txt <- search term stored in file
  • regex/replace.txt <- replace term stored in file
  • gulpfile.js

contents of gulpfile.js:

var gulp = require('gulp'), fs = require("fs"), browsersync = require('browser-sync');  var proj_url = "http://www.example.com"; var search_text = ""; var replace_text = "";  gulp.task('readregex', function() {     return gulp.src('regex/*.txt')     .pipe(fs.readfile("regex/search.txt", "utf-8", function(err, data) {         search_text = data;     }))     .pipe(fs.readfile("regex/replace.txt", "utf-8", function(err, data) {         replace_text = data;     })) });  gulp.task('browser-sync', function() {     browsersync({         proxy: {             target: proj_url         },         rewriterules: [             {                 match: search_text,                 fn: function (match) {                     return replace_text;                 }             }         ]     }); });  gulp.task('default', ['readregex','browser-sync'], function() {     gulp.watch(['regex/*.txt'], [browsersync.reload]); }); 

this doesn't work. following error:

typeerror: cannot call method 'on' of undefined ... 

for work need make browser-sync dependant in readregex

gulp.task('browser-sync', ['readregex'], function() { 

this guarantees proper execution order.

then can make readregex sync (and simpler) way:

gulp.task('readregex', function() {     search_text = fs.readfilesync("regex/search.txt", "utf-8").tostring();     replace_text = fs.readfilesync("regex/replace.txt", "utf-8").tostring(); }); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -