|
5 | 5 | 'use strict';
|
6 | 6 |
|
7 | 7 | const path = require('path');
|
8 |
| -const { createJoinFunction, defaultJoinGenerator, defaultJoinOperation } = require('resolve-url-loader'); |
| 8 | +const { createJoinFunction, createJoinImplementation, defaultJoinGenerator } = require('resolve-url-loader'); |
9 | 9 |
|
10 |
| -module.exports = (options = {}) => { |
11 |
| - const includeRoot = !!options.includeRoot; |
12 |
| - const attempts = Math.max(0, options.attempts) || 1E+9; |
13 |
| - const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json']; |
| 10 | +const generator = function* (item, options, loader) { |
| 11 | + if (item.isAbsolute) { |
| 12 | + for (let tuple of defaultJoinGenerator(item, options, loader)) { |
| 13 | + yield tuple; |
| 14 | + } |
| 15 | + } else { |
| 16 | + const includeRoot = !!options.includeRoot; |
| 17 | + const attempts = Math.max(0, options.attempts) || 1E+9; |
| 18 | + const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json']; |
| 19 | + const resolvedRoot = options.root || process.cwd(); |
14 | 20 |
|
15 |
| - const predicate = typeof options.predicate === 'function' ? |
16 |
| - options.testIsRunning : |
17 |
| - ((fs, absolutePath) => breakOnFile |
18 |
| - .map((file) => path.resolve(absolutePath, file)) |
19 |
| - .every((file) => !fs.existsSync(file) || !fs.statSync(file).isFile())); |
| 21 | + const isFile = (absolutePath) => { |
| 22 | + try { |
| 23 | + return loader.fs.statSync(absolutePath).isFile(); |
| 24 | + } catch (error) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + const isDirectory = (absolutePath) => { |
| 30 | + try { |
| 31 | + return loader.fs.statSync(absolutePath).isDirectory(); |
| 32 | + } catch (error) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + }; |
20 | 36 |
|
21 |
| - const baseGenerator = typeof options.generator === 'function' ? |
22 |
| - options.generator : |
23 |
| - defaultJoinGenerator; |
| 37 | + const predicate = typeof options.predicate === 'function' ? |
| 38 | + options.predicate : |
| 39 | + ((fs, absolutePath) => breakOnFile |
| 40 | + .map((file) => path.resolve(absolutePath, file)) |
| 41 | + .every((absolutePath) => !isFile(absolutePath))); |
24 | 42 |
|
25 |
| - const searchingGeneartor = (filename, uri, bases, isAbsolute, {root, fs}) => { |
26 |
| - const resolvedRoot = !!root && path.resolve(root) || process.cwd(); |
27 | 43 | const testWithinLimit = (absolutePath) => {
|
28 |
| - var relative = path.relative(resolvedRoot, absolutePath); |
| 44 | + const relative = path.relative(resolvedRoot, absolutePath); |
29 | 45 | return !!relative && (relative.slice(0, 2) !== '..');
|
30 | 46 | };
|
31 | 47 |
|
32 | 48 | const enqueue = (queue, excludes, basePath) =>
|
33 |
| - fs.readdirSync(basePath) |
| 49 | + loader.fs.readdirSync(basePath) |
34 | 50 | .filter((filename) => filename.charAt(0) !== '.')
|
35 | 51 | .map((filename) => path.join(basePath, filename))
|
36 |
| - .filter((absolutePath) => fs.existsSync(absolutePath) && fs.statSync(absolutePath).isDirectory()) |
| 52 | + .filter(isDirectory) |
37 | 53 | .filter((absolutePath) => !excludes.contains(absolutePath))
|
38 |
| - .filter((absolutePath) => predicate(fs, absolutePath)) |
| 54 | + .filter((absolutePath) => predicate(loader.fs, absolutePath)) |
39 | 55 | .forEach((absolutePath) => queue.push(absolutePath));
|
40 | 56 |
|
41 |
| - return function* () { |
42 |
| - for (let base of baseGenerator(filename, uri, bases, isAbsolute, options)) { |
43 |
| - // #69 limit searching: make at least one attempt |
44 |
| - let remaining = attempts; |
| 57 | + for (let [absoluteStart, uri] of defaultJoinGenerator(item, options, loader)) { |
| 58 | + // #69 limit searching: make at least one attempt |
| 59 | + let remaining = attempts; |
45 | 60 |
|
46 |
| - // find path to the root, stopping at cwd or at explicit boundary |
47 |
| - const pathToRoot = []; |
48 |
| - let isWorking; |
49 |
| - let absoluteStart = path.resolve(base); |
50 |
| - do { |
51 |
| - pathToRoot.push(absoluteStart); |
52 |
| - isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart); |
53 |
| - absoluteStart = path.resolve(absoluteStart, '..'); |
54 |
| - } while (isWorking); |
| 61 | + // find path to the root, stopping at cwd or at explicit boundary |
| 62 | + const pathToRoot = []; |
| 63 | + let isWorking; |
| 64 | + do { |
| 65 | + pathToRoot.push(absoluteStart); |
| 66 | + isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart); |
| 67 | + absoluteStart = path.resolve(absoluteStart, '..'); |
| 68 | + } while (isWorking); |
55 | 69 |
|
56 |
| - // #62 support stylus nib: optionally force that path to include the root |
57 |
| - const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0); |
58 |
| - const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []); |
| 70 | + // #62 support stylus nib: optionally force that path to include the root |
| 71 | + const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0); |
| 72 | + const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []); |
59 | 73 |
|
60 |
| - // the queue pattern ensures that we favour paths closest the the start path |
61 |
| - // process the queue until empty or until we exhaust our attempts |
62 |
| - while (queue.length && (remaining-- > 0)) { |
63 |
| - var basePath = queue.shift(); |
64 |
| - yield basePath; |
65 |
| - enqueue(queue, pathToRoot, basePath); |
66 |
| - } |
| 74 | + // the queue pattern ensures that we favour paths closest the the start path |
| 75 | + // process the queue until empty or until we exhaust our attempts |
| 76 | + while (queue.length && (remaining-- > 0)) { |
| 77 | + const base = queue.shift(); |
| 78 | + yield [base, uri]; |
| 79 | + enqueue(queue, pathToRoot, base); |
67 | 80 | }
|
68 |
| - }; |
69 |
| - }; |
70 |
| - |
71 |
| - // only decorate relative URIs |
72 |
| - const generator = (filename, uri, bases, isAbsolute, options) => |
73 |
| - (isAbsolute ? baseGenerator : searchingGeneartor)(filename, uri, bases, isAbsolute, options); |
74 |
| - |
75 |
| - return createJoinFunction({ |
76 |
| - name: 'resolve-url-loader-filesearch', |
77 |
| - scheme: 'alstroemeria', |
78 |
| - generator: generator, |
79 |
| - operation: defaultJoinOperation |
80 |
| - }); |
| 81 | + } |
| 82 | + } |
81 | 83 | };
|
| 84 | + |
| 85 | +module.exports = createJoinFunction( |
| 86 | + 'resolve-url-loader-filesearch', |
| 87 | + createJoinImplementation(generator) |
| 88 | +); |
0 commit comments