This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 339
add suport for block replacement for custom block types #337
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ Blocks are expressed as: | |
<!-- endbuild --> | ||
``` | ||
|
||
* **type**: either `js` or `css` | ||
* **type**: can be `js`, `css` or a custom type with a [block replacement function](#blockreplacements) defined | ||
* If another type, the block will be ignored. Useful for "development only" blocks that won't appear in your build | ||
* **alternate search path**: (optional) By default the input files are relative to the treated file. Alternate search path allows one to change that | ||
* **path**: the file path of the optimized file, the target output | ||
|
@@ -327,7 +327,7 @@ By default `usemin` will look under `dist/html` for revved versions of `styles/m | |
|
||
#### assetsDirs | ||
|
||
Type: 'Array' | ||
Type: 'Array' <br/> | ||
Default: Single item array set to the value of the directory where the currently looked at file is. | ||
|
||
List of directories where we should start to look for revved version of the assets referenced in the currently looked at file. | ||
|
@@ -344,7 +344,7 @@ usemin: { | |
|
||
#### patterns | ||
|
||
Type: 'Object' | ||
Type: 'Object' <br/> | ||
Default: Empty | ||
|
||
Allows for user defined pattern to replace reference to files. For example, let's suppose that you want to replace | ||
|
@@ -375,9 +375,36 @@ So in short: | |
* FIXME | ||
* FIXME | ||
|
||
#### blockReplacements | ||
|
||
Type: 'Object' <br/> | ||
Default: `{ css: function (block) { ... }, js: function (block) { ... } }` | ||
|
||
This lets you define how blocks get their content replaced. Useful to have block types other that `css` and `js`. | ||
|
||
* Object key matches a block type | ||
* Value is the replacement function for that block type. | ||
* The replacement function gets called with a single argument: a [block](#block) object. | ||
* Must return a `String`, the "summary" line that will replace the block content. | ||
|
||
For example, to create a `less` block you could define its replacement function like this: | ||
|
||
```js | ||
usemin: { | ||
html: index.html, | ||
options: { | ||
blockReplacements: { | ||
less: function (block) { | ||
return '<link rel="stylesheet" href="' + block.dest + '" />'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
#### revmap | ||
|
||
Type: 'String' | ||
Type: 'String' <br/> | ||
Default: Empty | ||
|
||
Indicate the location of a map file, as produced by `grunt-filerev` for example. This map file is a simple JSON file, holding an object | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,22 @@ var _defaultPatterns = { | |
] | ||
}; | ||
|
||
var FileProcessor = module.exports = function (patterns, finder, logcb) { | ||
// | ||
// Default block replacement functions, for css and js types | ||
// | ||
var defaultBlockReplacements = { | ||
css: function (block) { | ||
var media = block.media ? ' media="' + block.media + '"' : ''; | ||
return '<link rel="stylesheet" href="' + block.dest + '"' + media + '/>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
js: function (block) { | ||
var defer = block.defer ? 'defer ' : ''; | ||
var async = block.async ? 'async ' : ''; | ||
return '<script ' + defer + async + 'src="' + block.dest + '"><\/script>'; | ||
} | ||
}; | ||
|
||
var FileProcessor = module.exports = function (patterns, finder, logcb, blockReplacements) { | ||
if (!patterns) { | ||
throw new Error('No pattern given'); | ||
} | ||
|
@@ -89,6 +104,13 @@ var FileProcessor = module.exports = function (patterns, finder, logcb) { | |
throw new Error('Missing parameter: finder'); | ||
} | ||
this.finder = finder; | ||
|
||
|
||
this.blockReplacements = _.assign({}, defaultBlockReplacements); | ||
if (blockReplacements && _.keys(blockReplacements).length > 0) { | ||
_.assign(this.blockReplacements, blockReplacements); | ||
} | ||
|
||
}; | ||
|
||
// | ||
|
@@ -107,21 +129,13 @@ FileProcessor.prototype.replaceBlocks = function replaceBlocks(file) { | |
|
||
FileProcessor.prototype.replaceWith = function replaceWith(block) { | ||
var result; | ||
var dest = block.dest; | ||
var conditionalStart = block.conditionalStart ? block.conditionalStart + '\n' + block.indent : ''; | ||
var conditionalEnd = block.conditionalEnd ? '\n' + block.indent + block.conditionalEnd : ''; | ||
if (typeof block.src === 'undefined' || block.src === null || block.src.length === 0) { | ||
// there are no css or js files in the block, remove it completely | ||
result = ''; | ||
} else if (block.type === 'css') { | ||
var media = block.media ? ' media="' + block.media + '"' : ''; | ||
result = block.indent + conditionalStart + '<link rel="stylesheet" href="' + dest + '"' + media + '/>' + conditionalEnd; | ||
} else if (block.defer) { | ||
result = block.indent + conditionalStart + '<script defer src="' + dest + '"><\/script>' + conditionalEnd; | ||
} else if (block.async) { | ||
result = block.indent + conditionalStart + '<script async src="' + dest + '"><\/script>' + conditionalEnd; | ||
} else if (block.type === 'js') { | ||
result = block.indent + conditionalStart + '<script src="' + dest + '"><\/script>' + conditionalEnd; | ||
} else if (_.contains(_.keys(this.blockReplacements), block.type)) { | ||
result = block.indent + conditionalStart + this.blockReplacements[block.type](block) + conditionalEnd; | ||
} else { | ||
result = ''; | ||
} | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!-- build:less styles/main.css --> | ||
<link rel="stylesheet/less" href="styles/foo.less"> | ||
<link rel="stylesheet/less" href="styles/bar.less"> | ||
<!-- endbuild --> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,6 +331,27 @@ describe('usemin', function () { | |
assert.ok(changed.match('<img src="images/test.2134.png">')); | ||
}); | ||
|
||
it('should allow for custom block replacement functions', function () { | ||
grunt.log.muted = true; | ||
grunt.config.init(); | ||
grunt.config('usemin', { | ||
html: 'index.html', | ||
options: { | ||
blockReplacements: { | ||
less: function (block) { | ||
return '<link rel="stylesheet" href="' + block.dest + '" />'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} | ||
}); | ||
grunt.file.copy(path.join(__dirname, 'fixtures/block_with_custom_type.html'), 'index.html'); | ||
grunt.task.run('usemin'); | ||
grunt.task.start(); | ||
|
||
var changed = grunt.file.read('index.html'); | ||
// Check replace has performed its duty | ||
assert.equal(changed, '<link rel="stylesheet" href="styles/main.css" />'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
}); | ||
|
||
describe('useminPrepare', function () { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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