I am trying to move away from gulp and use npm scripts. We have developers that use different versions of node/gulp and the dependence on plugins and deprecation's has been an unwanted hassle.
I am trying to convert our gulp script to npm but there are a few areas I'm stuck on. I'm having issues converting from gulp-sourcemaps to npm map-stream and converting from gulp-uglify to uglifyjs.
Here is the gulp file we are currently using:
/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var pump = require('pump');
var del = require('del');
// set a variable telling us if we're building in release
var isRelease = true;
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'Release') {
isRelease = false;
}
var config = {
//Include all js files but exclude any min.js files
src: ['Scripts/*.js', '!Scripts/*.min.js']
}
//delete the output file(s)
gulp.task('clean', function () {
//del is an async function and not a gulp plugin (just standard nodejs)
//It returns a promise, so make sure you return that from this task function
// so gulp knows when the delete is complete
return del(['Scripts/*.min.js', 'Scripts/Maps/*.map']);
});
// Combine and minify all files from the app folder
// This tasks depends on the clean task which means gulp will ensure that the
// Clean task is completed before running the scripts task.
gulp.task('scripts', ['clean'], function (cb) {
pump([
gulp.src(config.src),
sourcemaps.init(),
uglify({ mangle: isRelease }),
rename({ suffix: '.min' }),
sourcemaps.write('Maps', { includeContent: false, sourceRoot: './' }),
gulp.dest('Scripts/')
],
cb
);
});
//Set a default tasks
gulp.task('default', ['scripts'], function () { });
Here is my current npm script (still has some errors):
/*
This is the main entry point for defiinng npm tasks
*/
const del = require('del');
var map = require('map-stream');
var pump = require('pump');
var vfs = require('vinyl-fs');
var uglifyjs = require('uglify-js');
// set a variable telling us if we're building in release
var isRelease = true;
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'Release') {
isRelease = false;
}
console.log(process.env.NODE_ENV);
//task to delete output files
(async () => {
const deletedPaths = await del(['Scripts/*.min.js', 'Scripts/Maps/*.map']);
console.log('Deleted files and folders:\n', deletedPaths.join('\n'));
})();
var log = function(file, cb) {
console.log(file.path);
cb(null, file);
};
// vinyl metadata object
// Include all js files but exclude any min.js files
pump([
vfs.src(['Scripts/*.js', '!Scripts/*.min.js']),
map(log),
uglifyjs({mangle:isRelease}),
rename({ suffix: '.min' }),
(vfs.dest('Scripts/'))
])
The mapping should create a Maps folder under the Scripts directory.
Scripts
--Maps
--jquery.min.js.map
--jquery-1.4.1.min.js.map
Any help would be appreciated.
Thanks!
Checking the build type (Release, Production, Debug etc.) is done using node so there weren't any changes to be made in this part of the script.
As for the npm del module, I opted for the synchronous method. The reason is that I wanted each file to be processed separately instead of in parallel. The async method kept missing the .acingore file and there aren't many files that need to be deleted anyway.
This block does all the work. This function runs an array of functions in series, each passing their results to the next in the array. Each function is passed a callback on completion.
SIDE NOTE: If you're running node 10+ and you cannot set the path to npm in your .csproj file, install node globally and you don't need to set the path.
Hopefully my comments in the code are sufficient. Good luck if you are trying to move away from gulp and transition to npm!