Skip to content

Commit f3961df

Browse files
Maël Nisonljharb
authored andcommitted
[New] Implements a "normalize-options" pseudo-hook
1 parent 1018c0e commit f3961df

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/async.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6+
var normalizeOptions = require('./normalize-options.js');
67

78
var defaultIsFile = function isFile(file, cb) {
89
fs.stat(file, function (err, stat) {
@@ -16,8 +17,8 @@ var defaultIsFile = function isFile(file, cb) {
1617

1718
module.exports = function resolve(x, options, callback) {
1819
var cb = callback;
19-
var opts = options || {};
20-
if (typeof opts === 'function') {
20+
var opts = options;
21+
if (typeof options === 'function') {
2122
cb = opts;
2223
opts = {};
2324
}
@@ -28,6 +29,8 @@ module.exports = function resolve(x, options, callback) {
2829
});
2930
}
3031

32+
opts = normalizeOptions(x, opts);
33+
3134
var isFile = opts.isFile || defaultIsFile;
3235
var readFile = opts.readFile || fs.readFile;
3336

lib/normalize-options.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (x, opts) {
2+
/**
3+
* This file is purposefully a passthrough. It's expected that third-party
4+
* environments will override it at runtime in order to inject special logic
5+
* into `resolve` (by manipulating the options). One such example is the PnP
6+
* code path in Yarn.
7+
*/
8+
9+
return opts || {};
10+
};

lib/sync.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6+
var normalizeOptions = require('./normalize-options.js');
67

78
var defaultIsFile = function isFile(file) {
89
try {
@@ -18,7 +19,8 @@ module.exports = function (x, options) {
1819
if (typeof x !== 'string') {
1920
throw new TypeError('Path must be a string.');
2021
}
21-
var opts = options || {};
22+
var opts = normalizeOptions(x, options);
23+
2224
var isFile = opts.isFile || defaultIsFile;
2325
var readFileSync = opts.readFileSync || fs.readFileSync;
2426

0 commit comments

Comments
 (0)