-
Notifications
You must be signed in to change notification settings - Fork 339
add suport for block replacement for custom block types #337
Conversation
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" |
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 :) |
This is so insanely helpful! I hope the devs accept this merge request soon! |
Yes Please!!!! +++++ 👍 |
+1 |
@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? |
@futurechan I just did a rebase to master and resolved the merge conflict, enjoy 😄 |
@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? |
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 |
Need some docs, but other than that 👍 |
Cool, I'll add some docs then |
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/> |
There was a problem hiding this comment.
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
fix linebreaks in README.md
@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 :) |
add suport for block replacement for custom block types
Thanks :)
No commitment required. |
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
this configuration:
and have this result:
If this works out for you, I can help updating the docs too.
What do you think?