-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pull resolve.sync code directly into default_resolver. (incomplete) * Strip out extraneous core and caller dependencies. * Pull node-modules-path into codebase as well. (incomplete) * Remove dependency on path-parse. Assume existence of path.parse (added in v0.11.15). * Use internal resolve within default_resolver. * Adhere to linter constraints. * Extend basic error type with code property. * Fix typing and remove extraneous options. * Gather helper methods at end of resolveSync(). * Rename "start" parameter to more obvious "basedir". * Avoid shadowing basedir variable. * Inline loadNodeModulesSync() method. * Clarify relative import vs node_modules. * Use import instead of require. * Small logic and formatting simplifications. * Use isBuiltinModule() to detect core modules. Restore use of moduleDirectory option. * Remove package dependency on resolve. * Make node_modules_paths available to resolver core logic, rather than just default_resolver. * Add standard header to node_modules_paths.js. Add adaptation note to default_resolver.js. * Trim out unused options when calling nodeModulesPaths().
- Loading branch information
Showing
4 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* Adapted from: https://github.com/substack/node-resolve | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Path} from 'types/Config'; | ||
import path from 'path'; | ||
|
||
type NodeModulesPathsOptions = {| | ||
moduleDirectory?: Array<string>, | ||
paths?: ?Array<Path>, | ||
|}; | ||
|
||
function nodeModulesPaths( | ||
basedir: Path, | ||
options: NodeModulesPathsOptions, | ||
): Path[] { | ||
const modules = | ||
options && options.moduleDirectory | ||
? [].concat(options.moduleDirectory) | ||
: ['node_modules']; | ||
|
||
// ensure that `basedir` is an absolute path at this point, | ||
// resolving against the process' current working directory | ||
const basedirAbs = path.resolve(basedir); | ||
|
||
let prefix = '/'; | ||
if (/^([A-Za-z]:)/.test(basedirAbs)) { | ||
prefix = ''; | ||
} else if (/^\\\\/.test(basedirAbs)) { | ||
prefix = '\\\\'; | ||
} | ||
|
||
const paths = [basedirAbs]; | ||
let parsed = path.parse(basedirAbs); | ||
while (parsed.dir !== paths[paths.length - 1]) { | ||
paths.push(parsed.dir); | ||
parsed = path.parse(parsed.dir); | ||
} | ||
|
||
const dirs = paths.reduce((dirs, aPath) => { | ||
return dirs.concat( | ||
modules.map(moduleDir => { | ||
return path.join(prefix, aPath, moduleDir); | ||
}), | ||
); | ||
}, []); | ||
|
||
return options.paths ? dirs.concat(options.paths) : dirs; | ||
} | ||
|
||
module.exports = nodeModulesPaths; |