this project uses the gulp-replace plug-in to implement the include function (introducing public header.html, footer.html, etc.). There was no problem with the project before, but later, as the project grew larger, the problem of path and packaging time was exposed, the first packaging can be achieved, but the second packaging path will have problems, and the packaging time will become long. The function implementation code is as follows:
// include
function includeFn () {
return gulpReplace(/\<include.*<\/include>/g, function (a, b) {
var regSrc = /(src[ ]*=[ ]*[""]([^"]*)[""])/g,
result = regSrc.exec(a),
fileCon, src, srcReal;
if ( !result) {
return a;
}
src = result[2].trim();
srcReal = path.resolve(path.parse($.streamFilePath).dir, src);
if (!$[srcReal]) {
try {
$[srcReal] = fs.readFileSync(srcReal).toString();
} catch (err) {
console.log(chalk.bgRed(":") + "" + $.streamFilePath);
console.log(err);
}
}
fileCon = $[srcReal];
// //
// var regOn = /(on[]*=[]*[""]([\d]+)[""])/g,
// onResult = regOn.exec(a),
// num;
// if ( onResult ) {
// num = parseInt(onResult[1], 10);
// }
return fileCon;
});
}
// htmlinclude,include
gulp.task("dev-html", function () {
return gulp.src(["src/**/*.html", "!src/**/include/*.html","!src/common/**/*.html"]).
//
pipe(gulpChanged(EVN_PRO)).
//
pipe(mapStream(function (file, cb) {
$.streamFilePath = file.history[0];
cb(null, file);
})).
pipe(gulpDebug({title: chalk.bgBlue("---HTML: ")})).
pipe(includeFn()).
pipe(gulpAbsolute({dev: "src"})).
pipe(gulp.dest(EVN_PRO)).
pipe(browserSync.stream());
});
the project path is as follows:
I hope the bosses can take a look at it and give some suggestions and ideas. Thank you!