Skip to content

Commit 2e520d2

Browse files
committed
Allow non-prefix files if prefix version missing
This fixes #5 == modules Prefixed files are checked first and then fallback to non-prefixed files. == globs All the matches are checked and if a prefixed version can't be found but a non-prefixed version exists that match is used instead. Example: --- _baz.css --- bar.css --- baz.css Given the above list of files and `*.css` as the glob it would return [`_baz.css`, `bar.css`]
1 parent 77fbc0a commit 2e520d2

File tree

13 files changed

+101
-22
lines changed

13 files changed

+101
-22
lines changed

lib/resolve-glob.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,51 @@ var globby = require('globby');
33
var addPrefix = require('./add-prefix');
44
var hasPrefix = require('./has-prefix');
55
var hasExtensions = require('./has-extensions');
6+
var reduce = require('lodash/fp/reduce');
7+
var findIndex = require('lodash/fp/findIndex');
8+
var map = require('lodash/fp/map');
9+
var flow = require('lodash/fp/flow');
10+
var filter = require('lodash/fp/filter');
11+
12+
function checkPrefixedVersionExists(fileToCheck, paths, prefix) {
13+
var index = findIndex(p => {
14+
const currentFile = path.basename(p);
15+
return currentFile === prefix + fileToCheck;
16+
}, paths);
17+
return index === -1;
18+
}
619

720
module.exports = function resolveGlob(id, base, opts) {
821
var prefix = opts.prefix;
922
var extensions = opts.extensions;
1023
var paths = [base].concat(opts.path);
11-
var patterns = [];
1224
var prefixedId = prefix ? addPrefix(id, prefix) : null;
1325

14-
paths.forEach(function (p) {
15-
[''].concat(extensions).forEach(function (ext) {
26+
var patterns = reduce((acc, p) => {
27+
[''].concat(extensions).forEach(ext => {
1628
if (prefix) {
17-
patterns.push(path.resolve(p, prefixedId + ext));
29+
acc.push(path.resolve(p, prefixedId + ext));
1830
}
19-
patterns.push(path.resolve(p, id + ext));
31+
acc.push(path.resolve(p, id + ext));
2032
});
21-
});
33+
return acc;
34+
}, [], paths);
2235

23-
return globby(patterns).then(function (files) {
24-
return files.filter(function (file) {
25-
return hasExtensions(file, extensions) &&
26-
(!prefix || hasPrefix(file, prefix));
27-
}).map(function (file) {
28-
return path.normalize(file);
36+
return globby(patterns)
37+
.then(files => {
38+
return flow(
39+
// Allows a file through if it has a prefix. If it doesn't have
40+
// a prefix the current list of files are checked to see if a
41+
// prefix version exists and if not it is added.
42+
reduce((acc, item) => {
43+
var fileName = path.basename(item);
44+
if (hasPrefix(item, prefix) || checkPrefixedVersionExists(fileName, acc, prefix)) { // eslint-disable-line max-len
45+
acc.push(item);
46+
}
47+
return acc;
48+
}, []),
49+
filter(file => hasExtensions(file, extensions)),
50+
map(file => path.normalize(file))
51+
)(files);
2952
});
30-
});
3153
};

lib/resolve-module.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ module.exports = function (id, base, opts) {
4141
}
4242
};
4343

44-
return resolve('./' + prefixedId, resolveOpts).catch(function () {
45-
if (!prefix) {
46-
throw Error();
47-
}
48-
return resolve(prefixedId, resolveOpts);
49-
}).catch(function () {
50-
return resolve(id, resolveOpts);
51-
});
44+
return resolve('./' + prefixedId, resolveOpts)
45+
.catch(() => {
46+
if (!prefix) {
47+
throw Error();
48+
}
49+
return resolve(prefixedId, resolveOpts);
50+
})
51+
.catch(() => resolve('./' + id, resolveOpts))
52+
.catch(() => resolve(id, resolveOpts));
5253
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"globby": "^6.1.0",
2222
"is-glob": "^3.1.0",
23+
"lodash": "^4.17.4",
2324
"object-assign": "^4.0.1",
2425
"pify": "^2.3.0",
2526
"postcss": "^5.0.15",

test/fixtures/glob/prefixed/without/_baz.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/_foo.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/_z.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/bar.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/baz.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/file.css

Whitespace-only changes.

test/fixtures/glob/prefixed/without/foo.css

Whitespace-only changes.

0 commit comments

Comments
 (0)