-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: symlinks in node_modules #257
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,45 +90,74 @@ function resolve( | |
return resolution; | ||
} | ||
} catch (error) {} | ||
if (isDirectImport) { | ||
throw new Error('Failed to resolve module: ' + realModuleName); | ||
} | ||
} | ||
|
||
const dirPaths = []; | ||
for ( | ||
let currDir = path.dirname(originModulePath); | ||
currDir !== '.' && currDir !== path.parse(originModulePath).root; | ||
currDir = path.dirname(currDir) | ||
) { | ||
const searchPath = path.join(currDir, 'node_modules'); | ||
dirPaths.push(path.join(searchPath, realModuleName)); | ||
} | ||
const modulePaths = []; | ||
for (let modulePath of genModulePaths(context, realModuleName)) { | ||
modulePath = context.redirectModulePath(modulePath); | ||
|
||
const extraPaths = []; | ||
const {extraNodeModules} = context; | ||
if (extraNodeModules) { | ||
let bits = path.normalize(moduleName).split(path.sep); | ||
let packageName; | ||
// Normalize packageName and bits for scoped modules | ||
if (bits.length >= 2 && bits[0].startsWith('@')) { | ||
packageName = bits.slice(0, 2).join('/'); | ||
bits = bits.slice(1); | ||
} else { | ||
packageName = bits[0]; | ||
} | ||
if (extraNodeModules[packageName]) { | ||
bits[0] = extraNodeModules[packageName]; | ||
extraPaths.push(path.join.apply(path, bits)); | ||
const result = resolveFileOrDir(context, modulePath, platform); | ||
if (result.type === 'resolved') { | ||
return result.resolution; | ||
} | ||
|
||
modulePaths.push(modulePath); | ||
} | ||
throw new FailedToResolveNameError(modulePaths); | ||
} | ||
|
||
const allDirPaths = dirPaths.concat(extraPaths); | ||
for (let i = 0; i < allDirPaths.length; ++i) { | ||
const realModuleName = context.redirectModulePath(allDirPaths[i]); | ||
const result = resolveFileOrDir(context, realModuleName, platform); | ||
if (result.type === 'resolved') { | ||
return result.resolution; | ||
/** Generate the potential module paths */ | ||
function* genModulePaths( | ||
context: ResolutionContext, | ||
toModuleName: string, | ||
): Iterable<string> { | ||
const {extraNodeModules, follow, originModulePath} = context; | ||
|
||
/** | ||
* Extract the scope and package name from the module name. | ||
*/ | ||
let bits = path.normalize(toModuleName).split(path.sep); | ||
let packageName, scopeName; | ||
if (bits.length >= 2 && bits[0].startsWith('@')) { | ||
packageName = bits.slice(0, 2).join('/'); | ||
scopeName = bits[0]; | ||
bits = bits.slice(2); | ||
} else { | ||
packageName = bits.shift(); | ||
} | ||
|
||
/** | ||
* Find the nearest "node_modules" directory that contains | ||
* the imported package. | ||
*/ | ||
const {root} = path.parse(originModulePath); | ||
let parent = originModulePath; | ||
do { | ||
parent = path.dirname(parent); | ||
if (path.basename(parent) !== 'node_modules') { | ||
yield path.join( | ||
follow(path.join(parent, 'node_modules', packageName)), | ||
...bits, | ||
); | ||
} | ||
} while (parent !== root); | ||
|
||
/** | ||
* Check the user-provided `extraNodeModules` module map for a | ||
* direct mapping to a directory that contains the imported package. | ||
*/ | ||
if (extraNodeModules) { | ||
parent = | ||
extraNodeModules[packageName] || | ||
(scopeName ? extraNodeModules[scopeName] : void 0); | ||
|
||
if (parent) { | ||
yield path.join(follow(path.join(parent, packageName)), ...bits); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous code dealing with The idea is that a package name can be mapped to a extraNodeModules: {
react: '/a/b/c/node_modules'
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nevermind. I think I misread the old code. |
||
} | ||
} | ||
throw new FailedToResolveNameError(dirPaths, extraPaths); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy