Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

add suport for block replacement for custom block types #337

Merged
merged 2 commits into from
Jun 23, 2014

Conversation

mcampo
Copy link
Contributor

@mcampo mcampo commented Apr 6, 2014

Be able to define custom block replacement logic for custom block types (types other than 'css' and 'js').

We can have a "less" block type and define a flow for it and configure the "less" task properly. The resulting css file is generated and placed in the correct dest directory.

But blocks with type other that "css" or "js" won't be correctly "summarized" in the html. I get "undefined" instead of the proper "link" tag referencing the output file.

Main motivation in my case is to be able to define a block for .less files, call less processor in a custom flow (already possible) and have the resulting css file included in a single 'link' tag. This PR addresses the last part.

With this PR, you can have:

This block in your html

<!-- build:less styles/main.css -->
<link rel="stylesheet/less" href="styles/main.less">
<link rel="stylesheet/less" href="styles/home.less">
...
<!-- endbuild -->

this configuration:

    useminPrepare: {
      html: 'index.html',
      options: {
        dest: 'dist',
        flow: { 
            steps: { 
                'js': ['concat', 'uglifyjs'], 
                'css': ['concat', 'cssmin'],
                'less': [{
                    name: 'less',
                    createConfig: lessCreateConfig
                }]
            },
            post: {}
        }
      }
    },

    usemin: {
      html: ['dist/index.html'],
      options: {
        blockReplacements: {
            less: function (block) {
                return '<link rel="stylesheet" href="' + block.dest + '" />';
            }
        }
      }
    }

and have this result:

<link rel="stylesheet" href="styles/main.css">

If this works out for you, I can help updating the docs too.

What do you think?

@jaymes-bearden
Copy link

This is awesome! It would also be incredibly helpful if we had the ability to modify the other default types.

For example, I use grunt to modify Velocity template files which are then processed server side. It would be great if I could alter how the link / script href's get output to include other things such as ${request.contextPath} -- ex: link href="${request.contextPath}/resources/css/site.d39064.css" type="text/css"

@mcampo
Copy link
Contributor Author

mcampo commented Apr 29, 2014

You can override replacement functions for default block types the same way you add new block types

Defaults are defined like this:

var defaultBlockReplacements = {
  'css': function (block) {
    var media = block.media ? ' media="' + block.media + '"' : '';
    return '<link rel="stylesheet" href="' + block.dest + '"' + media + '/>';
  },
  'js': function (block) {
    var defer = block.defer ? 'defer ' : '',
        async = block.async ? 'async ' : '';
    return '<script ' + defer + async + 'src="' + block.dest + '"><\/script>';
  }
};

So, to override then you could do this on your Gruntfile:

    usemin: {
      html: ['dist/index.html'],
      options: {
        blockReplacements: {
          'css': function (block) {
            var media = block.media ? ' media="' + block.media + '"' : '';
            return '<link rel="stylesheet" href="${request.contextPath}/' + block.dest + '"' + media + '/>';
          },
          'js': function (block) {
            var defer = block.defer ? 'defer ' : '',
                async = block.async ? 'async ' : '';
            return '<script ' + defer + async + 'src="${request.contextPath}/' + block.dest + '"><\/script>';
          }
        }
      }
    }

Hope this helps :)

@jaymes-bearden
Copy link

This is so insanely helpful! I hope the devs accept this merge request soon!

@eirslett
Copy link

eirslett commented Jun 2, 2014

Yes Please!!!! +++++ 👍

@futurechan
Copy link

+1

@futurechan
Copy link

@mcampo I would really like to use this immediately. Since there are merge conflicts, how can I fork your fork and merge yeoman's into yours?

@mcampo
Copy link
Contributor Author

mcampo commented Jun 19, 2014

@futurechan I just did a rebase to master and resolved the merge conflict, enjoy 😄

@mcampo
Copy link
Contributor Author

mcampo commented Jun 19, 2014

@sindresorhus @XhmikosR do you think this could be useful?

Seems to be some interest in this, and I know I don't to be rebasing all the time for getting updates :)

What do you think?

@XhmikosR
Copy link
Contributor

I can see how this can be useful, indeed. From a quick look, it looks good to me. But I'd definitely want someone else's opinion too before merging this.

/CC @yeoman/yeoman-contributors

@XhmikosR XhmikosR added this to the 2.2.1 milestone Jun 19, 2014
@sindresorhus
Copy link
Member

Need some docs, but other than that 👍

@mcampo
Copy link
Contributor Author

mcampo commented Jun 19, 2014

Cool, I'll add some docs then

@mcampo
Copy link
Contributor Author

mcampo commented Jun 20, 2014

I documented this change on the readme file, @sindresorhus let me know if there's anything else that should be there before merging.

@@ -327,7 +327,7 @@ By default `usemin` will look under `dist/html` for revved versions of `styles/m

#### assetsDirs

Type: 'Array'
Type: 'Array' <br/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two space is linebreak in markdown

@sindresorhus
Copy link
Member

@mcampo Awesome work on this!

Would you be interested in helping out more? We would be happy to add you to the repo if so. No worries if not :) #313

fix linebreaks in README.md
@mcampo
Copy link
Contributor Author

mcampo commented Jun 23, 2014

@sindresorhus Thanks! I made the requested changes. I left them in a separate commit, let me know if you want me to squash it.

I can't commit to weekly reviews, but I'll be happy to help however I can :)

sindresorhus added a commit that referenced this pull request Jun 23, 2014
add suport for block replacement for custom block types
@sindresorhus sindresorhus merged commit 2a6b574 into yeoman:master Jun 23, 2014
@sindresorhus
Copy link
Member

Thanks :)

I can't commit to weekly reviews, but I'll be happy to help however I can :)

No commitment required.

@mcampo mcampo deleted the custom-types branch June 23, 2014 21:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants