Skip to content

Commit

Permalink
Merge pull request #192 from bholloway/more-changes-to-join-api
Browse files Browse the repository at this point in the history
More changes to join api
  • Loading branch information
bholloway committed Mar 25, 2021
2 parents 8a76bb3 + d5ea53c commit 3bf87c4
Show file tree
Hide file tree
Showing 26 changed files with 1,398 additions and 858 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"maxlen": 120,
"scripturl": true,
"node": true,
"esversion": 6
"esversion": 9
}
117 changes: 62 additions & 55 deletions packages/resolve-url-loader-filesearch/index.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,84 @@
'use strict';

const path = require('path');
const { createJoinFunction, defaultJoinGenerator, defaultJoinOperation } = require('resolve-url-loader');
const { createJoinFunction, createJoinImplementation, defaultJoinGenerator } = require('resolve-url-loader');

module.exports = (options = {}) => {
const includeRoot = !!options.includeRoot;
const attempts = Math.max(0, options.attempts) || 1E+9;
const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json'];
const generator = function* (item, options, loader) {
if (item.isAbsolute) {
for (let tuple of defaultJoinGenerator(item, options, loader)) {
yield tuple;
}
} else {
const includeRoot = !!options.includeRoot;
const attempts = Math.max(0, options.attempts) || 1E+9;
const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json'];
const resolvedRoot = options.root || process.cwd();

const predicate = typeof options.predicate === 'function' ?
options.testIsRunning :
((fs, absolutePath) => breakOnFile
.map((file) => path.resolve(absolutePath, file))
.every((file) => !fs.existsSync(file) || !fs.statSync(file).isFile()));
const isFile = (absolutePath) => {
try {
return loader.fs.statSync(absolutePath).isFile();
} catch (error) {
return false;
}
};

const isDirectory = (absolutePath) => {
try {
return loader.fs.statSync(absolutePath).isDirectory();
} catch (error) {
return false;
}
};

const baseGenerator = typeof options.generator === 'function' ?
options.generator :
defaultJoinGenerator;
const predicate = typeof options.predicate === 'function' ?
options.predicate :
((fs, absolutePath) => breakOnFile
.map((file) => path.resolve(absolutePath, file))
.every((absolutePath) => !isFile(absolutePath)));

const searchingGeneartor = (filename, uri, bases, isAbsolute, {root, fs}) => {
const resolvedRoot = !!root && path.resolve(root) || process.cwd();
const testWithinLimit = (absolutePath) => {
var relative = path.relative(resolvedRoot, absolutePath);
const relative = path.relative(resolvedRoot, absolutePath);
return !!relative && (relative.slice(0, 2) !== '..');
};

const enqueue = (queue, excludes, basePath) =>
fs.readdirSync(basePath)
loader.fs.readdirSync(basePath)
.filter((filename) => filename.charAt(0) !== '.')
.map((filename) => path.join(basePath, filename))
.filter((absolutePath) => fs.existsSync(absolutePath) && fs.statSync(absolutePath).isDirectory())
.filter(isDirectory)
.filter((absolutePath) => !excludes.contains(absolutePath))
.filter((absolutePath) => predicate(fs, absolutePath))
.filter((absolutePath) => predicate(loader.fs, absolutePath))
.forEach((absolutePath) => queue.push(absolutePath));

return function* () {
for (let base of baseGenerator(filename, uri, bases, isAbsolute, options)) {
// #69 limit searching: make at least one attempt
let remaining = attempts;
for (let [absoluteStart, uri] of defaultJoinGenerator(item, options, loader)) {
// #69 limit searching: make at least one attempt
let remaining = attempts;

// find path to the root, stopping at cwd or at explicit boundary
const pathToRoot = [];
let isWorking;
let absoluteStart = path.resolve(base);
do {
pathToRoot.push(absoluteStart);
isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart);
absoluteStart = path.resolve(absoluteStart, '..');
} while (isWorking);
// find path to the root, stopping at cwd or at explicit boundary
const pathToRoot = [];
let isWorking;
do {
pathToRoot.push(absoluteStart);
isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart);
absoluteStart = path.resolve(absoluteStart, '..');
} while (isWorking);

// #62 support stylus nib: optionally force that path to include the root
const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0);
const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []);
// #62 support stylus nib: optionally force that path to include the root
const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0);
const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []);

// the queue pattern ensures that we favour paths closest the the start path
// process the queue until empty or until we exhaust our attempts
while (queue.length && (remaining-- > 0)) {
var basePath = queue.shift();
yield basePath;
enqueue(queue, pathToRoot, basePath);
}
// the queue pattern ensures that we favour paths closest the the start path
// process the queue until empty or until we exhaust our attempts
while (queue.length && (remaining-- > 0)) {
const base = queue.shift();
yield [base, uri];
enqueue(queue, pathToRoot, base);
}
};
};

// only decorate relative URIs
const generator = (filename, uri, bases, isAbsolute, options) =>
(isAbsolute ? baseGenerator : searchingGeneartor)(filename, uri, bases, isAbsolute, options);

return createJoinFunction({
name: 'resolve-url-loader-filesearch',
scheme: 'alstroemeria',
generator: generator,
operation: defaultJoinOperation
});
}
}
};

module.exports = createJoinFunction(
'resolve-url-loader-filesearch',
createJoinImplementation(generator)
);
2 changes: 2 additions & 0 deletions packages/resolve-url-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ With functions and mixins and multiple nesting it gets more complicated. Read mo

## Getting started

> **Upgrading?** the [changelog](CHANGELOG.md) shows how to migrate your webpack config.
### Install

via npm
Expand Down

0 comments on commit 3bf87c4

Please sign in to comment.