Skip to content

Commit

Permalink
🔧 update gulpfile.js
Browse files Browse the repository at this point in the history
  • Loading branch information
phucbm committed Apr 4, 2022
1 parent ccad26a commit 408857e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
64 changes: 63 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const rename = require("gulp-rename");
const uglify = require('gulp-uglify-es').default;
const cleanCSS = require('gulp-clean-css');
const replace = require('gulp-replace');
const readlineSync = require('readline-sync');

// Static server
gulp.task('serve', function(){
Expand All @@ -13,4 +18,61 @@ gulp.task('serve', function(){
// serve files from root directory
server: {baseDir: "./"}
});
});
});

// Minify JS
gulp.task('minify-js', function(){
return gulp.src(['build/*.js'])
.pipe(rename({extname: '.min.js'}))
.pipe(uglify(/* options */))
.pipe(gulp.dest("dist"));
});

// Minify CSS
gulp.task('minify-css', () => {
return gulp.src(['build/*.css'])
.pipe(rename({extname: '.min.css'}))
.pipe(cleanCSS({debug: true}, (details) => {
console.log(`Original ${details.name}: ${details.stats.originalSize}`);
console.log(`Minified ${details.name}: ${details.stats.minifiedSize}`);
}))
.pipe(gulp.dest('dist'));
});

// Replace version
let oldVersion = '', newVersion = '', count = 1;
const replaceFiles = ['index.html', 'README.md', 'build/*'];
gulp.task('replace', function(){
return gulp.src(replaceFiles)
.pipe(replace(oldVersion, function handleReplace(match){
console.log(`[${count}] Found "${oldVersion}", replace with "${newVersion}"`);
count++;
return newVersion;
}))
.pipe(gulp.dest(function(file){
console.log(file.base)
return file.base;
}, {overwrite: true}));
});

// gulp release
gulp.task('release', gulp.series(
function(done){
oldVersion = readlineSync.question('Enter the current version to replace: ');
return done();
},
function(done){
newVersion = readlineSync.question('New version: ');
return done();
},
function(done){
if(readlineSync.keyInYN(`Do you want to replace "${oldVersion}" with "${newVersion}" in [${replaceFiles}]?`)){
return done();
}
console.log('Ok, not replace, stop releasing.');
process.exit(1);
},
'replace',
'minify-css',
'minify-js',
));
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
"devDependencies": {
"browser-sync": "^2.27.7",
"gulp": "^4.0.2"
},
"dependencies": {
"gulp-clean-css": "^4.3.0",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.3",
"gulp-uglify-es": "^3.0.0",
"readline-sync": "^1.4.10"
}
}

0 comments on commit 408857e

Please sign in to comment.