问题
I have been using GULP 4 for little over a week now and have never used it before. The code below is what I have been playing around with for a week now, so that it will do the job I have asked of it. My question, is it necessary to copy every file in a project from the src to dist directory, especially on change, as I have over 60 php files, and to update every php file on change does not strike me as being efficient. First, is necessary to copy all project files from the src to dist on change. Second, is there a way to just update the one file that has been modified in the src directory? In the past I haven't looked at using automated tools such as GULP, however; the emphasis is on using such tools in the development process to save time, along with other benefits that help the cause. As a beginner, it is going to take some time to appreciate these benefits. Any improvements that you may see with respect with the code I have given would be much appreciated. Kindest Regards
const gulp = require('gulp');
const php = require('gulp-connect-php');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const lineec = require('gulp-line-ending-corrector');
const browserSync = require('browser-sync').create();
const styleSRC = './src/scss/**/*.scss';
const styleDIST = './dist/css';
const jsSRC = 'src/js/**/*.js';
const jsDIST = './dist/js';
const phpSRC = 'src/php/**/*.php';
const phpDIST = './dist/php';
const htmlSRC = 'src/html/**/*.html';
const htmlDIST = './dist/html';
function style()
{
return gulp.src(styleSRC)
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest(styleDIST))
.pipe(browserSync.stream());
}
function javascript() {
return gulp.src(jsSRC)
.pipe(uglify())
.pipe(lineec())
.pipe(gulp.dest(jsDIST));
}
function phpscript() {
return gulp.src(phpSRC)
.pipe(gulp.dest(phpDIST));
}
function server()
{
php.server({base:'./src/php', port:8010, keepalive:true});
}
function sync()
{
browserSync.init({
proxy: "http://lansdownelions/src/php/login.php",
baseDir: './src/php',
open: true,
notify: false
});
}
function watch()
{
gulp.watch(styleSRC, style);
gulp.watch(jsSRC, javascript);
gulp.watch(jsSRC).on('change', browserSync.reload);
gulp.watch(phpSRC, phpscript);
gulp.watch(phpSRC).on('change', browserSync.reload);
gulp.watch(htmlSRC).on('change', browserSync.reload);
}
exports.style = style;
exports.javascript = javascript;
exports.phpscript = phpscript;
exports.server = server;
exports.sync = sync;
exports.watch = watch;
var build = gulp.parallel(style, javascript, phpscript, sync, server, watch);
gulp.task('default', build);
回答1:
Solved what I wanted to achieve based on the post. To do this, all I needed to do was to change one of the functions with an extra line of code shown below. This change works like a charm, and as a benefit, the files in the dist sub-folder, only appear to overwrite if the date/timestamp are different. This saves time when running $ gulp default from the command line where no files copy to the dist sub-directories when run, if the files have not changed. I want to thank each and everyone for the valid contributions, which really has helped me in different areas with gulp. Kindest Regards
//original function code:
function phpscript()
{
return gulp.src(phpSRC)
.pipe(gulp.dest(phpDIST));
}
// changed function code to:
function phpscript()
{
return gulp.src(phpSRC)
.pipe(changed(phpDIST))
.pipe(gulp.dest(phpDIST));
}
来源:https://stackoverflow.com/questions/56023181/new-to-gulp-is-it-necessary-to-copy-all-files-from-src-directory-to-dist-direc