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

Commit d65eea6

Browse files
doc(README): review and add explanations about resolveSource hook
1 parent 7f1592c commit d65eea6

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

README.md

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ It uses only one target: `html`, with a list of the concerned files. For example
141141
By default, it will consider the directory where the looked-at file is located as the 'root' filesystem. Each relative path (for example to a javascript file) will be resolved from this path. Same goes for the absolute ones.
142142
If you need to change the 'root' dir, use the `root` option (see below).
143143

144-
Note that `useminPrepare` and `usemin` tasks will both throw an error if a resource is not found.
144+
Note that `useminPrepare` and `usemin` tasks will both throw an error if a resource is not found.
145145

146146
```js
147147
useminPrepare: {
@@ -260,18 +260,23 @@ The given steps or post-processors may be specified as strings (for the default
260260
### resolveSource
261261

262262
Type: 'function'
263-
Default: `nil`
263+
Default: `undefined`
264264

265265
This is an optional hook allowing applications to override how source urls
266266
are resolved to filesystem paths. This function is called with the signature
267-
`resolveSource(sourceUrl, fileDir, fileName, blockTarget, searchPath)`. It should
268-
return the path to the source file, `null` to indicate the default resolution
269-
rules should be used, or `false` to indicate the file cannot be resolved.
267+
`resolveSource(sourceUrl, fileDir, fileName, blockTarget, searchPath)`, where:
268+
269+
* `sourceUrl` is the source reference being resolved,
270+
* `fileDir` and `fileName` are respectively, the directory and name of the file, the reference was found in,
271+
* `blockTarget` is the target path for the block the reference occurred within,
272+
* `searchPath` is an array of the directories that would be searched by default.
273+
274+
The return value of this function should be :
275+
* the path to the source file,
276+
* `null` to indicate the default resolution rules should be used to resolve `sourceUrl`,
277+
* `false` to indicate the file cannot be resolved, thus default resolution rules are not used.
270278

271-
`sourceUrl` is the source reference being resolved, `fileDir` and `fileName`
272-
are the directory and name of the file the reference was found in,
273-
`blockTarget` is the target path for the block the reference occurred within,
274-
and `searchPath` is an array of the directories that would be searched by default.
279+
The default resolution is to search the `sourceUrl` in all provided entries in `searchPath` array.
275280

276281
For example:
277282

@@ -281,32 +286,49 @@ For example:
281286
'useminPrepare', {
282287
options: {
283288
resolveSource: function (sourceUrl, fileDir, fileName, blockTarget, searchPath) {
284-
var fs = require('fs'), path = require('path');
285-
var m = sourceUrl.match(/^\/alt-static-url\/(.*)/);
286-
if(m){
287-
var source = path.join("alt-static-location", m[1]);
288-
if(fs.existsSync(source)) { return source; }
289+
var fs = require('fs');
290+
var path = require('path');
291+
292+
var match = sourceUrl.match(/^\/alt-static-url\/(.*)/);
293+
if (match) {
294+
var source = path.join("alt-static-location", match[1]);
295+
// return the override source path if found on filesystem
296+
// else will default to null which means that resource will be search using
297+
// default behavior
298+
if (fs.existsSync(source)) {
299+
return source;
300+
}
301+
/*
302+
// You might be interested in return false if source cannot be resolved using the
303+
// following code instead:
304+
// it will return the overriden source path if resource found on filesystem
305+
// or false if not found, meaning default behavior will not be used
306+
return fs.existsSync(source) ? source : false;
307+
*/
289308
}
309+
// returning null will match the default behavior for any url not matching
310+
// the prefix `^/alt-static-url/`
290311
return null;
291312
}
292313
}
293314
}
294315
```
295316

296-
* To replicate the default behavior:
317+
* To replicate the default behavior, use the following version. It should be noted that this is only for illustration purpose, as the default behavior will be used if no `resolveSource` hook is provided, or if `resolveSource` fails to resolve the sourceUrl, hence returning null.
297318

298319
```js
299320
'useminPrepare', {
300321
options: {
301322
resolveSource: function (sourceUrl, fileDir, fileName, blockTarget, searchPath) {
302323
var fs = require('fs');
303324
var path = require('path');
304-
for (var i=0; i < searchPath.length; ++i) {
305-
var source = path.join(searchPath[i], fname);
306-
if (fs.existsSync(source)) {
307-
return source;
308-
}
325+
for (var i = 0; i < searchPath.length; ++i) {
326+
var source = path.join(searchPath[i], fname);
327+
if (fs.existsSync(source)) {
328+
return source;
329+
}
309330
}
331+
// source not found, so returning false as it cannot be resolved
310332
return false;
311333
}
312334
}

lib/configwriter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var ConfigWriter = module.exports = function (flow, dirs, options) {
6464
this.dest = dirs.dest;
6565
this.staging = dirs.staging;
6666

67-
// user will be noticed by default about missing files
67+
// optional hook to resolve source url
6868
this.resolveSource = options && options.resolveSource;
6969

7070
this.steps = {};
@@ -180,6 +180,7 @@ ConfigWriter.prototype.process = function (file, config) {
180180

181181
function resolveInFile (fname) {
182182
var source = null;
183+
// if a resolveSource hook function is provided use it for file source
183184
if (self.resolveSource) {
184185
source = self.resolveSource(fname, lfile.dir, lfile.name, block.dest, searchPath);
185186
if (source) {
@@ -195,6 +196,9 @@ ConfigWriter.prototype.process = function (file, config) {
195196
return source;
196197
}
197198
}
199+
// default behavior to search file in provided search paths.
200+
// * source being null meaning that no resolveSource hook provided
201+
// * source being false meaning that
198202
if (source !== false) {
199203
for (var i = 0; i < searchPath.length; ++i) {
200204
source = path.join(searchPath[i], fname);

0 commit comments

Comments
 (0)