Skip to content

Commit

Permalink
Merge c6549c9 into ee63d95
Browse files Browse the repository at this point in the history
  • Loading branch information
jussi-kalliokoski committed Jan 20, 2015
2 parents ee63d95 + c6549c9 commit f266e11
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ This will give you two functions, `html()` and `css()` to use respectively for w
## Options

* `assetsDir` (defaults to `.`) a string that determines the path where to seek the replaced references from.
* `createLink` (optional) if specified, overrides the default link construction functionality. The function should return a string and input will be an object such as the following:

```javascript
{
original: "the full original link",
originalDirname: "the dirname of the original link",
originalBasename: "the basename of the original link (without extension)",
originalExtname: "the file extension of the original link",
query: "the query part of the link",
fragment: "the fragment part of the link",
dirname: "the dirname of the best match (if applicable, otherwise undefined)",
basename: "the basename of the best match (if applicable, otherwise undefined)",
extname: "the extname of the best match (if applicable, otherwise undefined)",
}
```

## Examples

Expand Down
8 changes: 5 additions & 3 deletions lib/utils/createResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ var _ = require("lodash");
var globQ = require("./globQ");
var resolve = require("./resolve");
var mergeChunks = require("./mergeChunks");
var defaultCreateLink = require("./defaultCreateLink");

module.exports = function (findReferences) {
return function Stream (options) {
options = _.extend({
assetsDir: "."
assetsDir: ".",
createLink: defaultCreateLink,
}, options);

var availableFiles = globQ("**/*", {
Expand All @@ -23,14 +25,14 @@ module.exports = function (findReferences) {

availableFiles.then(function (files) {
content.links = content.links.map(function (link) {
return resolve(link, files);
return options.createLink(resolve(link, files));
});

var replacedContent = mergeChunks(content);
file.contents = new Buffer(replacedContent);
self.push(file);
callback();
});
}).catch(this.emit.bind(this, "error"));
};

return through.obj(process);
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/defaultCreateLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = function defaultCreateLink (options) {
if ( !options.basename ) { return options.original; }

return options.originalDirname + options.basename + options.extname + options.query + options.fragment;
};
28 changes: 21 additions & 7 deletions lib/utils/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@ var _ = require("lodash");
var stripExtension = require("./stripExtension");

module.exports = function (link, availableFiles) {
// strip fragment
var linkName = link.replace(/([?#].*)?$/, "");
var fragment = RegExp.$1;
// strip query and fragment
var linkName = link.replace(/(\?[^#]*)?(#.*)?$/, "");
var query = RegExp.$1;
var fragment = RegExp.$2;
var extname = path.extname(linkName);
var basename = stripExtension(path.basename(linkName), extname);
var prefix = linkName.substr(0, linkName.length - path.basename(linkName).length);
var dirname = linkName.substr(0, linkName.length - basename.length - extname.length - query.length - fragment.length);

var result = {
original: link,
originalDirname: dirname,
originalBasename: basename,
originalExtname: extname,
query: query,
fragment: fragment,
};

if ( !basename ) {
return link;
return result;
}

var bestMatch = _.find(availableFiles, function (file) {
return extname === path.extname(file) && path.basename(file).indexOf(basename) !== -1;
});

if ( !bestMatch ) {
return link;
return result;
}

return prefix + path.basename(bestMatch) + fragment;
result.dirname = path.dirname(bestMatch);
result.extname = path.extname(bestMatch);
result.basename = stripExtension(path.basename(bestMatch), result.extname);

return result;
};
3 changes: 3 additions & 0 deletions test/e2e/expected/css-custom-link-processing/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-image: url("/foo/rat.4890123.png?v=1.1.1#faa");
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/e2e/fixtures/css-custom-link-processing/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-image: url("cat.jpg?v=4.0.3#foo");
}
19 changes: 13 additions & 6 deletions test/e2e/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ var attempt = function (done, callback) {
};
};

var verifyBuild = function (type, name) {
var verifyBuild = function (type, name, createLink) {
var options = {};

options.assetsDir = "./test/e2e/fixtures/" + name + "/";
if ( createLink ) { options.createLink = createLink; }

var expected = gulp.src(path.join("./test/e2e/expected", name, "index." + type));
var actual = gulp.src("./test/e2e/fixtures/" + name + "/index." + type)
.pipe(resolver[type]({
assetsDir: "./test/e2e/fixtures/" + name + "/"
}));
.pipe(resolver[type](options));
return actual.should.produce.sameFilesAs(expected);
};

var buildVerifier = function (type, name) {
var buildVerifier = function (type, name, createLink) {
return function () {
return verifyBuild(type, type + "-" + name);
return verifyBuild(type, type + "-" + name, createLink);
};
};

Expand Down Expand Up @@ -60,5 +63,9 @@ describe("gulp-resolver", function () {
it("should handle dots in query parameters", buildVerifier("css", "query-parameters-with-dots-in-urls"));
it("should empty search query string in URLs", buildVerifier("css", "empty-search-in-urls"));
it("should preserve relative paths", buildVerifier("css", "relative-path"));
it("should allow custom link post-processing", buildVerifier("css", "custom-link-processing", function (link) {
return path.join("/foo/", link.dirname, link.basename.replace(/^c/, "r") + link.extname.replace(/jp/, "pn") +
link.query.replace(/\d/g, "1") + link.fragment.replace(/o/g, "a"));
}));
});
});

0 comments on commit f266e11

Please sign in to comment.