forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes hard-coded module aliases to be relative
Without this, modules built with Babel or Webpack would have hard-coded absolute paths all the way back to the root of the filesystem. This prevented compilation and running on different machines or even from different directories on the same machine. With this change, paths are hard-coded to the top-most node_madules directory found, which should make them portable relative to the app. Fixes vercel#1160
- Loading branch information
Fin Hopkins
committed
Feb 15, 2017
1 parent
2c9c84e
commit 9f6fa79
Showing
3 changed files
with
46 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Next.js needs to use module.resolve to generate paths to modules it includes, | ||
// but those paths need to be relative to something so that they're portable | ||
// across directories and machines. | ||
// | ||
// This function returns paths relative to the top-level 'node_modules' | ||
// directory found in the path. If none is found, returns the complete path. | ||
|
||
const RELATIVE_START = 'node_modules/' | ||
|
||
// Pass in the module's `require` object since it's module-specific. | ||
export default (moduleRequire) => (path) => { | ||
// package.json removed because babel-runtime is resolved as | ||
// "babel-runtime/package" | ||
const absolutePath = moduleRequire.resolve(path) | ||
.replace(/[\\/]package\.json$/, '') | ||
|
||
const relativeStartIndex = absolutePath.indexOf(RELATIVE_START) | ||
|
||
if (relativeStartIndex === -1) { | ||
return absolutePath | ||
} | ||
|
||
return absolutePath.substring(relativeStartIndex + RELATIVE_START.length) | ||
} |
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