I want to minify some JS files with Gulp, but can't seem to get control over the process. I want only the minified version in the destination, and am currently getting copies of the originals as well.
I'm thinking I may need the rename package, but am not sure how to use it for this task - I would presumably need some kind of variable to hold the current file name for each script.
Any help much appreciated. The code is below:
var gulp = require( 'gulp' );
var minify = require( 'gulp-minify' );
var rename = require( 'gulp-rename' );
//script paths
var jsFiles = 'js/**/*.js',
jsDest = 'js/dist/';
gulp.task('scripts', function() {
return gulp.src(jsFiles)
.pipe(minify())
.pipe(gulp.dest(jsDest));
});
the minfied HTML files are output to the declared destination. The below code first declares a new gulp task, ‘gulp clean’, which deletes everything in the output folder (this is to ensure that only the required files are in the output folder). The code then declares a task, ‘gulp default’, which runs the minification tasks in sequence: 4.
The gulp-minify plugin minifies JS files. The resulting files are lighter. The minified files have the -min.js extension. We initiate a Node.js project and install Gulp and gulp-minify plugin. We initiate a Node project and install Gulp and Gulp CLI.
Once you have installed Node.js on your computer, you need to create a file called package.json and declare the npm packages that will be used for the minification process. Copy and paste the code below into your package.json file: 3. Create a ‘gulpfile.js’ file and add the required code Next, create a file called gulpfile.js.
To use gulp, you need to create a file called gulpfile.js and save it at the root of your project. Do that first. Then paste the following into that file: Your default task will run, and you should see some output similar to this:
Set the noSource
config like the following
.pipe(minify({noSource: true})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With