jonkemp / useref

Parse build blocks in HTML files to replace references
MIT License
51 stars 13 forks source link

Feature (Pull) Request: Transform Output Path #10

Closed Bradleycorn closed 6 years ago

Bradleycorn commented 8 years ago

I'm using this library (via gulp-useref) on a .NET project, and as such I need to apply a transform to the final path that is output into the html for each block. In my specific case, I need to wrap the url in src attribute for css refs with a bit of server side .NET code. I've made the necessary updates to add this functionality (it was just 2 lines added to the refManager.js), and if it's useful to others, I can submit a pull request.

For example, given the following input block:

<!-- build:css /Styles/theme-styles.css -->
<link rel="stylesheet" href="Styles/bootstrap.css"/>
<link rel="stylesheet" href="Styles/theme.css"/>
<link rel="stylesheet" href="/Styles/print.css"/>
<link rel="stylesheet" href="/Styles/developer.css"/>
<!-- endbuild --> 

I need to produce the following output (after concatinating and writing the final result css file):

<link rel="stylesheet" href="<%# ResolveRockUrl('~~/Styles/theme-styles.css', true) %>">

To achieve this, I added a "transformOutputPath" option that takes a function with signature:

function(filePath, buildType, extras, searchPaths)

if present, this function will be passed the target output path, the build type (css, js, etc), the extra attributes in the build tag, and the search paths included in the build tag. The function should return a string, and that string is used as the target when constructing the final output html. Again using the example above, my options passed into useref (again, via gulp-useref) to achieve the desired result are:

{
  transformOutputPath: function(filePath, buildType, extras, searchPaths) {
     if (buildType == 'css') {
          filePath = "<%# ResolveRockUrl('~~" + filePath + "', true) %>";
     }
     return filePath;
  }
}

As I mentioned above, if this feature is useful to others, I can submit a pull request.

jonkemp commented 6 years ago

Fixed 647f0d2eaf967d7a5b76ac6a27d56b631f9487d4.