Last active
July 6, 2017 13:30
-
-
Save CarbonFactory/851d285269e379fb6529 to your computer and use it in GitHub Desktop.
How to make gulp-wirdep pick the minified ".min" files instead of the un-minified files - ASP.net C# template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Description: How to make gulp-wirdep pick the minified ".min" files instead of the un-minified files. The template I've used for ASP.net. You can change the regex in the 'block' option to whichever you want like html or jade | |
* Author: Dev Anand | |
* Author URI: https://github.com/CarbonFactory | |
* License: MIT | |
*/ | |
/** | |
* wiredep options | |
*/ | |
getWiredepOptions = function() { | |
var options = { | |
directory: './bower_components/', | |
bowerJson: bowerJson, | |
ignorePath: '..', | |
fileTypes: { | |
cs: { | |
block: /(([ \t]*)\/\/\s*bower:*(\S*)\s*)(\n|\r|.)*?(\/\/\s*endbower\s*)/gi, | |
detect: { | |
js: /<script.*src=['"](.+)['"]>/gi, | |
css: /<link.*href=['"](.+)['"]/gi | |
}, | |
replace: { | |
css: function(filePath) { | |
if (isProduction && filePath.indexOf(".min") === -1) { | |
var minFilePath = filePath.replace('.css', '.min.css'); | |
var fullPath = path.join(process.cwd(), minFilePath); | |
if (fs.existsSync(fullPath) == undefined) { | |
return '.Include("~' + filePath + '")'; | |
} else { | |
return '.Include("~' + minFilePath + '")'; | |
} | |
} else { | |
return '.Include("~' + filePath + '")'; | |
} | |
}, | |
js: function(filePath) { | |
if (isProduction && filePath.indexOf(".min") === -1) { // isProduction is a variable that is set to determine build type. Its good practise to use unminified files during development | |
var minFilePath = filePath.replace('.js', '.min.js'); | |
var fullPath = path.join(process.cwd(), minFilePath); | |
if (fs.existsSync(fullPath) == undefined) { | |
return '.Include("~' + filePath + '")'; | |
} else { | |
return '.Include("~' + minFilePath + '")'; | |
} | |
} else { | |
return '.Include("~' + filePath + '")'; | |
} | |
} | |
}, | |
} | |
}, | |
dependencies: true, | |
devDependencies: false, | |
onError: function(err) { | |
// log this | |
}, | |
onFileUpdated: function(filePath) { | |
// log this | |
} | |
}; | |
return options; | |
}; | |
/** | |
* wiredep task | |
*/ | |
gulp.task('wiredep', function() { | |
return gulp.src('./App_Start/BundleConfig.cs') | |
.pipe(wiredep(getWiredepOptions())) | |
.pipe(gulp.dest('./App_Start/')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment