broccoli-asset-rewrite
Advanced tools
Comparing version 0.1.0 to 1.0.0
88
index.js
var Filter = require('broccoli-filter'); | ||
var relative = require('relative'); | ||
@@ -8,2 +9,4 @@ function AssetRewrite(inputTree, options) { | ||
Filter.call(this, inputTree, options); | ||
options = options || {}; | ||
@@ -16,2 +19,3 @@ | ||
this.description = options.description; | ||
this.ignore = options.ignore || []; // files to ignore | ||
} | ||
@@ -26,5 +30,36 @@ | ||
return Filter.prototype.processAndCacheFile.apply(this, arguments); | ||
}; | ||
} | ||
AssetRewrite.prototype.processString = function (string) { | ||
/** | ||
* Checks that file is not being ignored and destination doesn't already have a file | ||
* @param relativePath | ||
* @returns {boolean} | ||
*/ | ||
AssetRewrite.prototype.canProcessFile = function(relativePath) { | ||
if (!this.inverseAssetMap) { | ||
var inverseAssetMap = {}; | ||
var assetMap = this.assetMap; | ||
Object.keys(assetMap).forEach(function(key) { | ||
var value = assetMap[key]; | ||
inverseAssetMap[value] = key; | ||
}, this); | ||
this.inverseAssetMap = inverseAssetMap; | ||
} | ||
/* | ||
* relativePath can be fingerprinted or not. | ||
* Check that neither of these variations are being ignored | ||
*/ | ||
if (this.ignore.indexOf(relativePath) !== -1 || this.ignore.indexOf(this.inverseAssetMap[relativePath]) !== -1) { | ||
return false; | ||
} | ||
return Filter.prototype.canProcessFile.apply(this, arguments); | ||
} | ||
AssetRewrite.prototype.rewriteAssetPath = function (string, assetPath, replacementPath) { | ||
var newString = string; | ||
@@ -50,18 +85,43 @@ | ||
var re = new RegExp('["\'\\(=]{1}\\s*([^"\'\\(\\)=]*' + escapeRegExp(assetPath) + '[^"\'\\(\\)\\\\>=]*)\\s*[\\\\]*\\s*["\'\\)> ]{1}', 'g'); | ||
var match = null; | ||
while (match = re.exec(newString)) { | ||
var replaceString = ''; | ||
if (this.prepend && this.prepend !== '') { | ||
replaceString = this.prepend + replacementPath; | ||
} else { | ||
replaceString = match[1].replace(assetPath, replacementPath); | ||
} | ||
newString = newString.replace(new RegExp(escapeRegExp(match[1]), 'g'), replaceString); | ||
} | ||
return newString; | ||
}; | ||
AssetRewrite.prototype.processString = function (string, relativePath) { | ||
var newString = string; | ||
for (var key in this.assetMap) { | ||
if (this.assetMap.hasOwnProperty(key)) { | ||
var re = new RegExp('["\'\\(=]{1}\\s*([^"\'\\(\\)=]*' + key.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1") + '[^"\'\\(\\)\\\\>=]*)\\s*[\\\\]*\\s*["\'\\)> ]{1}', 'g'); | ||
var match = null; | ||
/* | ||
* Rewrite absolute URLs | ||
*/ | ||
while (match = re.exec(newString)) { | ||
var replaceString = ''; | ||
newString = this.rewriteAssetPath(newString, key, this.assetMap[key]); | ||
if (this.prepend && this.prepend !== '') { | ||
replaceString = this.prepend + this.assetMap[key]; | ||
} else { | ||
replaceString = match[1].replace(key, this.assetMap[key]); | ||
} | ||
/* | ||
* Rewrite relative URLs. If there is a prepend, use the full absolute path. | ||
*/ | ||
newString = newString.replace(new RegExp(match[1], 'g'), replaceString); | ||
var pathDiff = relative(relativePath, key); | ||
var replacementDiff = relative(relativePath, this.assetMap[key]); | ||
if (this.prepend && this.prepend !== '') { | ||
replacementDiff = this.assetMap[key]; | ||
} | ||
newString = this.rewriteAssetPath(newString, pathDiff, replacementDiff); | ||
} | ||
@@ -73,2 +133,6 @@ } | ||
function escapeRegExp(string) { | ||
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | ||
} | ||
module.exports = AssetRewrite; |
{ | ||
"name": "broccoli-asset-rewrite", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "broccoli plugin to rewrite a source tree from an asset map.", | ||
@@ -27,3 +27,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"broccoli-filter": "^0.1.6" | ||
"broccoli-filter": "^0.1.10", | ||
"relative": "^1.0.1" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
@@ -51,1 +51,2 @@ #broccoli-asset-rewrite | ||
- `prepend` - Default: `''` - A string to prepend to all of the assets. Useful for CDN urls like `https://subdomain.cloudfront.net/` | ||
- `ignore` - Default: `[]` - Ignore files from being rewritten. |
@@ -40,2 +40,10 @@ var fs = require('fs'); | ||
'images/sample.png': 'images/fingerprinted-sample.png', | ||
'fonts/OpenSans/Light/OpenSans-Light.eot': 'fonts/OpenSans/Light/fingerprinted-OpenSans-Light.eot', | ||
'fonts/OpenSans/Light/OpenSans-Light.woff': 'fonts/OpenSans/Light/fingerprinted-OpenSans-Light.woff', | ||
'fonts/OpenSans/Light/OpenSans-Light.ttf': 'fonts/OpenSans/Light/fingerprinted-OpenSans-Light.ttf', | ||
'fonts/OpenSans/Light/OpenSans-Light.svg': 'fonts/OpenSans/Light/fingerprinted-OpenSans-Light.svg', | ||
'fonts/OpenSans/Medium/OpenSans-Medium.eot': 'fonts/OpenSans/Medium/fingerprinted-OpenSans-Medium.eot', | ||
'fonts/OpenSans/Medium/OpenSans-Medium.woff': 'fonts/OpenSans/Medium/fingerprinted-OpenSans-Medium.woff', | ||
'fonts/OpenSans/Medium/OpenSans-Medium.ttf': 'fonts/OpenSans/Medium/fingerprinted-OpenSans-Medium.ttf', | ||
'fonts/OpenSans/Medium/OpenSans-Medium.svg': 'fonts/OpenSans/Medium/fingerprinted-OpenSans-Medium.svg' | ||
} | ||
@@ -49,2 +57,52 @@ }); | ||
}) | ||
it('ignore option tell filter what files should not be processed', function(){ | ||
var sourcePath = 'tests/fixtures/with-ignore'; | ||
var tree = rewrite(sourcePath + '/input', { | ||
assetMap: { | ||
'foo/bar/widget.js': 'blahzorz-1.js', | ||
'images/sample.png': 'images/fingerprinted-sample.png', | ||
}, | ||
ignore: ['ignore-this-file.html'] | ||
}); | ||
builder = new broccoli.Builder(tree); | ||
return builder.build().then(function(graph) { | ||
confirmOutput(graph.directory, sourcePath + '/output'); | ||
}); | ||
}); | ||
it('rewrites relative urls', function () { | ||
var sourcePath = 'tests/fixtures/relative-urls'; | ||
var tree = rewrite(sourcePath + '/input', { | ||
assetMap: { | ||
'foo/bar/widget.js': 'blahzorz-1.js', | ||
'images/sample.png': 'images/fingerprinted-sample.png', | ||
'assets/images/foobar.png': 'assets/images/foobar-fingerprint.png' | ||
} | ||
}); | ||
builder = new broccoli.Builder(tree); | ||
return builder.build().then(function (graph) { | ||
confirmOutput(graph.directory, sourcePath + '/output'); | ||
}); | ||
}); | ||
it('rewrites relative urls with prepend', function () { | ||
var sourcePath = 'tests/fixtures/relative-urls-prepend'; | ||
var tree = rewrite(sourcePath + '/input', { | ||
assetMap: { | ||
'foo/bar/widget.js': 'blahzorz-1.js', | ||
'images/sample.png': 'images/fingerprinted-sample.png', | ||
'assets/images/foobar.png': 'assets/images/foobar-fingerprint.png' | ||
}, | ||
prepend: 'https://cloudfront.net/' | ||
}); | ||
builder = new broccoli.Builder(tree); | ||
return builder.build().then(function (graph) { | ||
confirmOutput(graph.directory, sourcePath + '/output'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15459
39
235
0
52
2
+ Addedrelative@^1.0.1
+ Addedrelative@1.2.0(transitive)
Updatedbroccoli-filter@^0.1.10