gulp-useref icon indicating copy to clipboard operation
gulp-useref copied to clipboard

Possible to use different types?

Open robdodson opened this issue 11 years ago • 17 comments

Is it possible to add additional types for the build blocks, for example:

<!-- build:html elements/elements.vulcanized.html -->
  <link rel="import" href="elements/elements.html">
<!-- endbuild-->

I'd like to do this to support HTML Imports

robdodson avatar Jan 27 '15 03:01 robdodson

I'll mark this as a feature request. Thanks!

jonkemp avatar Jan 27 '15 14:01 jonkemp

@robdodson I was just looking for how to do the very same.

@jonkemp if you can give me some clues where to start, I can start looking into implementing this.

therealklanni avatar Mar 06 '15 18:03 therealklanni

@therealklanni probably need to start with the useref module which is in a separate repo.

jonkemp avatar Mar 06 '15 19:03 jonkemp

@jonkemp way ahead of you there. Can you give me some insight with gulp-useref, though?

I'm working on implementing custom blocks in node-useref, and I see that in gulp-useref we directly call that. So my thought was to just pass an options object there to useref. I figured options can be callbacks by type, such as

useref(input, {
  import: function (importBlocks) {
    // do something with importBlocks (array of useref blocks found matching "build:import")
  }
});

So I guess what I need to know about gulp-useref is what is the best way to handle the options?

therealklanni avatar Mar 06 '15 21:03 therealklanni

I'm thinking the best way to implement this in node-useref is as follows:

<!-- build:import components -->
<link rel="import" href="/bower_components/some/path"></link>
<!-- endbuild -->

In this example, for a custom use block, the path part of the directive ("components" here) would just be exposed to the handler as such:

useref(input, {
  import: function (importBlocks) {
    var components = importBlocks.components.assets; // <-- components here is associated with the `path` in the directive.
  }
});

This is just how useref currently constructs the references object, but we can potentially improve on that. Of course, normally that path value is just used to later concat all the blocks that were given the same path into a single file. Being that we're adding custom behavior, I thought it was best to leave any concatenation to the handler. useref also allows an "extra attributes" field in the directive, which I think was added specifically to support adding attributes to a tag, but I can generalize this behavior so it can be used as an "additional options" field instead (extra attributes or options, depending on context), allowing more options to be passed to a custom block, if that would be useful to anyone. Maybe that could be where a concat option can be enabled to tell useref to follow the default behavior of concatenating the results into a file?

@robdodson @jonkemp please give me any feedback/suggestions for how you think we can improve the interface.

therealklanni avatar Mar 06 '15 22:03 therealklanni

What you have above looks good to me. Being able to define my own handler is ideal.

robdodson avatar Mar 10 '15 02:03 robdodson

Slightly different result for the handler signature. See my included documentation for details.

therealklanni avatar Mar 10 '15 17:03 therealklanni

FYI, my changes to useref have been merged and published as 0.3.9. So someone should be able to add the support for that to gulp-useref now. :metal:

therealklanni avatar Mar 17 '15 02:03 therealklanni

How can I pass the customized type settings to useref? Can we pass an opts object to useref like this?

var useref = require('gulp-useref');

gulp.src('index.html')
  .pipe(useref({import: function(content, target, options) { /*...*/ }}));

bcho avatar Apr 15 '15 06:04 bcho

@bcho yes, exactly (for useref, it's not implemented in gulp-useref, yet, as far as I know). See digisfera/useref#33

therealklanni avatar Apr 15 '15 08:04 therealklanni

Is it possible for to dynamically provide a filename for the build operations? After spending some time investigating 'import' I've found that it does not concatenate files in a custom block.. The following is what I am trying to do:

   <!-- build:js ${requestScope.contextRoot}/dist/vendor.min.js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<!-- endbuild -->

The code resides in a JSP so I am making the code be context root aware.. Today my project layout is: -app   -css   -images   -js -dist   -all minified/compiled code

If I set context root as '/' then I need to add '/dist/' to all my src/hrefs.. I thought I'd be able to handle this via custom blocks but file concatenation does not occur in that case?

Thoughts?

doconnor78 avatar Oct 07 '15 04:10 doconnor78

Hi, Has this been implemented in gulp-useref? I could really use it for building my Polymer elements in my Angular 2 app.

Thanks in advance

emanuelet avatar Jul 28 '16 08:07 emanuelet

@emanuelet which thing are you referring to? Custom blocks are supported it would seem based on the status of this issue.

jonkemp avatar Jul 28 '16 13:07 jonkemp

I know that they are supported in the userref package but is not clear how to use them in the gulp-userref one.

Forgive my ignorance, I'm pretty sure that is a very simple syntax, but if you can provide me an example that will be wonderful

emanuelet avatar Jul 28 '16 13:07 emanuelet

A look at the tests gave me an example.

useref({
    custom: function (content, target) {
        return content === 'someContent' ? target : content;
    }
});

and the build block looks like this:

<!-- build:custom customResult -->
someContent
<!-- endbuild -->

jonkemp avatar Jul 28 '16 13:07 jonkemp

I knew that it was somewhere. Thanks you so much for the help.

P.S.: I think this should be added to the Readme

emanuelet avatar Jul 28 '16 13:07 emanuelet

That's how I got it to work

This has to go in the index.html (notice the I had to spin-off the Polymer initialisation to its own file)

    <!-- build:js assets/webcomponents.js -->
    <script src="/assets/components/webcomponentsjs/webcomponents.js"></script>
    <!-- endinject -->
    <!-- build:js assets/polymer.js -->
    <script src="/assets/scripts/polymer.js"></script>
    <!-- endinject -->
    <!-- build:custom elements.html -->
    <link rel="import" href="/assets/components/polymer/polymer.html">
    <link rel="import" href="/assets/components/paper-button/paper-button.html">
    <link rel="import" href="/assets/components/paper-input/paper-input.html">
    <!-- endinject -->

And this is the change needed in the build.js

        gulp.src(config.assetsPath.components + '**/*.*', {
                base: config.assetsPath.components
            })
            .pipe(gulp.dest(config.build.assetPath + 'components'));

        gulp.src(config.index)
            .pipe(useref({
                custom: function(content, target) {
                    return content === '/assets/components' ? target : content;
                }
            }))

useref is still not able to combine the html files but now it will keep the references instead of stripping them

emanuelet avatar Jul 29 '16 00:07 emanuelet